diff options
author | Michal Nazarewicz <m.nazarewicz@samsung.com> | 2009-11-09 08:15:24 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2009-12-11 14:55:23 -0500 |
commit | 8ea864cffdfd327117d4b7829935974b3f47ff31 (patch) | |
tree | 8af069b335c60b52d6e48b81ad06badc4471be9a | |
parent | d26a6aa08b9f12b44fb1ee65625e7480d3d5bb81 (diff) |
USB: g_mass_storage: most data moved to fsg_common
Most of the data from fsg_dev have been moved to fsg_common
structure. The fsg_dev structure holds only endpoint dependent
data. The fsg_common structure has a fsg pointer which points
to active fsg_dev structure -- endpoints are referenced via this
pointer.
This fixes the problem of several threads created when a single
instance of MSF is used in several USB configurations.
Signed-off-by: Michal Nazarewicz <m.nazarewicz@samsung.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r-- | drivers/usb/gadget/f_mass_storage.c | 1230 |
1 files changed, 648 insertions, 582 deletions
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 5eaf22db7fcc..a6cec37768a9 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c | |||
@@ -312,14 +312,26 @@ static const char fsg_string_interface[] = "Mass Storage"; | |||
312 | 312 | ||
313 | /*-------------------------------------------------------------------------*/ | 313 | /*-------------------------------------------------------------------------*/ |
314 | 314 | ||
315 | struct fsg_dev; | ||
316 | |||
315 | 317 | ||
316 | /* Data shared by all the FSG instances. */ | 318 | /* Data shared by all the FSG instances. */ |
317 | struct fsg_common { | 319 | struct fsg_common { |
318 | struct usb_gadget *gadget; | 320 | struct usb_gadget *gadget; |
321 | struct fsg_dev *fsg; | ||
322 | struct fsg_dev *prev_fsg; | ||
319 | 323 | ||
320 | /* filesem protects: backing files in use */ | 324 | /* filesem protects: backing files in use */ |
321 | struct rw_semaphore filesem; | 325 | struct rw_semaphore filesem; |
322 | 326 | ||
327 | /* lock protects: state, all the req_busy's */ | ||
328 | spinlock_t lock; | ||
329 | |||
330 | struct usb_ep *ep0; /* Copy of gadget->ep0 */ | ||
331 | struct usb_request *ep0req; /* Copy of cdev->req */ | ||
332 | unsigned int ep0_req_tag; | ||
333 | const char *ep0req_name; | ||
334 | |||
323 | struct fsg_buffhd *next_buffhd_to_fill; | 335 | struct fsg_buffhd *next_buffhd_to_fill; |
324 | struct fsg_buffhd *next_buffhd_to_drain; | 336 | struct fsg_buffhd *next_buffhd_to_drain; |
325 | struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; | 337 | struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; |
@@ -332,10 +344,28 @@ struct fsg_common { | |||
332 | struct fsg_lun *luns; | 344 | struct fsg_lun *luns; |
333 | struct fsg_lun *curlun; | 345 | struct fsg_lun *curlun; |
334 | 346 | ||
347 | unsigned int bulk_out_maxpacket; | ||
348 | enum fsg_state state; /* For exception handling */ | ||
349 | unsigned int exception_req_tag; | ||
350 | |||
351 | u8 config, new_config; | ||
352 | enum data_direction data_dir; | ||
353 | u32 data_size; | ||
354 | u32 data_size_from_cmnd; | ||
355 | u32 tag; | ||
356 | u32 residue; | ||
357 | u32 usb_amount_left; | ||
358 | |||
335 | unsigned int can_stall:1; | 359 | unsigned int can_stall:1; |
336 | unsigned int free_storage_on_release:1; | 360 | unsigned int free_storage_on_release:1; |
361 | unsigned int phase_error:1; | ||
362 | unsigned int short_packet_received:1; | ||
363 | unsigned int bad_lun_okay:1; | ||
364 | unsigned int running:1; | ||
337 | 365 | ||
338 | const char *thread_name; | 366 | int thread_wakeup_needed; |
367 | struct completion thread_notifier; | ||
368 | struct task_struct *thread_task; | ||
339 | 369 | ||
340 | /* Vendor (8 chars), product (16 chars), release (4 | 370 | /* Vendor (8 chars), product (16 chars), release (4 |
341 | * hexadecimal digits) and NUL byte */ | 371 | * hexadecimal digits) and NUL byte */ |
@@ -367,52 +397,32 @@ struct fsg_config { | |||
367 | 397 | ||
368 | struct fsg_dev { | 398 | struct fsg_dev { |
369 | struct usb_function function; | 399 | struct usb_function function; |
370 | struct usb_composite_dev *cdev; | ||
371 | struct usb_gadget *gadget; /* Copy of cdev->gadget */ | 400 | struct usb_gadget *gadget; /* Copy of cdev->gadget */ |
372 | struct fsg_common *common; | 401 | struct fsg_common *common; |
373 | 402 | ||
374 | u16 interface_number; | 403 | u16 interface_number; |
375 | 404 | ||
376 | /* lock protects: state, all the req_busy's */ | ||
377 | spinlock_t lock; | ||
378 | |||
379 | struct usb_ep *ep0; /* Copy of gadget->ep0 */ | ||
380 | struct usb_request *ep0req; /* Copy of cdev->req */ | ||
381 | unsigned int ep0_req_tag; | ||
382 | const char *ep0req_name; | ||
383 | |||
384 | unsigned int bulk_out_maxpacket; | ||
385 | enum fsg_state state; /* For exception handling */ | ||
386 | unsigned int exception_req_tag; | ||
387 | |||
388 | u8 config, new_config; | ||
389 | |||
390 | unsigned int running:1; | ||
391 | unsigned int bulk_in_enabled:1; | 405 | unsigned int bulk_in_enabled:1; |
392 | unsigned int bulk_out_enabled:1; | 406 | unsigned int bulk_out_enabled:1; |
393 | unsigned int phase_error:1; | ||
394 | unsigned int short_packet_received:1; | ||
395 | unsigned int bad_lun_okay:1; | ||
396 | unsigned int can_stall:1; | ||
397 | 407 | ||
398 | unsigned long atomic_bitflags; | 408 | unsigned long atomic_bitflags; |
399 | #define REGISTERED 0 | 409 | #define IGNORE_BULK_OUT 0 |
400 | #define IGNORE_BULK_OUT 1 | ||
401 | 410 | ||
402 | struct usb_ep *bulk_in; | 411 | struct usb_ep *bulk_in; |
403 | struct usb_ep *bulk_out; | 412 | struct usb_ep *bulk_out; |
413 | }; | ||
404 | 414 | ||
405 | int thread_wakeup_needed; | ||
406 | struct completion thread_notifier; | ||
407 | struct task_struct *thread_task; | ||
408 | 415 | ||
409 | enum data_direction data_dir; | 416 | static inline int __fsg_is_set(struct fsg_common *common, |
410 | u32 data_size; | 417 | const char *func, unsigned line) |
411 | u32 data_size_from_cmnd; | 418 | { |
412 | u32 tag; | 419 | if (common->fsg) |
413 | u32 residue; | 420 | return 1; |
414 | u32 usb_amount_left; | 421 | ERROR(common, "common->fsg is NULL in %s at %u\n", func, line); |
415 | }; | 422 | return 0; |
423 | } | ||
424 | |||
425 | #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__)) | ||
416 | 426 | ||
417 | 427 | ||
418 | static inline struct fsg_dev *fsg_from_func(struct usb_function *f) | 428 | static inline struct fsg_dev *fsg_from_func(struct usb_function *f) |
@@ -423,21 +433,21 @@ static inline struct fsg_dev *fsg_from_func(struct usb_function *f) | |||
423 | 433 | ||
424 | typedef void (*fsg_routine_t)(struct fsg_dev *); | 434 | typedef void (*fsg_routine_t)(struct fsg_dev *); |
425 | 435 | ||
426 | static int exception_in_progress(struct fsg_dev *fsg) | 436 | static int exception_in_progress(struct fsg_common *common) |
427 | { | 437 | { |
428 | return (fsg->state > FSG_STATE_IDLE); | 438 | return common->state > FSG_STATE_IDLE; |
429 | } | 439 | } |
430 | 440 | ||
431 | /* Make bulk-out requests be divisible by the maxpacket size */ | 441 | /* Make bulk-out requests be divisible by the maxpacket size */ |
432 | static void set_bulk_out_req_length(struct fsg_dev *fsg, | 442 | static void set_bulk_out_req_length(struct fsg_common *common, |
433 | struct fsg_buffhd *bh, unsigned int length) | 443 | struct fsg_buffhd *bh, unsigned int length) |
434 | { | 444 | { |
435 | unsigned int rem; | 445 | unsigned int rem; |
436 | 446 | ||
437 | bh->bulk_out_intended_length = length; | 447 | bh->bulk_out_intended_length = length; |
438 | rem = length % fsg->bulk_out_maxpacket; | 448 | rem = length % common->bulk_out_maxpacket; |
439 | if (rem > 0) | 449 | if (rem > 0) |
440 | length += fsg->bulk_out_maxpacket - rem; | 450 | length += common->bulk_out_maxpacket - rem; |
441 | bh->outreq->length = length; | 451 | bh->outreq->length = length; |
442 | } | 452 | } |
443 | 453 | ||
@@ -463,47 +473,46 @@ static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) | |||
463 | /* These routines may be called in process context or in_irq */ | 473 | /* These routines may be called in process context or in_irq */ |
464 | 474 | ||
465 | /* Caller must hold fsg->lock */ | 475 | /* Caller must hold fsg->lock */ |
466 | static void wakeup_thread(struct fsg_dev *fsg) | 476 | static void wakeup_thread(struct fsg_common *common) |
467 | { | 477 | { |
468 | /* Tell the main thread that something has happened */ | 478 | /* Tell the main thread that something has happened */ |
469 | fsg->thread_wakeup_needed = 1; | 479 | common->thread_wakeup_needed = 1; |
470 | if (fsg->thread_task) | 480 | if (common->thread_task) |
471 | wake_up_process(fsg->thread_task); | 481 | wake_up_process(common->thread_task); |
472 | } | 482 | } |
473 | 483 | ||
474 | 484 | ||
475 | static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state) | 485 | static void raise_exception(struct fsg_common *common, enum fsg_state new_state) |
476 | { | 486 | { |
477 | unsigned long flags; | 487 | unsigned long flags; |
478 | 488 | ||
479 | /* Do nothing if a higher-priority exception is already in progress. | 489 | /* Do nothing if a higher-priority exception is already in progress. |
480 | * If a lower-or-equal priority exception is in progress, preempt it | 490 | * If a lower-or-equal priority exception is in progress, preempt it |
481 | * and notify the main thread by sending it a signal. */ | 491 | * and notify the main thread by sending it a signal. */ |
482 | spin_lock_irqsave(&fsg->lock, flags); | 492 | spin_lock_irqsave(&common->lock, flags); |
483 | if (fsg->state <= new_state) { | 493 | if (common->state <= new_state) { |
484 | fsg->exception_req_tag = fsg->ep0_req_tag; | 494 | common->exception_req_tag = common->ep0_req_tag; |
485 | fsg->state = new_state; | 495 | common->state = new_state; |
486 | if (fsg->thread_task) | 496 | if (common->thread_task) |
487 | send_sig_info(SIGUSR1, SEND_SIG_FORCED, | 497 | send_sig_info(SIGUSR1, SEND_SIG_FORCED, |
488 | fsg->thread_task); | 498 | common->thread_task); |
489 | } | 499 | } |
490 | spin_unlock_irqrestore(&fsg->lock, flags); | 500 | spin_unlock_irqrestore(&common->lock, flags); |
491 | } | 501 | } |
492 | 502 | ||
493 | 503 | ||
494 | /*-------------------------------------------------------------------------*/ | 504 | /*-------------------------------------------------------------------------*/ |
495 | 505 | ||
496 | static int ep0_queue(struct fsg_dev *fsg) | 506 | static int ep0_queue(struct fsg_common *common) |
497 | { | 507 | { |
498 | int rc; | 508 | int rc; |
499 | 509 | ||
500 | rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC); | 510 | rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC); |
501 | fsg->ep0->driver_data = fsg; | 511 | common->ep0->driver_data = common; |
502 | if (rc != 0 && rc != -ESHUTDOWN) { | 512 | if (rc != 0 && rc != -ESHUTDOWN) { |
503 | |||
504 | /* We can't do much more than wait for a reset */ | 513 | /* We can't do much more than wait for a reset */ |
505 | WARNING(fsg, "error in submission: %s --> %d\n", | 514 | WARNING(common, "error in submission: %s --> %d\n", |
506 | fsg->ep0->name, rc); | 515 | common->ep0->name, rc); |
507 | } | 516 | } |
508 | return rc; | 517 | return rc; |
509 | } | 518 | } |
@@ -515,32 +524,32 @@ static int ep0_queue(struct fsg_dev *fsg) | |||
515 | 524 | ||
516 | static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req) | 525 | static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req) |
517 | { | 526 | { |
518 | struct fsg_dev *fsg = ep->driver_data; | 527 | struct fsg_common *common = ep->driver_data; |
519 | struct fsg_buffhd *bh = req->context; | 528 | struct fsg_buffhd *bh = req->context; |
520 | 529 | ||
521 | if (req->status || req->actual != req->length) | 530 | if (req->status || req->actual != req->length) |
522 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, | 531 | DBG(common, "%s --> %d, %u/%u\n", __func__, |
523 | req->status, req->actual, req->length); | 532 | req->status, req->actual, req->length); |
524 | if (req->status == -ECONNRESET) /* Request was cancelled */ | 533 | if (req->status == -ECONNRESET) /* Request was cancelled */ |
525 | usb_ep_fifo_flush(ep); | 534 | usb_ep_fifo_flush(ep); |
526 | 535 | ||
527 | /* Hold the lock while we update the request and buffer states */ | 536 | /* Hold the lock while we update the request and buffer states */ |
528 | smp_wmb(); | 537 | smp_wmb(); |
529 | spin_lock(&fsg->lock); | 538 | spin_lock(&common->lock); |
530 | bh->inreq_busy = 0; | 539 | bh->inreq_busy = 0; |
531 | bh->state = BUF_STATE_EMPTY; | 540 | bh->state = BUF_STATE_EMPTY; |
532 | wakeup_thread(fsg); | 541 | wakeup_thread(common); |
533 | spin_unlock(&fsg->lock); | 542 | spin_unlock(&common->lock); |
534 | } | 543 | } |
535 | 544 | ||
536 | static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req) | 545 | static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req) |
537 | { | 546 | { |
538 | struct fsg_dev *fsg = ep->driver_data; | 547 | struct fsg_common *common = ep->driver_data; |
539 | struct fsg_buffhd *bh = req->context; | 548 | struct fsg_buffhd *bh = req->context; |
540 | 549 | ||
541 | dump_msg(fsg, "bulk-out", req->buf, req->actual); | 550 | dump_msg(common, "bulk-out", req->buf, req->actual); |
542 | if (req->status || req->actual != bh->bulk_out_intended_length) | 551 | if (req->status || req->actual != bh->bulk_out_intended_length) |
543 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, | 552 | DBG(common, "%s --> %d, %u/%u\n", __func__, |
544 | req->status, req->actual, | 553 | req->status, req->actual, |
545 | bh->bulk_out_intended_length); | 554 | bh->bulk_out_intended_length); |
546 | if (req->status == -ECONNRESET) /* Request was cancelled */ | 555 | if (req->status == -ECONNRESET) /* Request was cancelled */ |
@@ -548,11 +557,11 @@ static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req) | |||
548 | 557 | ||
549 | /* Hold the lock while we update the request and buffer states */ | 558 | /* Hold the lock while we update the request and buffer states */ |
550 | smp_wmb(); | 559 | smp_wmb(); |
551 | spin_lock(&fsg->lock); | 560 | spin_lock(&common->lock); |
552 | bh->outreq_busy = 0; | 561 | bh->outreq_busy = 0; |
553 | bh->state = BUF_STATE_FULL; | 562 | bh->state = BUF_STATE_FULL; |
554 | wakeup_thread(fsg); | 563 | wakeup_thread(common); |
555 | spin_unlock(&fsg->lock); | 564 | spin_unlock(&common->lock); |
556 | } | 565 | } |
557 | 566 | ||
558 | 567 | ||
@@ -564,12 +573,12 @@ static int fsg_setup(struct usb_function *f, | |||
564 | const struct usb_ctrlrequest *ctrl) | 573 | const struct usb_ctrlrequest *ctrl) |
565 | { | 574 | { |
566 | struct fsg_dev *fsg = fsg_from_func(f); | 575 | struct fsg_dev *fsg = fsg_from_func(f); |
567 | struct usb_request *req = fsg->ep0req; | 576 | struct usb_request *req = fsg->common->ep0req; |
568 | u16 w_index = le16_to_cpu(ctrl->wIndex); | 577 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
569 | u16 w_value = le16_to_cpu(ctrl->wValue); | 578 | u16 w_value = le16_to_cpu(ctrl->wValue); |
570 | u16 w_length = le16_to_cpu(ctrl->wLength); | 579 | u16 w_length = le16_to_cpu(ctrl->wLength); |
571 | 580 | ||
572 | if (!fsg->config) | 581 | if (!fsg->common->config) |
573 | return -EOPNOTSUPP; | 582 | return -EOPNOTSUPP; |
574 | 583 | ||
575 | switch (ctrl->bRequest) { | 584 | switch (ctrl->bRequest) { |
@@ -584,7 +593,7 @@ static int fsg_setup(struct usb_function *f, | |||
584 | /* Raise an exception to stop the current operation | 593 | /* Raise an exception to stop the current operation |
585 | * and reinitialize our state. */ | 594 | * and reinitialize our state. */ |
586 | DBG(fsg, "bulk reset request\n"); | 595 | DBG(fsg, "bulk reset request\n"); |
587 | raise_exception(fsg, FSG_STATE_RESET); | 596 | raise_exception(fsg->common, FSG_STATE_RESET); |
588 | return DELAYED_STATUS; | 597 | return DELAYED_STATUS; |
589 | 598 | ||
590 | case USB_BULK_GET_MAX_LUN_REQUEST: | 599 | case USB_BULK_GET_MAX_LUN_REQUEST: |
@@ -622,10 +631,10 @@ static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, | |||
622 | if (ep == fsg->bulk_in) | 631 | if (ep == fsg->bulk_in) |
623 | dump_msg(fsg, "bulk-in", req->buf, req->length); | 632 | dump_msg(fsg, "bulk-in", req->buf, req->length); |
624 | 633 | ||
625 | spin_lock_irq(&fsg->lock); | 634 | spin_lock_irq(&fsg->common->lock); |
626 | *pbusy = 1; | 635 | *pbusy = 1; |
627 | *state = BUF_STATE_BUSY; | 636 | *state = BUF_STATE_BUSY; |
628 | spin_unlock_irq(&fsg->lock); | 637 | spin_unlock_irq(&fsg->common->lock); |
629 | rc = usb_ep_queue(ep, req, GFP_KERNEL); | 638 | rc = usb_ep_queue(ep, req, GFP_KERNEL); |
630 | if (rc != 0) { | 639 | if (rc != 0) { |
631 | *pbusy = 0; | 640 | *pbusy = 0; |
@@ -642,8 +651,18 @@ static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, | |||
642 | } | 651 | } |
643 | } | 652 | } |
644 | 653 | ||
654 | #define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \ | ||
655 | if (fsg_is_set(common)) \ | ||
656 | start_transfer((common)->fsg, (common)->fsg->ep_name, \ | ||
657 | req, pbusy, state); \ | ||
658 | else | ||
659 | |||
660 | #define START_TRANSFER(common, ep_name, req, pbusy, state) \ | ||
661 | START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0 | ||
662 | |||
663 | |||
645 | 664 | ||
646 | static int sleep_thread(struct fsg_dev *fsg) | 665 | static int sleep_thread(struct fsg_common *common) |
647 | { | 666 | { |
648 | int rc = 0; | 667 | int rc = 0; |
649 | 668 | ||
@@ -655,21 +674,21 @@ static int sleep_thread(struct fsg_dev *fsg) | |||
655 | rc = -EINTR; | 674 | rc = -EINTR; |
656 | break; | 675 | break; |
657 | } | 676 | } |
658 | if (fsg->thread_wakeup_needed) | 677 | if (common->thread_wakeup_needed) |
659 | break; | 678 | break; |
660 | schedule(); | 679 | schedule(); |
661 | } | 680 | } |
662 | __set_current_state(TASK_RUNNING); | 681 | __set_current_state(TASK_RUNNING); |
663 | fsg->thread_wakeup_needed = 0; | 682 | common->thread_wakeup_needed = 0; |
664 | return rc; | 683 | return rc; |
665 | } | 684 | } |
666 | 685 | ||
667 | 686 | ||
668 | /*-------------------------------------------------------------------------*/ | 687 | /*-------------------------------------------------------------------------*/ |
669 | 688 | ||
670 | static int do_read(struct fsg_dev *fsg) | 689 | static int do_read(struct fsg_common *common) |
671 | { | 690 | { |
672 | struct fsg_lun *curlun = fsg->common->curlun; | 691 | struct fsg_lun *curlun = common->curlun; |
673 | u32 lba; | 692 | u32 lba; |
674 | struct fsg_buffhd *bh; | 693 | struct fsg_buffhd *bh; |
675 | int rc; | 694 | int rc; |
@@ -681,15 +700,15 @@ static int do_read(struct fsg_dev *fsg) | |||
681 | 700 | ||
682 | /* Get the starting Logical Block Address and check that it's | 701 | /* Get the starting Logical Block Address and check that it's |
683 | * not too big */ | 702 | * not too big */ |
684 | if (fsg->common->cmnd[0] == SC_READ_6) | 703 | if (common->cmnd[0] == SC_READ_6) |
685 | lba = get_unaligned_be24(&fsg->common->cmnd[1]); | 704 | lba = get_unaligned_be24(&common->cmnd[1]); |
686 | else { | 705 | else { |
687 | lba = get_unaligned_be32(&fsg->common->cmnd[2]); | 706 | lba = get_unaligned_be32(&common->cmnd[2]); |
688 | 707 | ||
689 | /* We allow DPO (Disable Page Out = don't save data in the | 708 | /* We allow DPO (Disable Page Out = don't save data in the |
690 | * cache) and FUA (Force Unit Access = don't read from the | 709 | * cache) and FUA (Force Unit Access = don't read from the |
691 | * cache), but we don't implement them. */ | 710 | * cache), but we don't implement them. */ |
692 | if ((fsg->common->cmnd[1] & ~0x18) != 0) { | 711 | if ((common->cmnd[1] & ~0x18) != 0) { |
693 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 712 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
694 | return -EINVAL; | 713 | return -EINVAL; |
695 | } | 714 | } |
@@ -701,7 +720,7 @@ static int do_read(struct fsg_dev *fsg) | |||
701 | file_offset = ((loff_t) lba) << 9; | 720 | file_offset = ((loff_t) lba) << 9; |
702 | 721 | ||
703 | /* Carry out the file reads */ | 722 | /* Carry out the file reads */ |
704 | amount_left = fsg->data_size_from_cmnd; | 723 | amount_left = common->data_size_from_cmnd; |
705 | if (unlikely(amount_left == 0)) | 724 | if (unlikely(amount_left == 0)) |
706 | return -EIO; /* No default reply */ | 725 | return -EIO; /* No default reply */ |
707 | 726 | ||
@@ -724,9 +743,9 @@ static int do_read(struct fsg_dev *fsg) | |||
724 | partial_page); | 743 | partial_page); |
725 | 744 | ||
726 | /* Wait for the next buffer to become available */ | 745 | /* Wait for the next buffer to become available */ |
727 | bh = fsg->common->next_buffhd_to_fill; | 746 | bh = common->next_buffhd_to_fill; |
728 | while (bh->state != BUF_STATE_EMPTY) { | 747 | while (bh->state != BUF_STATE_EMPTY) { |
729 | rc = sleep_thread(fsg); | 748 | rc = sleep_thread(common); |
730 | if (rc) | 749 | if (rc) |
731 | return rc; | 750 | return rc; |
732 | } | 751 | } |
@@ -765,7 +784,7 @@ static int do_read(struct fsg_dev *fsg) | |||
765 | } | 784 | } |
766 | file_offset += nread; | 785 | file_offset += nread; |
767 | amount_left -= nread; | 786 | amount_left -= nread; |
768 | fsg->residue -= nread; | 787 | common->residue -= nread; |
769 | bh->inreq->length = nread; | 788 | bh->inreq->length = nread; |
770 | bh->state = BUF_STATE_FULL; | 789 | bh->state = BUF_STATE_FULL; |
771 | 790 | ||
@@ -782,9 +801,12 @@ static int do_read(struct fsg_dev *fsg) | |||
782 | 801 | ||
783 | /* Send this buffer and go read some more */ | 802 | /* Send this buffer and go read some more */ |
784 | bh->inreq->zero = 0; | 803 | bh->inreq->zero = 0; |
785 | start_transfer(fsg, fsg->bulk_in, bh->inreq, | 804 | START_TRANSFER_OR(common, bulk_in, bh->inreq, |
786 | &bh->inreq_busy, &bh->state); | 805 | &bh->inreq_busy, &bh->state) |
787 | fsg->common->next_buffhd_to_fill = bh->next; | 806 | /* Don't know what to do if |
807 | * common->fsg is NULL */ | ||
808 | return -EIO; | ||
809 | common->next_buffhd_to_fill = bh->next; | ||
788 | } | 810 | } |
789 | 811 | ||
790 | return -EIO; /* No default reply */ | 812 | return -EIO; /* No default reply */ |
@@ -793,9 +815,9 @@ static int do_read(struct fsg_dev *fsg) | |||
793 | 815 | ||
794 | /*-------------------------------------------------------------------------*/ | 816 | /*-------------------------------------------------------------------------*/ |
795 | 817 | ||
796 | static int do_write(struct fsg_dev *fsg) | 818 | static int do_write(struct fsg_common *common) |
797 | { | 819 | { |
798 | struct fsg_lun *curlun = fsg->common->curlun; | 820 | struct fsg_lun *curlun = common->curlun; |
799 | u32 lba; | 821 | u32 lba; |
800 | struct fsg_buffhd *bh; | 822 | struct fsg_buffhd *bh; |
801 | int get_some_more; | 823 | int get_some_more; |
@@ -816,20 +838,20 @@ static int do_write(struct fsg_dev *fsg) | |||
816 | 838 | ||
817 | /* Get the starting Logical Block Address and check that it's | 839 | /* Get the starting Logical Block Address and check that it's |
818 | * not too big */ | 840 | * not too big */ |
819 | if (fsg->common->cmnd[0] == SC_WRITE_6) | 841 | if (common->cmnd[0] == SC_WRITE_6) |
820 | lba = get_unaligned_be24(&fsg->common->cmnd[1]); | 842 | lba = get_unaligned_be24(&common->cmnd[1]); |
821 | else { | 843 | else { |
822 | lba = get_unaligned_be32(&fsg->common->cmnd[2]); | 844 | lba = get_unaligned_be32(&common->cmnd[2]); |
823 | 845 | ||
824 | /* We allow DPO (Disable Page Out = don't save data in the | 846 | /* We allow DPO (Disable Page Out = don't save data in the |
825 | * cache) and FUA (Force Unit Access = write directly to the | 847 | * cache) and FUA (Force Unit Access = write directly to the |
826 | * medium). We don't implement DPO; we implement FUA by | 848 | * medium). We don't implement DPO; we implement FUA by |
827 | * performing synchronous output. */ | 849 | * performing synchronous output. */ |
828 | if ((fsg->common->cmnd[1] & ~0x18) != 0) { | 850 | if (common->cmnd[1] & ~0x18) { |
829 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 851 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
830 | return -EINVAL; | 852 | return -EINVAL; |
831 | } | 853 | } |
832 | if (fsg->common->cmnd[1] & 0x08) { /* FUA */ | 854 | if (common->cmnd[1] & 0x08) { /* FUA */ |
833 | spin_lock(&curlun->filp->f_lock); | 855 | spin_lock(&curlun->filp->f_lock); |
834 | curlun->filp->f_flags |= O_SYNC; | 856 | curlun->filp->f_flags |= O_SYNC; |
835 | spin_unlock(&curlun->filp->f_lock); | 857 | spin_unlock(&curlun->filp->f_lock); |
@@ -843,12 +865,13 @@ static int do_write(struct fsg_dev *fsg) | |||
843 | /* Carry out the file writes */ | 865 | /* Carry out the file writes */ |
844 | get_some_more = 1; | 866 | get_some_more = 1; |
845 | file_offset = usb_offset = ((loff_t) lba) << 9; | 867 | file_offset = usb_offset = ((loff_t) lba) << 9; |
846 | amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd; | 868 | amount_left_to_req = common->data_size_from_cmnd; |
869 | amount_left_to_write = common->data_size_from_cmnd; | ||
847 | 870 | ||
848 | while (amount_left_to_write > 0) { | 871 | while (amount_left_to_write > 0) { |
849 | 872 | ||
850 | /* Queue a request for more data from the host */ | 873 | /* Queue a request for more data from the host */ |
851 | bh = fsg->common->next_buffhd_to_fill; | 874 | bh = common->next_buffhd_to_fill; |
852 | if (bh->state == BUF_STATE_EMPTY && get_some_more) { | 875 | if (bh->state == BUF_STATE_EMPTY && get_some_more) { |
853 | 876 | ||
854 | /* Figure out how much we want to get: | 877 | /* Figure out how much we want to get: |
@@ -887,7 +910,7 @@ static int do_write(struct fsg_dev *fsg) | |||
887 | 910 | ||
888 | /* Get the next buffer */ | 911 | /* Get the next buffer */ |
889 | usb_offset += amount; | 912 | usb_offset += amount; |
890 | fsg->usb_amount_left -= amount; | 913 | common->usb_amount_left -= amount; |
891 | amount_left_to_req -= amount; | 914 | amount_left_to_req -= amount; |
892 | if (amount_left_to_req == 0) | 915 | if (amount_left_to_req == 0) |
893 | get_some_more = 0; | 916 | get_some_more = 0; |
@@ -897,19 +920,22 @@ static int do_write(struct fsg_dev *fsg) | |||
897 | bh->outreq->length = amount; | 920 | bh->outreq->length = amount; |
898 | bh->bulk_out_intended_length = amount; | 921 | bh->bulk_out_intended_length = amount; |
899 | bh->outreq->short_not_ok = 1; | 922 | bh->outreq->short_not_ok = 1; |
900 | start_transfer(fsg, fsg->bulk_out, bh->outreq, | 923 | START_TRANSFER_OR(common, bulk_out, bh->outreq, |
901 | &bh->outreq_busy, &bh->state); | 924 | &bh->outreq_busy, &bh->state) |
902 | fsg->common->next_buffhd_to_fill = bh->next; | 925 | /* Don't know what to do if |
926 | * common->fsg is NULL */ | ||
927 | return -EIO; | ||
928 | common->next_buffhd_to_fill = bh->next; | ||
903 | continue; | 929 | continue; |
904 | } | 930 | } |
905 | 931 | ||
906 | /* Write the received data to the backing file */ | 932 | /* Write the received data to the backing file */ |
907 | bh = fsg->common->next_buffhd_to_drain; | 933 | bh = common->next_buffhd_to_drain; |
908 | if (bh->state == BUF_STATE_EMPTY && !get_some_more) | 934 | if (bh->state == BUF_STATE_EMPTY && !get_some_more) |
909 | break; /* We stopped early */ | 935 | break; /* We stopped early */ |
910 | if (bh->state == BUF_STATE_FULL) { | 936 | if (bh->state == BUF_STATE_FULL) { |
911 | smp_rmb(); | 937 | smp_rmb(); |
912 | fsg->common->next_buffhd_to_drain = bh->next; | 938 | common->next_buffhd_to_drain = bh->next; |
913 | bh->state = BUF_STATE_EMPTY; | 939 | bh->state = BUF_STATE_EMPTY; |
914 | 940 | ||
915 | /* Did something go wrong with the transfer? */ | 941 | /* Did something go wrong with the transfer? */ |
@@ -952,7 +978,7 @@ static int do_write(struct fsg_dev *fsg) | |||
952 | } | 978 | } |
953 | file_offset += nwritten; | 979 | file_offset += nwritten; |
954 | amount_left_to_write -= nwritten; | 980 | amount_left_to_write -= nwritten; |
955 | fsg->residue -= nwritten; | 981 | common->residue -= nwritten; |
956 | 982 | ||
957 | /* If an error occurred, report it and its position */ | 983 | /* If an error occurred, report it and its position */ |
958 | if (nwritten < amount) { | 984 | if (nwritten < amount) { |
@@ -964,14 +990,14 @@ static int do_write(struct fsg_dev *fsg) | |||
964 | 990 | ||
965 | /* Did the host decide to stop early? */ | 991 | /* Did the host decide to stop early? */ |
966 | if (bh->outreq->actual != bh->outreq->length) { | 992 | if (bh->outreq->actual != bh->outreq->length) { |
967 | fsg->short_packet_received = 1; | 993 | common->short_packet_received = 1; |
968 | break; | 994 | break; |
969 | } | 995 | } |
970 | continue; | 996 | continue; |
971 | } | 997 | } |
972 | 998 | ||
973 | /* Wait for something to happen */ | 999 | /* Wait for something to happen */ |
974 | rc = sleep_thread(fsg); | 1000 | rc = sleep_thread(common); |
975 | if (rc) | 1001 | if (rc) |
976 | return rc; | 1002 | return rc; |
977 | } | 1003 | } |
@@ -982,9 +1008,9 @@ static int do_write(struct fsg_dev *fsg) | |||
982 | 1008 | ||
983 | /*-------------------------------------------------------------------------*/ | 1009 | /*-------------------------------------------------------------------------*/ |
984 | 1010 | ||
985 | static int do_synchronize_cache(struct fsg_dev *fsg) | 1011 | static int do_synchronize_cache(struct fsg_common *common) |
986 | { | 1012 | { |
987 | struct fsg_lun *curlun = fsg->common->curlun; | 1013 | struct fsg_lun *curlun = common->curlun; |
988 | int rc; | 1014 | int rc; |
989 | 1015 | ||
990 | /* We ignore the requested LBA and write out all file's | 1016 | /* We ignore the requested LBA and write out all file's |
@@ -1008,12 +1034,12 @@ static void invalidate_sub(struct fsg_lun *curlun) | |||
1008 | VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc); | 1034 | VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc); |
1009 | } | 1035 | } |
1010 | 1036 | ||
1011 | static int do_verify(struct fsg_dev *fsg) | 1037 | static int do_verify(struct fsg_common *common) |
1012 | { | 1038 | { |
1013 | struct fsg_lun *curlun = fsg->common->curlun; | 1039 | struct fsg_lun *curlun = common->curlun; |
1014 | u32 lba; | 1040 | u32 lba; |
1015 | u32 verification_length; | 1041 | u32 verification_length; |
1016 | struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; | 1042 | struct fsg_buffhd *bh = common->next_buffhd_to_fill; |
1017 | loff_t file_offset, file_offset_tmp; | 1043 | loff_t file_offset, file_offset_tmp; |
1018 | u32 amount_left; | 1044 | u32 amount_left; |
1019 | unsigned int amount; | 1045 | unsigned int amount; |
@@ -1021,7 +1047,7 @@ static int do_verify(struct fsg_dev *fsg) | |||
1021 | 1047 | ||
1022 | /* Get the starting Logical Block Address and check that it's | 1048 | /* Get the starting Logical Block Address and check that it's |
1023 | * not too big */ | 1049 | * not too big */ |
1024 | lba = get_unaligned_be32(&fsg->common->cmnd[2]); | 1050 | lba = get_unaligned_be32(&common->cmnd[2]); |
1025 | if (lba >= curlun->num_sectors) { | 1051 | if (lba >= curlun->num_sectors) { |
1026 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; | 1052 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
1027 | return -EINVAL; | 1053 | return -EINVAL; |
@@ -1029,12 +1055,12 @@ static int do_verify(struct fsg_dev *fsg) | |||
1029 | 1055 | ||
1030 | /* We allow DPO (Disable Page Out = don't save data in the | 1056 | /* We allow DPO (Disable Page Out = don't save data in the |
1031 | * cache) but we don't implement it. */ | 1057 | * cache) but we don't implement it. */ |
1032 | if ((fsg->common->cmnd[1] & ~0x10) != 0) { | 1058 | if (common->cmnd[1] & ~0x10) { |
1033 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1059 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1034 | return -EINVAL; | 1060 | return -EINVAL; |
1035 | } | 1061 | } |
1036 | 1062 | ||
1037 | verification_length = get_unaligned_be16(&fsg->common->cmnd[7]); | 1063 | verification_length = get_unaligned_be16(&common->cmnd[7]); |
1038 | if (unlikely(verification_length == 0)) | 1064 | if (unlikely(verification_length == 0)) |
1039 | return -EIO; /* No default reply */ | 1065 | return -EIO; /* No default reply */ |
1040 | 1066 | ||
@@ -1106,13 +1132,13 @@ static int do_verify(struct fsg_dev *fsg) | |||
1106 | 1132 | ||
1107 | /*-------------------------------------------------------------------------*/ | 1133 | /*-------------------------------------------------------------------------*/ |
1108 | 1134 | ||
1109 | static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1135 | static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh) |
1110 | { | 1136 | { |
1111 | struct fsg_lun *curlun = fsg->common->curlun; | 1137 | struct fsg_lun *curlun = common->curlun; |
1112 | u8 *buf = (u8 *) bh->buf; | 1138 | u8 *buf = (u8 *) bh->buf; |
1113 | 1139 | ||
1114 | if (!curlun) { /* Unsupported LUNs are okay */ | 1140 | if (!curlun) { /* Unsupported LUNs are okay */ |
1115 | fsg->bad_lun_okay = 1; | 1141 | common->bad_lun_okay = 1; |
1116 | memset(buf, 0, 36); | 1142 | memset(buf, 0, 36); |
1117 | buf[0] = 0x7f; /* Unsupported, no device-type */ | 1143 | buf[0] = 0x7f; /* Unsupported, no device-type */ |
1118 | buf[4] = 31; /* Additional length */ | 1144 | buf[4] = 31; /* Additional length */ |
@@ -1127,15 +1153,14 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1127 | buf[5] = 0; /* No special options */ | 1153 | buf[5] = 0; /* No special options */ |
1128 | buf[6] = 0; | 1154 | buf[6] = 0; |
1129 | buf[7] = 0; | 1155 | buf[7] = 0; |
1130 | memcpy(buf + 8, fsg->common->inquiry_string, | 1156 | memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string); |
1131 | sizeof fsg->common->inquiry_string); | ||
1132 | return 36; | 1157 | return 36; |
1133 | } | 1158 | } |
1134 | 1159 | ||
1135 | 1160 | ||
1136 | static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1161 | static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh) |
1137 | { | 1162 | { |
1138 | struct fsg_lun *curlun = fsg->common->curlun; | 1163 | struct fsg_lun *curlun = common->curlun; |
1139 | u8 *buf = (u8 *) bh->buf; | 1164 | u8 *buf = (u8 *) bh->buf; |
1140 | u32 sd, sdinfo; | 1165 | u32 sd, sdinfo; |
1141 | int valid; | 1166 | int valid; |
@@ -1163,7 +1188,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1163 | #endif | 1188 | #endif |
1164 | 1189 | ||
1165 | if (!curlun) { /* Unsupported LUNs are okay */ | 1190 | if (!curlun) { /* Unsupported LUNs are okay */ |
1166 | fsg->bad_lun_okay = 1; | 1191 | common->bad_lun_okay = 1; |
1167 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; | 1192 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; |
1168 | sdinfo = 0; | 1193 | sdinfo = 0; |
1169 | valid = 0; | 1194 | valid = 0; |
@@ -1187,11 +1212,11 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1187 | } | 1212 | } |
1188 | 1213 | ||
1189 | 1214 | ||
1190 | static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1215 | static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh) |
1191 | { | 1216 | { |
1192 | struct fsg_lun *curlun = fsg->common->curlun; | 1217 | struct fsg_lun *curlun = common->curlun; |
1193 | u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]); | 1218 | u32 lba = get_unaligned_be32(&common->cmnd[2]); |
1194 | int pmi = fsg->common->cmnd[8]; | 1219 | int pmi = common->cmnd[8]; |
1195 | u8 *buf = (u8 *) bh->buf; | 1220 | u8 *buf = (u8 *) bh->buf; |
1196 | 1221 | ||
1197 | /* Check the PMI and LBA fields */ | 1222 | /* Check the PMI and LBA fields */ |
@@ -1207,14 +1232,14 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1207 | } | 1232 | } |
1208 | 1233 | ||
1209 | 1234 | ||
1210 | static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1235 | static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh) |
1211 | { | 1236 | { |
1212 | struct fsg_lun *curlun = fsg->common->curlun; | 1237 | struct fsg_lun *curlun = common->curlun; |
1213 | int msf = fsg->common->cmnd[1] & 0x02; | 1238 | int msf = common->cmnd[1] & 0x02; |
1214 | u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]); | 1239 | u32 lba = get_unaligned_be32(&common->cmnd[2]); |
1215 | u8 *buf = (u8 *) bh->buf; | 1240 | u8 *buf = (u8 *) bh->buf; |
1216 | 1241 | ||
1217 | if ((fsg->common->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ | 1242 | if (common->cmnd[1] & ~0x02) { /* Mask away MSF */ |
1218 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1243 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1219 | return -EINVAL; | 1244 | return -EINVAL; |
1220 | } | 1245 | } |
@@ -1230,14 +1255,14 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1230 | } | 1255 | } |
1231 | 1256 | ||
1232 | 1257 | ||
1233 | static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1258 | static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) |
1234 | { | 1259 | { |
1235 | struct fsg_lun *curlun = fsg->common->curlun; | 1260 | struct fsg_lun *curlun = common->curlun; |
1236 | int msf = fsg->common->cmnd[1] & 0x02; | 1261 | int msf = common->cmnd[1] & 0x02; |
1237 | int start_track = fsg->common->cmnd[6]; | 1262 | int start_track = common->cmnd[6]; |
1238 | u8 *buf = (u8 *) bh->buf; | 1263 | u8 *buf = (u8 *) bh->buf; |
1239 | 1264 | ||
1240 | if ((fsg->common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ | 1265 | if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ |
1241 | start_track > 1) { | 1266 | start_track > 1) { |
1242 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1267 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1243 | return -EINVAL; | 1268 | return -EINVAL; |
@@ -1258,10 +1283,10 @@ static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1258 | } | 1283 | } |
1259 | 1284 | ||
1260 | 1285 | ||
1261 | static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1286 | static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh) |
1262 | { | 1287 | { |
1263 | struct fsg_lun *curlun = fsg->common->curlun; | 1288 | struct fsg_lun *curlun = common->curlun; |
1264 | int mscmnd = fsg->common->cmnd[0]; | 1289 | int mscmnd = common->cmnd[0]; |
1265 | u8 *buf = (u8 *) bh->buf; | 1290 | u8 *buf = (u8 *) bh->buf; |
1266 | u8 *buf0 = buf; | 1291 | u8 *buf0 = buf; |
1267 | int pc, page_code; | 1292 | int pc, page_code; |
@@ -1269,12 +1294,12 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1269 | int valid_page = 0; | 1294 | int valid_page = 0; |
1270 | int len, limit; | 1295 | int len, limit; |
1271 | 1296 | ||
1272 | if ((fsg->common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ | 1297 | if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */ |
1273 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1298 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1274 | return -EINVAL; | 1299 | return -EINVAL; |
1275 | } | 1300 | } |
1276 | pc = fsg->common->cmnd[2] >> 6; | 1301 | pc = common->cmnd[2] >> 6; |
1277 | page_code = fsg->common->cmnd[2] & 0x3f; | 1302 | page_code = common->cmnd[2] & 0x3f; |
1278 | if (pc == 3) { | 1303 | if (pc == 3) { |
1279 | curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED; | 1304 | curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED; |
1280 | return -EINVAL; | 1305 | return -EINVAL; |
@@ -1339,32 +1364,32 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
1339 | } | 1364 | } |
1340 | 1365 | ||
1341 | 1366 | ||
1342 | static int do_start_stop(struct fsg_dev *fsg) | 1367 | static int do_start_stop(struct fsg_common *common) |
1343 | { | 1368 | { |
1344 | if (!fsg->common->curlun) { | 1369 | if (!common->curlun) { |
1345 | return -EINVAL; | 1370 | return -EINVAL; |
1346 | } else if (!fsg->common->curlun->removable) { | 1371 | } else if (!common->curlun->removable) { |
1347 | fsg->common->curlun->sense_data = SS_INVALID_COMMAND; | 1372 | common->curlun->sense_data = SS_INVALID_COMMAND; |
1348 | return -EINVAL; | 1373 | return -EINVAL; |
1349 | } | 1374 | } |
1350 | return 0; | 1375 | return 0; |
1351 | } | 1376 | } |
1352 | 1377 | ||
1353 | 1378 | ||
1354 | static int do_prevent_allow(struct fsg_dev *fsg) | 1379 | static int do_prevent_allow(struct fsg_common *common) |
1355 | { | 1380 | { |
1356 | struct fsg_lun *curlun = fsg->common->curlun; | 1381 | struct fsg_lun *curlun = common->curlun; |
1357 | int prevent; | 1382 | int prevent; |
1358 | 1383 | ||
1359 | if (!fsg->common->curlun) { | 1384 | if (!common->curlun) { |
1360 | return -EINVAL; | 1385 | return -EINVAL; |
1361 | } else if (!fsg->common->curlun->removable) { | 1386 | } else if (!common->curlun->removable) { |
1362 | fsg->common->curlun->sense_data = SS_INVALID_COMMAND; | 1387 | common->curlun->sense_data = SS_INVALID_COMMAND; |
1363 | return -EINVAL; | 1388 | return -EINVAL; |
1364 | } | 1389 | } |
1365 | 1390 | ||
1366 | prevent = fsg->common->cmnd[4] & 0x01; | 1391 | prevent = common->cmnd[4] & 0x01; |
1367 | if ((fsg->common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ | 1392 | if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */ |
1368 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1393 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1369 | return -EINVAL; | 1394 | return -EINVAL; |
1370 | } | 1395 | } |
@@ -1376,10 +1401,10 @@ static int do_prevent_allow(struct fsg_dev *fsg) | |||
1376 | } | 1401 | } |
1377 | 1402 | ||
1378 | 1403 | ||
1379 | static int do_read_format_capacities(struct fsg_dev *fsg, | 1404 | static int do_read_format_capacities(struct fsg_common *common, |
1380 | struct fsg_buffhd *bh) | 1405 | struct fsg_buffhd *bh) |
1381 | { | 1406 | { |
1382 | struct fsg_lun *curlun = fsg->common->curlun; | 1407 | struct fsg_lun *curlun = common->curlun; |
1383 | u8 *buf = (u8 *) bh->buf; | 1408 | u8 *buf = (u8 *) bh->buf; |
1384 | 1409 | ||
1385 | buf[0] = buf[1] = buf[2] = 0; | 1410 | buf[0] = buf[1] = buf[2] = 0; |
@@ -1394,12 +1419,13 @@ static int do_read_format_capacities(struct fsg_dev *fsg, | |||
1394 | } | 1419 | } |
1395 | 1420 | ||
1396 | 1421 | ||
1397 | static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 1422 | static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh) |
1398 | { | 1423 | { |
1399 | struct fsg_lun *curlun = fsg->common->curlun; | 1424 | struct fsg_lun *curlun = common->curlun; |
1400 | 1425 | ||
1401 | /* We don't support MODE SELECT */ | 1426 | /* We don't support MODE SELECT */ |
1402 | curlun->sense_data = SS_INVALID_COMMAND; | 1427 | if (curlun) |
1428 | curlun->sense_data = SS_INVALID_COMMAND; | ||
1403 | return -EINVAL; | 1429 | return -EINVAL; |
1404 | } | 1430 | } |
1405 | 1431 | ||
@@ -1459,73 +1485,78 @@ static int pad_with_zeros(struct fsg_dev *fsg) | |||
1459 | int rc; | 1485 | int rc; |
1460 | 1486 | ||
1461 | bh->state = BUF_STATE_EMPTY; /* For the first iteration */ | 1487 | bh->state = BUF_STATE_EMPTY; /* For the first iteration */ |
1462 | fsg->usb_amount_left = nkeep + fsg->residue; | 1488 | fsg->common->usb_amount_left = nkeep + fsg->common->residue; |
1463 | while (fsg->usb_amount_left > 0) { | 1489 | while (fsg->common->usb_amount_left > 0) { |
1464 | 1490 | ||
1465 | /* Wait for the next buffer to be free */ | 1491 | /* Wait for the next buffer to be free */ |
1466 | while (bh->state != BUF_STATE_EMPTY) { | 1492 | while (bh->state != BUF_STATE_EMPTY) { |
1467 | rc = sleep_thread(fsg); | 1493 | rc = sleep_thread(fsg->common); |
1468 | if (rc) | 1494 | if (rc) |
1469 | return rc; | 1495 | return rc; |
1470 | } | 1496 | } |
1471 | 1497 | ||
1472 | nsend = min(fsg->usb_amount_left, FSG_BUFLEN); | 1498 | nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN); |
1473 | memset(bh->buf + nkeep, 0, nsend - nkeep); | 1499 | memset(bh->buf + nkeep, 0, nsend - nkeep); |
1474 | bh->inreq->length = nsend; | 1500 | bh->inreq->length = nsend; |
1475 | bh->inreq->zero = 0; | 1501 | bh->inreq->zero = 0; |
1476 | start_transfer(fsg, fsg->bulk_in, bh->inreq, | 1502 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
1477 | &bh->inreq_busy, &bh->state); | 1503 | &bh->inreq_busy, &bh->state); |
1478 | bh = fsg->common->next_buffhd_to_fill = bh->next; | 1504 | bh = fsg->common->next_buffhd_to_fill = bh->next; |
1479 | fsg->usb_amount_left -= nsend; | 1505 | fsg->common->usb_amount_left -= nsend; |
1480 | nkeep = 0; | 1506 | nkeep = 0; |
1481 | } | 1507 | } |
1482 | return 0; | 1508 | return 0; |
1483 | } | 1509 | } |
1484 | 1510 | ||
1485 | static int throw_away_data(struct fsg_dev *fsg) | 1511 | static int throw_away_data(struct fsg_common *common) |
1486 | { | 1512 | { |
1487 | struct fsg_buffhd *bh; | 1513 | struct fsg_buffhd *bh; |
1488 | u32 amount; | 1514 | u32 amount; |
1489 | int rc; | 1515 | int rc; |
1490 | 1516 | ||
1491 | for (bh = fsg->common->next_buffhd_to_drain; | 1517 | for (bh = common->next_buffhd_to_drain; |
1492 | bh->state != BUF_STATE_EMPTY || fsg->usb_amount_left > 0; | 1518 | bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; |
1493 | bh = fsg->common->next_buffhd_to_drain) { | 1519 | bh = common->next_buffhd_to_drain) { |
1494 | 1520 | ||
1495 | /* Throw away the data in a filled buffer */ | 1521 | /* Throw away the data in a filled buffer */ |
1496 | if (bh->state == BUF_STATE_FULL) { | 1522 | if (bh->state == BUF_STATE_FULL) { |
1497 | smp_rmb(); | 1523 | smp_rmb(); |
1498 | bh->state = BUF_STATE_EMPTY; | 1524 | bh->state = BUF_STATE_EMPTY; |
1499 | fsg->common->next_buffhd_to_drain = bh->next; | 1525 | common->next_buffhd_to_drain = bh->next; |
1500 | 1526 | ||
1501 | /* A short packet or an error ends everything */ | 1527 | /* A short packet or an error ends everything */ |
1502 | if (bh->outreq->actual != bh->outreq->length || | 1528 | if (bh->outreq->actual != bh->outreq->length || |
1503 | bh->outreq->status != 0) { | 1529 | bh->outreq->status != 0) { |
1504 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); | 1530 | raise_exception(common, |
1531 | FSG_STATE_ABORT_BULK_OUT); | ||
1505 | return -EINTR; | 1532 | return -EINTR; |
1506 | } | 1533 | } |
1507 | continue; | 1534 | continue; |
1508 | } | 1535 | } |
1509 | 1536 | ||
1510 | /* Try to submit another request if we need one */ | 1537 | /* Try to submit another request if we need one */ |
1511 | bh = fsg->common->next_buffhd_to_fill; | 1538 | bh = common->next_buffhd_to_fill; |
1512 | if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) { | 1539 | if (bh->state == BUF_STATE_EMPTY |
1513 | amount = min(fsg->usb_amount_left, FSG_BUFLEN); | 1540 | && common->usb_amount_left > 0) { |
1541 | amount = min(common->usb_amount_left, FSG_BUFLEN); | ||
1514 | 1542 | ||
1515 | /* amount is always divisible by 512, hence by | 1543 | /* amount is always divisible by 512, hence by |
1516 | * the bulk-out maxpacket size */ | 1544 | * the bulk-out maxpacket size */ |
1517 | bh->outreq->length = amount; | 1545 | bh->outreq->length = amount; |
1518 | bh->bulk_out_intended_length = amount; | 1546 | bh->bulk_out_intended_length = amount; |
1519 | bh->outreq->short_not_ok = 1; | 1547 | bh->outreq->short_not_ok = 1; |
1520 | start_transfer(fsg, fsg->bulk_out, bh->outreq, | 1548 | START_TRANSFER_OR(common, bulk_out, bh->outreq, |
1521 | &bh->outreq_busy, &bh->state); | 1549 | &bh->outreq_busy, &bh->state) |
1522 | fsg->common->next_buffhd_to_fill = bh->next; | 1550 | /* Don't know what to do if |
1523 | fsg->usb_amount_left -= amount; | 1551 | * common->fsg is NULL */ |
1552 | return -EIO; | ||
1553 | common->next_buffhd_to_fill = bh->next; | ||
1554 | common->usb_amount_left -= amount; | ||
1524 | continue; | 1555 | continue; |
1525 | } | 1556 | } |
1526 | 1557 | ||
1527 | /* Otherwise wait for something to happen */ | 1558 | /* Otherwise wait for something to happen */ |
1528 | rc = sleep_thread(fsg); | 1559 | rc = sleep_thread(common); |
1529 | if (rc) | 1560 | if (rc) |
1530 | return rc; | 1561 | return rc; |
1531 | } | 1562 | } |
@@ -1533,12 +1564,12 @@ static int throw_away_data(struct fsg_dev *fsg) | |||
1533 | } | 1564 | } |
1534 | 1565 | ||
1535 | 1566 | ||
1536 | static int finish_reply(struct fsg_dev *fsg) | 1567 | static int finish_reply(struct fsg_common *common) |
1537 | { | 1568 | { |
1538 | struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill; | 1569 | struct fsg_buffhd *bh = common->next_buffhd_to_fill; |
1539 | int rc = 0; | 1570 | int rc = 0; |
1540 | 1571 | ||
1541 | switch (fsg->data_dir) { | 1572 | switch (common->data_dir) { |
1542 | case DATA_DIR_NONE: | 1573 | case DATA_DIR_NONE: |
1543 | break; /* Nothing to send */ | 1574 | break; /* Nothing to send */ |
1544 | 1575 | ||
@@ -1547,47 +1578,60 @@ static int finish_reply(struct fsg_dev *fsg) | |||
1547 | * try to send or receive any data. So stall both bulk pipes | 1578 | * try to send or receive any data. So stall both bulk pipes |
1548 | * if we can and wait for a reset. */ | 1579 | * if we can and wait for a reset. */ |
1549 | case DATA_DIR_UNKNOWN: | 1580 | case DATA_DIR_UNKNOWN: |
1550 | if (fsg->can_stall) { | 1581 | if (!common->can_stall) { |
1551 | fsg_set_halt(fsg, fsg->bulk_out); | 1582 | /* Nothing */ |
1552 | rc = halt_bulk_in_endpoint(fsg); | 1583 | } else if (fsg_is_set(common)) { |
1584 | fsg_set_halt(common->fsg, common->fsg->bulk_out); | ||
1585 | rc = halt_bulk_in_endpoint(common->fsg); | ||
1586 | } else { | ||
1587 | /* Don't know what to do if common->fsg is NULL */ | ||
1588 | rc = -EIO; | ||
1553 | } | 1589 | } |
1554 | break; | 1590 | break; |
1555 | 1591 | ||
1556 | /* All but the last buffer of data must have already been sent */ | 1592 | /* All but the last buffer of data must have already been sent */ |
1557 | case DATA_DIR_TO_HOST: | 1593 | case DATA_DIR_TO_HOST: |
1558 | if (fsg->data_size == 0) { | 1594 | if (common->data_size == 0) { |
1559 | /* Nothing to send */ | 1595 | /* Nothing to send */ |
1560 | 1596 | ||
1561 | /* If there's no residue, simply send the last buffer */ | 1597 | /* If there's no residue, simply send the last buffer */ |
1562 | } else if (fsg->residue == 0) { | 1598 | } else if (common->residue == 0) { |
1563 | bh->inreq->zero = 0; | 1599 | bh->inreq->zero = 0; |
1564 | start_transfer(fsg, fsg->bulk_in, bh->inreq, | 1600 | START_TRANSFER_OR(common, bulk_in, bh->inreq, |
1565 | &bh->inreq_busy, &bh->state); | 1601 | &bh->inreq_busy, &bh->state) |
1566 | fsg->common->next_buffhd_to_fill = bh->next; | 1602 | return -EIO; |
1603 | common->next_buffhd_to_fill = bh->next; | ||
1567 | 1604 | ||
1568 | /* For Bulk-only, if we're allowed to stall then send the | 1605 | /* For Bulk-only, if we're allowed to stall then send the |
1569 | * short packet and halt the bulk-in endpoint. If we can't | 1606 | * short packet and halt the bulk-in endpoint. If we can't |
1570 | * stall, pad out the remaining data with 0's. */ | 1607 | * stall, pad out the remaining data with 0's. */ |
1571 | } else if (fsg->can_stall) { | 1608 | } else if (common->can_stall) { |
1572 | bh->inreq->zero = 1; | 1609 | bh->inreq->zero = 1; |
1573 | start_transfer(fsg, fsg->bulk_in, bh->inreq, | 1610 | START_TRANSFER_OR(common, bulk_in, bh->inreq, |
1574 | &bh->inreq_busy, &bh->state); | 1611 | &bh->inreq_busy, &bh->state) |
1575 | fsg->common->next_buffhd_to_fill = bh->next; | 1612 | /* Don't know what to do if |
1576 | rc = halt_bulk_in_endpoint(fsg); | 1613 | * common->fsg is NULL */ |
1614 | rc = -EIO; | ||
1615 | common->next_buffhd_to_fill = bh->next; | ||
1616 | if (common->fsg) | ||
1617 | rc = halt_bulk_in_endpoint(common->fsg); | ||
1618 | } else if (fsg_is_set(common)) { | ||
1619 | rc = pad_with_zeros(common->fsg); | ||
1577 | } else { | 1620 | } else { |
1578 | rc = pad_with_zeros(fsg); | 1621 | /* Don't know what to do if common->fsg is NULL */ |
1622 | rc = -EIO; | ||
1579 | } | 1623 | } |
1580 | break; | 1624 | break; |
1581 | 1625 | ||
1582 | /* We have processed all we want from the data the host has sent. | 1626 | /* We have processed all we want from the data the host has sent. |
1583 | * There may still be outstanding bulk-out requests. */ | 1627 | * There may still be outstanding bulk-out requests. */ |
1584 | case DATA_DIR_FROM_HOST: | 1628 | case DATA_DIR_FROM_HOST: |
1585 | if (fsg->residue == 0) { | 1629 | if (common->residue == 0) { |
1586 | /* Nothing to receive */ | 1630 | /* Nothing to receive */ |
1587 | 1631 | ||
1588 | /* Did the host stop sending unexpectedly early? */ | 1632 | /* Did the host stop sending unexpectedly early? */ |
1589 | } else if (fsg->short_packet_received) { | 1633 | } else if (common->short_packet_received) { |
1590 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); | 1634 | raise_exception(common, FSG_STATE_ABORT_BULK_OUT); |
1591 | rc = -EINTR; | 1635 | rc = -EINTR; |
1592 | 1636 | ||
1593 | /* We haven't processed all the incoming data. Even though | 1637 | /* We haven't processed all the incoming data. Even though |
@@ -1597,16 +1641,18 @@ static int finish_reply(struct fsg_dev *fsg) | |||
1597 | * STALL. Not realizing the endpoint was halted, it wouldn't | 1641 | * STALL. Not realizing the endpoint was halted, it wouldn't |
1598 | * clear the halt -- leading to problems later on. */ | 1642 | * clear the halt -- leading to problems later on. */ |
1599 | #if 0 | 1643 | #if 0 |
1600 | } else if (fsg->can_stall) { | 1644 | } else if (common->can_stall) { |
1601 | fsg_set_halt(fsg, fsg->bulk_out); | 1645 | if (fsg_is_set(common)) |
1602 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); | 1646 | fsg_set_halt(common->fsg, |
1647 | common->fsg->bulk_out); | ||
1648 | raise_exception(common, FSG_STATE_ABORT_BULK_OUT); | ||
1603 | rc = -EINTR; | 1649 | rc = -EINTR; |
1604 | #endif | 1650 | #endif |
1605 | 1651 | ||
1606 | /* We can't stall. Read in the excess data and throw it | 1652 | /* We can't stall. Read in the excess data and throw it |
1607 | * all away. */ | 1653 | * all away. */ |
1608 | } else { | 1654 | } else { |
1609 | rc = throw_away_data(fsg); | 1655 | rc = throw_away_data(common); |
1610 | } | 1656 | } |
1611 | break; | 1657 | break; |
1612 | } | 1658 | } |
@@ -1614,9 +1660,9 @@ static int finish_reply(struct fsg_dev *fsg) | |||
1614 | } | 1660 | } |
1615 | 1661 | ||
1616 | 1662 | ||
1617 | static int send_status(struct fsg_dev *fsg) | 1663 | static int send_status(struct fsg_common *common) |
1618 | { | 1664 | { |
1619 | struct fsg_lun *curlun = fsg->common->curlun; | 1665 | struct fsg_lun *curlun = common->curlun; |
1620 | struct fsg_buffhd *bh; | 1666 | struct fsg_buffhd *bh; |
1621 | struct bulk_cs_wrap *csw; | 1667 | struct bulk_cs_wrap *csw; |
1622 | int rc; | 1668 | int rc; |
@@ -1624,9 +1670,9 @@ static int send_status(struct fsg_dev *fsg) | |||
1624 | u32 sd, sdinfo = 0; | 1670 | u32 sd, sdinfo = 0; |
1625 | 1671 | ||
1626 | /* Wait for the next buffer to become available */ | 1672 | /* Wait for the next buffer to become available */ |
1627 | bh = fsg->common->next_buffhd_to_fill; | 1673 | bh = common->next_buffhd_to_fill; |
1628 | while (bh->state != BUF_STATE_EMPTY) { | 1674 | while (bh->state != BUF_STATE_EMPTY) { |
1629 | rc = sleep_thread(fsg); | 1675 | rc = sleep_thread(common); |
1630 | if (rc) | 1676 | if (rc) |
1631 | return rc; | 1677 | return rc; |
1632 | } | 1678 | } |
@@ -1634,19 +1680,19 @@ static int send_status(struct fsg_dev *fsg) | |||
1634 | if (curlun) { | 1680 | if (curlun) { |
1635 | sd = curlun->sense_data; | 1681 | sd = curlun->sense_data; |
1636 | sdinfo = curlun->sense_data_info; | 1682 | sdinfo = curlun->sense_data_info; |
1637 | } else if (fsg->bad_lun_okay) | 1683 | } else if (common->bad_lun_okay) |
1638 | sd = SS_NO_SENSE; | 1684 | sd = SS_NO_SENSE; |
1639 | else | 1685 | else |
1640 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; | 1686 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; |
1641 | 1687 | ||
1642 | if (fsg->phase_error) { | 1688 | if (common->phase_error) { |
1643 | DBG(fsg, "sending phase-error status\n"); | 1689 | DBG(common, "sending phase-error status\n"); |
1644 | status = USB_STATUS_PHASE_ERROR; | 1690 | status = USB_STATUS_PHASE_ERROR; |
1645 | sd = SS_INVALID_COMMAND; | 1691 | sd = SS_INVALID_COMMAND; |
1646 | } else if (sd != SS_NO_SENSE) { | 1692 | } else if (sd != SS_NO_SENSE) { |
1647 | DBG(fsg, "sending command-failure status\n"); | 1693 | DBG(common, "sending command-failure status\n"); |
1648 | status = USB_STATUS_FAIL; | 1694 | status = USB_STATUS_FAIL; |
1649 | VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" | 1695 | VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" |
1650 | " info x%x\n", | 1696 | " info x%x\n", |
1651 | SK(sd), ASC(sd), ASCQ(sd), sdinfo); | 1697 | SK(sd), ASC(sd), ASCQ(sd), sdinfo); |
1652 | } | 1698 | } |
@@ -1655,16 +1701,18 @@ static int send_status(struct fsg_dev *fsg) | |||
1655 | csw = (void *)bh->buf; | 1701 | csw = (void *)bh->buf; |
1656 | 1702 | ||
1657 | csw->Signature = cpu_to_le32(USB_BULK_CS_SIG); | 1703 | csw->Signature = cpu_to_le32(USB_BULK_CS_SIG); |
1658 | csw->Tag = fsg->tag; | 1704 | csw->Tag = common->tag; |
1659 | csw->Residue = cpu_to_le32(fsg->residue); | 1705 | csw->Residue = cpu_to_le32(common->residue); |
1660 | csw->Status = status; | 1706 | csw->Status = status; |
1661 | 1707 | ||
1662 | bh->inreq->length = USB_BULK_CS_WRAP_LEN; | 1708 | bh->inreq->length = USB_BULK_CS_WRAP_LEN; |
1663 | bh->inreq->zero = 0; | 1709 | bh->inreq->zero = 0; |
1664 | start_transfer(fsg, fsg->bulk_in, bh->inreq, | 1710 | START_TRANSFER_OR(common, bulk_in, bh->inreq, |
1665 | &bh->inreq_busy, &bh->state); | 1711 | &bh->inreq_busy, &bh->state) |
1712 | /* Don't know what to do if common->fsg is NULL */ | ||
1713 | return -EIO; | ||
1666 | 1714 | ||
1667 | fsg->common->next_buffhd_to_fill = bh->next; | 1715 | common->next_buffhd_to_fill = bh->next; |
1668 | return 0; | 1716 | return 0; |
1669 | } | 1717 | } |
1670 | 1718 | ||
@@ -1673,52 +1721,47 @@ static int send_status(struct fsg_dev *fsg) | |||
1673 | 1721 | ||
1674 | /* Check whether the command is properly formed and whether its data size | 1722 | /* Check whether the command is properly formed and whether its data size |
1675 | * and direction agree with the values we already have. */ | 1723 | * and direction agree with the values we already have. */ |
1676 | static int check_command(struct fsg_dev *fsg, int cmnd_size, | 1724 | static int check_command(struct fsg_common *common, int cmnd_size, |
1677 | enum data_direction data_dir, unsigned int mask, | 1725 | enum data_direction data_dir, unsigned int mask, |
1678 | int needs_medium, const char *name) | 1726 | int needs_medium, const char *name) |
1679 | { | 1727 | { |
1680 | int i; | 1728 | int i; |
1681 | int lun = fsg->common->cmnd[1] >> 5; | 1729 | int lun = common->cmnd[1] >> 5; |
1682 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; | 1730 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; |
1683 | char hdlen[20]; | 1731 | char hdlen[20]; |
1684 | struct fsg_lun *curlun; | 1732 | struct fsg_lun *curlun; |
1685 | 1733 | ||
1686 | hdlen[0] = 0; | 1734 | hdlen[0] = 0; |
1687 | if (fsg->data_dir != DATA_DIR_UNKNOWN) | 1735 | if (common->data_dir != DATA_DIR_UNKNOWN) |
1688 | sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir], | 1736 | sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], |
1689 | fsg->data_size); | 1737 | common->data_size); |
1690 | VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", | 1738 | VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", |
1691 | name, cmnd_size, dirletter[(int) data_dir], | 1739 | name, cmnd_size, dirletter[(int) data_dir], |
1692 | fsg->data_size_from_cmnd, fsg->common->cmnd_size, hdlen); | 1740 | common->data_size_from_cmnd, common->cmnd_size, hdlen); |
1693 | 1741 | ||
1694 | /* We can't reply at all until we know the correct data direction | 1742 | /* We can't reply at all until we know the correct data direction |
1695 | * and size. */ | 1743 | * and size. */ |
1696 | if (fsg->data_size_from_cmnd == 0) | 1744 | if (common->data_size_from_cmnd == 0) |
1697 | data_dir = DATA_DIR_NONE; | 1745 | data_dir = DATA_DIR_NONE; |
1698 | if (fsg->data_dir == DATA_DIR_UNKNOWN) { /* CB or CBI */ | 1746 | if (common->data_size < common->data_size_from_cmnd) { |
1699 | fsg->data_dir = data_dir; | 1747 | /* Host data size < Device data size is a phase error. |
1700 | fsg->data_size = fsg->data_size_from_cmnd; | 1748 | * Carry out the command, but only transfer as much as |
1701 | 1749 | * we are allowed. */ | |
1702 | } else { /* Bulk-only */ | 1750 | common->data_size_from_cmnd = common->data_size; |
1703 | if (fsg->data_size < fsg->data_size_from_cmnd) { | 1751 | common->phase_error = 1; |
1704 | |||
1705 | /* Host data size < Device data size is a phase error. | ||
1706 | * Carry out the command, but only transfer as much | ||
1707 | * as we are allowed. */ | ||
1708 | fsg->data_size_from_cmnd = fsg->data_size; | ||
1709 | fsg->phase_error = 1; | ||
1710 | } | ||
1711 | } | 1752 | } |
1712 | fsg->residue = fsg->usb_amount_left = fsg->data_size; | 1753 | common->residue = common->data_size; |
1754 | common->usb_amount_left = common->data_size; | ||
1713 | 1755 | ||
1714 | /* Conflicting data directions is a phase error */ | 1756 | /* Conflicting data directions is a phase error */ |
1715 | if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) { | 1757 | if (common->data_dir != data_dir |
1716 | fsg->phase_error = 1; | 1758 | && common->data_size_from_cmnd > 0) { |
1759 | common->phase_error = 1; | ||
1717 | return -EINVAL; | 1760 | return -EINVAL; |
1718 | } | 1761 | } |
1719 | 1762 | ||
1720 | /* Verify the length of the command itself */ | 1763 | /* Verify the length of the command itself */ |
1721 | if (cmnd_size != fsg->common->cmnd_size) { | 1764 | if (cmnd_size != common->cmnd_size) { |
1722 | 1765 | ||
1723 | /* Special case workaround: There are plenty of buggy SCSI | 1766 | /* Special case workaround: There are plenty of buggy SCSI |
1724 | * implementations. Many have issues with cbw->Length | 1767 | * implementations. Many have issues with cbw->Length |
@@ -1732,40 +1775,41 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, | |||
1732 | * REQUEST SENSE with cbw->Length == 10 where it should | 1775 | * REQUEST SENSE with cbw->Length == 10 where it should |
1733 | * be 6 as well. | 1776 | * be 6 as well. |
1734 | */ | 1777 | */ |
1735 | if (cmnd_size <= fsg->common->cmnd_size) { | 1778 | if (cmnd_size <= common->cmnd_size) { |
1736 | DBG(fsg, "%s is buggy! Expected length %d " | 1779 | DBG(common, "%s is buggy! Expected length %d " |
1737 | "but we got %d\n", name, | 1780 | "but we got %d\n", name, |
1738 | cmnd_size, fsg->common->cmnd_size); | 1781 | cmnd_size, common->cmnd_size); |
1739 | cmnd_size = fsg->common->cmnd_size; | 1782 | cmnd_size = common->cmnd_size; |
1740 | } else { | 1783 | } else { |
1741 | fsg->phase_error = 1; | 1784 | common->phase_error = 1; |
1742 | return -EINVAL; | 1785 | return -EINVAL; |
1743 | } | 1786 | } |
1744 | } | 1787 | } |
1745 | 1788 | ||
1746 | /* Check that the LUN values are consistent */ | 1789 | /* Check that the LUN values are consistent */ |
1747 | if (fsg->common->lun != lun) | 1790 | if (common->lun != lun) |
1748 | DBG(fsg, "using LUN %d from CBW, not LUN %d from CDB\n", | 1791 | DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n", |
1749 | fsg->common->lun, lun); | 1792 | common->lun, lun); |
1750 | 1793 | ||
1751 | /* Check the LUN */ | 1794 | /* Check the LUN */ |
1752 | if (fsg->common->lun >= 0 && fsg->common->lun < fsg->common->nluns) { | 1795 | if (common->lun >= 0 && common->lun < common->nluns) { |
1753 | curlun = &fsg->common->luns[fsg->common->lun]; | 1796 | curlun = &common->luns[common->lun]; |
1754 | fsg->common->curlun = curlun; | 1797 | common->curlun = curlun; |
1755 | if (fsg->common->cmnd[0] != SC_REQUEST_SENSE) { | 1798 | if (common->cmnd[0] != SC_REQUEST_SENSE) { |
1756 | curlun->sense_data = SS_NO_SENSE; | 1799 | curlun->sense_data = SS_NO_SENSE; |
1757 | curlun->sense_data_info = 0; | 1800 | curlun->sense_data_info = 0; |
1758 | curlun->info_valid = 0; | 1801 | curlun->info_valid = 0; |
1759 | } | 1802 | } |
1760 | } else { | 1803 | } else { |
1761 | fsg->common->curlun = curlun = NULL; | 1804 | common->curlun = NULL; |
1762 | fsg->bad_lun_okay = 0; | 1805 | curlun = NULL; |
1806 | common->bad_lun_okay = 0; | ||
1763 | 1807 | ||
1764 | /* INQUIRY and REQUEST SENSE commands are explicitly allowed | 1808 | /* INQUIRY and REQUEST SENSE commands are explicitly allowed |
1765 | * to use unsupported LUNs; all others may not. */ | 1809 | * to use unsupported LUNs; all others may not. */ |
1766 | if (fsg->common->cmnd[0] != SC_INQUIRY && | 1810 | if (common->cmnd[0] != SC_INQUIRY && |
1767 | fsg->common->cmnd[0] != SC_REQUEST_SENSE) { | 1811 | common->cmnd[0] != SC_REQUEST_SENSE) { |
1768 | DBG(fsg, "unsupported LUN %d\n", fsg->common->lun); | 1812 | DBG(common, "unsupported LUN %d\n", common->lun); |
1769 | return -EINVAL; | 1813 | return -EINVAL; |
1770 | } | 1814 | } |
1771 | } | 1815 | } |
@@ -1773,17 +1817,17 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, | |||
1773 | /* If a unit attention condition exists, only INQUIRY and | 1817 | /* If a unit attention condition exists, only INQUIRY and |
1774 | * REQUEST SENSE commands are allowed; anything else must fail. */ | 1818 | * REQUEST SENSE commands are allowed; anything else must fail. */ |
1775 | if (curlun && curlun->unit_attention_data != SS_NO_SENSE && | 1819 | if (curlun && curlun->unit_attention_data != SS_NO_SENSE && |
1776 | fsg->common->cmnd[0] != SC_INQUIRY && | 1820 | common->cmnd[0] != SC_INQUIRY && |
1777 | fsg->common->cmnd[0] != SC_REQUEST_SENSE) { | 1821 | common->cmnd[0] != SC_REQUEST_SENSE) { |
1778 | curlun->sense_data = curlun->unit_attention_data; | 1822 | curlun->sense_data = curlun->unit_attention_data; |
1779 | curlun->unit_attention_data = SS_NO_SENSE; | 1823 | curlun->unit_attention_data = SS_NO_SENSE; |
1780 | return -EINVAL; | 1824 | return -EINVAL; |
1781 | } | 1825 | } |
1782 | 1826 | ||
1783 | /* Check that only command bytes listed in the mask are non-zero */ | 1827 | /* Check that only command bytes listed in the mask are non-zero */ |
1784 | fsg->common->cmnd[1] &= 0x1f; /* Mask away the LUN */ | 1828 | common->cmnd[1] &= 0x1f; /* Mask away the LUN */ |
1785 | for (i = 1; i < cmnd_size; ++i) { | 1829 | for (i = 1; i < cmnd_size; ++i) { |
1786 | if (fsg->common->cmnd[i] && !(mask & (1 << i))) { | 1830 | if (common->cmnd[i] && !(mask & (1 << i))) { |
1787 | if (curlun) | 1831 | if (curlun) |
1788 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | 1832 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
1789 | return -EINVAL; | 1833 | return -EINVAL; |
@@ -1801,7 +1845,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size, | |||
1801 | } | 1845 | } |
1802 | 1846 | ||
1803 | 1847 | ||
1804 | static int do_scsi_command(struct fsg_dev *fsg) | 1848 | static int do_scsi_command(struct fsg_common *common) |
1805 | { | 1849 | { |
1806 | struct fsg_buffhd *bh; | 1850 | struct fsg_buffhd *bh; |
1807 | int rc; | 1851 | int rc; |
@@ -1809,182 +1853,181 @@ static int do_scsi_command(struct fsg_dev *fsg) | |||
1809 | int i; | 1853 | int i; |
1810 | static char unknown[16]; | 1854 | static char unknown[16]; |
1811 | 1855 | ||
1812 | dump_cdb(fsg->common); | 1856 | dump_cdb(common); |
1813 | 1857 | ||
1814 | /* Wait for the next buffer to become available for data or status */ | 1858 | /* Wait for the next buffer to become available for data or status */ |
1815 | bh = fsg->common->next_buffhd_to_fill; | 1859 | bh = common->next_buffhd_to_fill; |
1816 | fsg->common->next_buffhd_to_drain = bh; | 1860 | common->next_buffhd_to_drain = bh; |
1817 | while (bh->state != BUF_STATE_EMPTY) { | 1861 | while (bh->state != BUF_STATE_EMPTY) { |
1818 | rc = sleep_thread(fsg); | 1862 | rc = sleep_thread(common); |
1819 | if (rc) | 1863 | if (rc) |
1820 | return rc; | 1864 | return rc; |
1821 | } | 1865 | } |
1822 | fsg->phase_error = 0; | 1866 | common->phase_error = 0; |
1823 | fsg->short_packet_received = 0; | 1867 | common->short_packet_received = 0; |
1824 | 1868 | ||
1825 | /* We're using the backing file */ | 1869 | down_read(&common->filesem); /* We're using the backing file */ |
1826 | down_read(&fsg->common->filesem); | 1870 | switch (common->cmnd[0]) { |
1827 | switch (fsg->common->cmnd[0]) { | ||
1828 | 1871 | ||
1829 | case SC_INQUIRY: | 1872 | case SC_INQUIRY: |
1830 | fsg->data_size_from_cmnd = fsg->common->cmnd[4]; | 1873 | common->data_size_from_cmnd = common->cmnd[4]; |
1831 | reply = check_command(fsg, 6, DATA_DIR_TO_HOST, | 1874 | reply = check_command(common, 6, DATA_DIR_TO_HOST, |
1832 | (1<<4), 0, | 1875 | (1<<4), 0, |
1833 | "INQUIRY"); | 1876 | "INQUIRY"); |
1834 | if (reply == 0) | 1877 | if (reply == 0) |
1835 | reply = do_inquiry(fsg, bh); | 1878 | reply = do_inquiry(common, bh); |
1836 | break; | 1879 | break; |
1837 | 1880 | ||
1838 | case SC_MODE_SELECT_6: | 1881 | case SC_MODE_SELECT_6: |
1839 | fsg->data_size_from_cmnd = fsg->common->cmnd[4]; | 1882 | common->data_size_from_cmnd = common->cmnd[4]; |
1840 | reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, | 1883 | reply = check_command(common, 6, DATA_DIR_FROM_HOST, |
1841 | (1<<1) | (1<<4), 0, | 1884 | (1<<1) | (1<<4), 0, |
1842 | "MODE SELECT(6)"); | 1885 | "MODE SELECT(6)"); |
1843 | if (reply == 0) | 1886 | if (reply == 0) |
1844 | reply = do_mode_select(fsg, bh); | 1887 | reply = do_mode_select(common, bh); |
1845 | break; | 1888 | break; |
1846 | 1889 | ||
1847 | case SC_MODE_SELECT_10: | 1890 | case SC_MODE_SELECT_10: |
1848 | fsg->data_size_from_cmnd = | 1891 | common->data_size_from_cmnd = |
1849 | get_unaligned_be16(&fsg->common->cmnd[7]); | 1892 | get_unaligned_be16(&common->cmnd[7]); |
1850 | reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, | 1893 | reply = check_command(common, 10, DATA_DIR_FROM_HOST, |
1851 | (1<<1) | (3<<7), 0, | 1894 | (1<<1) | (3<<7), 0, |
1852 | "MODE SELECT(10)"); | 1895 | "MODE SELECT(10)"); |
1853 | if (reply == 0) | 1896 | if (reply == 0) |
1854 | reply = do_mode_select(fsg, bh); | 1897 | reply = do_mode_select(common, bh); |
1855 | break; | 1898 | break; |
1856 | 1899 | ||
1857 | case SC_MODE_SENSE_6: | 1900 | case SC_MODE_SENSE_6: |
1858 | fsg->data_size_from_cmnd = fsg->common->cmnd[4]; | 1901 | common->data_size_from_cmnd = common->cmnd[4]; |
1859 | reply = check_command(fsg, 6, DATA_DIR_TO_HOST, | 1902 | reply = check_command(common, 6, DATA_DIR_TO_HOST, |
1860 | (1<<1) | (1<<2) | (1<<4), 0, | 1903 | (1<<1) | (1<<2) | (1<<4), 0, |
1861 | "MODE SENSE(6)"); | 1904 | "MODE SENSE(6)"); |
1862 | if (reply == 0) | 1905 | if (reply == 0) |
1863 | reply = do_mode_sense(fsg, bh); | 1906 | reply = do_mode_sense(common, bh); |
1864 | break; | 1907 | break; |
1865 | 1908 | ||
1866 | case SC_MODE_SENSE_10: | 1909 | case SC_MODE_SENSE_10: |
1867 | fsg->data_size_from_cmnd = | 1910 | common->data_size_from_cmnd = |
1868 | get_unaligned_be16(&fsg->common->cmnd[7]); | 1911 | get_unaligned_be16(&common->cmnd[7]); |
1869 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1912 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1870 | (1<<1) | (1<<2) | (3<<7), 0, | 1913 | (1<<1) | (1<<2) | (3<<7), 0, |
1871 | "MODE SENSE(10)"); | 1914 | "MODE SENSE(10)"); |
1872 | if (reply == 0) | 1915 | if (reply == 0) |
1873 | reply = do_mode_sense(fsg, bh); | 1916 | reply = do_mode_sense(common, bh); |
1874 | break; | 1917 | break; |
1875 | 1918 | ||
1876 | case SC_PREVENT_ALLOW_MEDIUM_REMOVAL: | 1919 | case SC_PREVENT_ALLOW_MEDIUM_REMOVAL: |
1877 | fsg->data_size_from_cmnd = 0; | 1920 | common->data_size_from_cmnd = 0; |
1878 | reply = check_command(fsg, 6, DATA_DIR_NONE, | 1921 | reply = check_command(common, 6, DATA_DIR_NONE, |
1879 | (1<<4), 0, | 1922 | (1<<4), 0, |
1880 | "PREVENT-ALLOW MEDIUM REMOVAL"); | 1923 | "PREVENT-ALLOW MEDIUM REMOVAL"); |
1881 | if (reply == 0) | 1924 | if (reply == 0) |
1882 | reply = do_prevent_allow(fsg); | 1925 | reply = do_prevent_allow(common); |
1883 | break; | 1926 | break; |
1884 | 1927 | ||
1885 | case SC_READ_6: | 1928 | case SC_READ_6: |
1886 | i = fsg->common->cmnd[4]; | 1929 | i = common->cmnd[4]; |
1887 | fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; | 1930 | common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; |
1888 | reply = check_command(fsg, 6, DATA_DIR_TO_HOST, | 1931 | reply = check_command(common, 6, DATA_DIR_TO_HOST, |
1889 | (7<<1) | (1<<4), 1, | 1932 | (7<<1) | (1<<4), 1, |
1890 | "READ(6)"); | 1933 | "READ(6)"); |
1891 | if (reply == 0) | 1934 | if (reply == 0) |
1892 | reply = do_read(fsg); | 1935 | reply = do_read(common); |
1893 | break; | 1936 | break; |
1894 | 1937 | ||
1895 | case SC_READ_10: | 1938 | case SC_READ_10: |
1896 | fsg->data_size_from_cmnd = | 1939 | common->data_size_from_cmnd = |
1897 | get_unaligned_be16(&fsg->common->cmnd[7]) << 9; | 1940 | get_unaligned_be16(&common->cmnd[7]) << 9; |
1898 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1941 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1899 | (1<<1) | (0xf<<2) | (3<<7), 1, | 1942 | (1<<1) | (0xf<<2) | (3<<7), 1, |
1900 | "READ(10)"); | 1943 | "READ(10)"); |
1901 | if (reply == 0) | 1944 | if (reply == 0) |
1902 | reply = do_read(fsg); | 1945 | reply = do_read(common); |
1903 | break; | 1946 | break; |
1904 | 1947 | ||
1905 | case SC_READ_12: | 1948 | case SC_READ_12: |
1906 | fsg->data_size_from_cmnd = | 1949 | common->data_size_from_cmnd = |
1907 | get_unaligned_be32(&fsg->common->cmnd[6]) << 9; | 1950 | get_unaligned_be32(&common->cmnd[6]) << 9; |
1908 | reply = check_command(fsg, 12, DATA_DIR_TO_HOST, | 1951 | reply = check_command(common, 12, DATA_DIR_TO_HOST, |
1909 | (1<<1) | (0xf<<2) | (0xf<<6), 1, | 1952 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
1910 | "READ(12)"); | 1953 | "READ(12)"); |
1911 | if (reply == 0) | 1954 | if (reply == 0) |
1912 | reply = do_read(fsg); | 1955 | reply = do_read(common); |
1913 | break; | 1956 | break; |
1914 | 1957 | ||
1915 | case SC_READ_CAPACITY: | 1958 | case SC_READ_CAPACITY: |
1916 | fsg->data_size_from_cmnd = 8; | 1959 | common->data_size_from_cmnd = 8; |
1917 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1960 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1918 | (0xf<<2) | (1<<8), 1, | 1961 | (0xf<<2) | (1<<8), 1, |
1919 | "READ CAPACITY"); | 1962 | "READ CAPACITY"); |
1920 | if (reply == 0) | 1963 | if (reply == 0) |
1921 | reply = do_read_capacity(fsg, bh); | 1964 | reply = do_read_capacity(common, bh); |
1922 | break; | 1965 | break; |
1923 | 1966 | ||
1924 | case SC_READ_HEADER: | 1967 | case SC_READ_HEADER: |
1925 | if (!fsg->common->curlun || !fsg->common->curlun->cdrom) | 1968 | if (!common->curlun || !common->curlun->cdrom) |
1926 | goto unknown_cmnd; | 1969 | goto unknown_cmnd; |
1927 | fsg->data_size_from_cmnd = | 1970 | common->data_size_from_cmnd = |
1928 | get_unaligned_be16(&fsg->common->cmnd[7]); | 1971 | get_unaligned_be16(&common->cmnd[7]); |
1929 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1972 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1930 | (3<<7) | (0x1f<<1), 1, | 1973 | (3<<7) | (0x1f<<1), 1, |
1931 | "READ HEADER"); | 1974 | "READ HEADER"); |
1932 | if (reply == 0) | 1975 | if (reply == 0) |
1933 | reply = do_read_header(fsg, bh); | 1976 | reply = do_read_header(common, bh); |
1934 | break; | 1977 | break; |
1935 | 1978 | ||
1936 | case SC_READ_TOC: | 1979 | case SC_READ_TOC: |
1937 | if (!fsg->common->curlun || !fsg->common->curlun->cdrom) | 1980 | if (!common->curlun || !common->curlun->cdrom) |
1938 | goto unknown_cmnd; | 1981 | goto unknown_cmnd; |
1939 | fsg->data_size_from_cmnd = | 1982 | common->data_size_from_cmnd = |
1940 | get_unaligned_be16(&fsg->common->cmnd[7]); | 1983 | get_unaligned_be16(&common->cmnd[7]); |
1941 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1984 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1942 | (7<<6) | (1<<1), 1, | 1985 | (7<<6) | (1<<1), 1, |
1943 | "READ TOC"); | 1986 | "READ TOC"); |
1944 | if (reply == 0) | 1987 | if (reply == 0) |
1945 | reply = do_read_toc(fsg, bh); | 1988 | reply = do_read_toc(common, bh); |
1946 | break; | 1989 | break; |
1947 | 1990 | ||
1948 | case SC_READ_FORMAT_CAPACITIES: | 1991 | case SC_READ_FORMAT_CAPACITIES: |
1949 | fsg->data_size_from_cmnd = | 1992 | common->data_size_from_cmnd = |
1950 | get_unaligned_be16(&fsg->common->cmnd[7]); | 1993 | get_unaligned_be16(&common->cmnd[7]); |
1951 | reply = check_command(fsg, 10, DATA_DIR_TO_HOST, | 1994 | reply = check_command(common, 10, DATA_DIR_TO_HOST, |
1952 | (3<<7), 1, | 1995 | (3<<7), 1, |
1953 | "READ FORMAT CAPACITIES"); | 1996 | "READ FORMAT CAPACITIES"); |
1954 | if (reply == 0) | 1997 | if (reply == 0) |
1955 | reply = do_read_format_capacities(fsg, bh); | 1998 | reply = do_read_format_capacities(common, bh); |
1956 | break; | 1999 | break; |
1957 | 2000 | ||
1958 | case SC_REQUEST_SENSE: | 2001 | case SC_REQUEST_SENSE: |
1959 | fsg->data_size_from_cmnd = fsg->common->cmnd[4]; | 2002 | common->data_size_from_cmnd = common->cmnd[4]; |
1960 | reply = check_command(fsg, 6, DATA_DIR_TO_HOST, | 2003 | reply = check_command(common, 6, DATA_DIR_TO_HOST, |
1961 | (1<<4), 0, | 2004 | (1<<4), 0, |
1962 | "REQUEST SENSE"); | 2005 | "REQUEST SENSE"); |
1963 | if (reply == 0) | 2006 | if (reply == 0) |
1964 | reply = do_request_sense(fsg, bh); | 2007 | reply = do_request_sense(common, bh); |
1965 | break; | 2008 | break; |
1966 | 2009 | ||
1967 | case SC_START_STOP_UNIT: | 2010 | case SC_START_STOP_UNIT: |
1968 | fsg->data_size_from_cmnd = 0; | 2011 | common->data_size_from_cmnd = 0; |
1969 | reply = check_command(fsg, 6, DATA_DIR_NONE, | 2012 | reply = check_command(common, 6, DATA_DIR_NONE, |
1970 | (1<<1) | (1<<4), 0, | 2013 | (1<<1) | (1<<4), 0, |
1971 | "START-STOP UNIT"); | 2014 | "START-STOP UNIT"); |
1972 | if (reply == 0) | 2015 | if (reply == 0) |
1973 | reply = do_start_stop(fsg); | 2016 | reply = do_start_stop(common); |
1974 | break; | 2017 | break; |
1975 | 2018 | ||
1976 | case SC_SYNCHRONIZE_CACHE: | 2019 | case SC_SYNCHRONIZE_CACHE: |
1977 | fsg->data_size_from_cmnd = 0; | 2020 | common->data_size_from_cmnd = 0; |
1978 | reply = check_command(fsg, 10, DATA_DIR_NONE, | 2021 | reply = check_command(common, 10, DATA_DIR_NONE, |
1979 | (0xf<<2) | (3<<7), 1, | 2022 | (0xf<<2) | (3<<7), 1, |
1980 | "SYNCHRONIZE CACHE"); | 2023 | "SYNCHRONIZE CACHE"); |
1981 | if (reply == 0) | 2024 | if (reply == 0) |
1982 | reply = do_synchronize_cache(fsg); | 2025 | reply = do_synchronize_cache(common); |
1983 | break; | 2026 | break; |
1984 | 2027 | ||
1985 | case SC_TEST_UNIT_READY: | 2028 | case SC_TEST_UNIT_READY: |
1986 | fsg->data_size_from_cmnd = 0; | 2029 | common->data_size_from_cmnd = 0; |
1987 | reply = check_command(fsg, 6, DATA_DIR_NONE, | 2030 | reply = check_command(common, 6, DATA_DIR_NONE, |
1988 | 0, 1, | 2031 | 0, 1, |
1989 | "TEST UNIT READY"); | 2032 | "TEST UNIT READY"); |
1990 | break; | 2033 | break; |
@@ -1992,42 +2035,42 @@ static int do_scsi_command(struct fsg_dev *fsg) | |||
1992 | /* Although optional, this command is used by MS-Windows. We | 2035 | /* Although optional, this command is used by MS-Windows. We |
1993 | * support a minimal version: BytChk must be 0. */ | 2036 | * support a minimal version: BytChk must be 0. */ |
1994 | case SC_VERIFY: | 2037 | case SC_VERIFY: |
1995 | fsg->data_size_from_cmnd = 0; | 2038 | common->data_size_from_cmnd = 0; |
1996 | reply = check_command(fsg, 10, DATA_DIR_NONE, | 2039 | reply = check_command(common, 10, DATA_DIR_NONE, |
1997 | (1<<1) | (0xf<<2) | (3<<7), 1, | 2040 | (1<<1) | (0xf<<2) | (3<<7), 1, |
1998 | "VERIFY"); | 2041 | "VERIFY"); |
1999 | if (reply == 0) | 2042 | if (reply == 0) |
2000 | reply = do_verify(fsg); | 2043 | reply = do_verify(common); |
2001 | break; | 2044 | break; |
2002 | 2045 | ||
2003 | case SC_WRITE_6: | 2046 | case SC_WRITE_6: |
2004 | i = fsg->common->cmnd[4]; | 2047 | i = common->cmnd[4]; |
2005 | fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; | 2048 | common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; |
2006 | reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, | 2049 | reply = check_command(common, 6, DATA_DIR_FROM_HOST, |
2007 | (7<<1) | (1<<4), 1, | 2050 | (7<<1) | (1<<4), 1, |
2008 | "WRITE(6)"); | 2051 | "WRITE(6)"); |
2009 | if (reply == 0) | 2052 | if (reply == 0) |
2010 | reply = do_write(fsg); | 2053 | reply = do_write(common); |
2011 | break; | 2054 | break; |
2012 | 2055 | ||
2013 | case SC_WRITE_10: | 2056 | case SC_WRITE_10: |
2014 | fsg->data_size_from_cmnd = | 2057 | common->data_size_from_cmnd = |
2015 | get_unaligned_be16(&fsg->common->cmnd[7]) << 9; | 2058 | get_unaligned_be16(&common->cmnd[7]) << 9; |
2016 | reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, | 2059 | reply = check_command(common, 10, DATA_DIR_FROM_HOST, |
2017 | (1<<1) | (0xf<<2) | (3<<7), 1, | 2060 | (1<<1) | (0xf<<2) | (3<<7), 1, |
2018 | "WRITE(10)"); | 2061 | "WRITE(10)"); |
2019 | if (reply == 0) | 2062 | if (reply == 0) |
2020 | reply = do_write(fsg); | 2063 | reply = do_write(common); |
2021 | break; | 2064 | break; |
2022 | 2065 | ||
2023 | case SC_WRITE_12: | 2066 | case SC_WRITE_12: |
2024 | fsg->data_size_from_cmnd = | 2067 | common->data_size_from_cmnd = |
2025 | get_unaligned_be32(&fsg->common->cmnd[6]) << 9; | 2068 | get_unaligned_be32(&common->cmnd[6]) << 9; |
2026 | reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, | 2069 | reply = check_command(common, 12, DATA_DIR_FROM_HOST, |
2027 | (1<<1) | (0xf<<2) | (0xf<<6), 1, | 2070 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
2028 | "WRITE(12)"); | 2071 | "WRITE(12)"); |
2029 | if (reply == 0) | 2072 | if (reply == 0) |
2030 | reply = do_write(fsg); | 2073 | reply = do_write(common); |
2031 | break; | 2074 | break; |
2032 | 2075 | ||
2033 | /* Some mandatory commands that we recognize but don't implement. | 2076 | /* Some mandatory commands that we recognize but don't implement. |
@@ -2042,17 +2085,17 @@ static int do_scsi_command(struct fsg_dev *fsg) | |||
2042 | 2085 | ||
2043 | default: | 2086 | default: |
2044 | unknown_cmnd: | 2087 | unknown_cmnd: |
2045 | fsg->data_size_from_cmnd = 0; | 2088 | common->data_size_from_cmnd = 0; |
2046 | sprintf(unknown, "Unknown x%02x", fsg->common->cmnd[0]); | 2089 | sprintf(unknown, "Unknown x%02x", common->cmnd[0]); |
2047 | reply = check_command(fsg, fsg->common->cmnd_size, | 2090 | reply = check_command(common, common->cmnd_size, |
2048 | DATA_DIR_UNKNOWN, 0xff, 0, unknown); | 2091 | DATA_DIR_UNKNOWN, 0xff, 0, unknown); |
2049 | if (reply == 0) { | 2092 | if (reply == 0) { |
2050 | fsg->common->curlun->sense_data = SS_INVALID_COMMAND; | 2093 | common->curlun->sense_data = SS_INVALID_COMMAND; |
2051 | reply = -EINVAL; | 2094 | reply = -EINVAL; |
2052 | } | 2095 | } |
2053 | break; | 2096 | break; |
2054 | } | 2097 | } |
2055 | up_read(&fsg->common->filesem); | 2098 | up_read(&common->filesem); |
2056 | 2099 | ||
2057 | if (reply == -EINTR || signal_pending(current)) | 2100 | if (reply == -EINTR || signal_pending(current)) |
2058 | return -EINTR; | 2101 | return -EINTR; |
@@ -2060,11 +2103,11 @@ unknown_cmnd: | |||
2060 | /* Set up the single reply buffer for finish_reply() */ | 2103 | /* Set up the single reply buffer for finish_reply() */ |
2061 | if (reply == -EINVAL) | 2104 | if (reply == -EINVAL) |
2062 | reply = 0; /* Error reply length */ | 2105 | reply = 0; /* Error reply length */ |
2063 | if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) { | 2106 | if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { |
2064 | reply = min((u32) reply, fsg->data_size_from_cmnd); | 2107 | reply = min((u32) reply, common->data_size_from_cmnd); |
2065 | bh->inreq->length = reply; | 2108 | bh->inreq->length = reply; |
2066 | bh->state = BUF_STATE_FULL; | 2109 | bh->state = BUF_STATE_FULL; |
2067 | fsg->residue -= reply; | 2110 | common->residue -= reply; |
2068 | } /* Otherwise it's already set */ | 2111 | } /* Otherwise it's already set */ |
2069 | 2112 | ||
2070 | return 0; | 2113 | return 0; |
@@ -2075,8 +2118,9 @@ unknown_cmnd: | |||
2075 | 2118 | ||
2076 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | 2119 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
2077 | { | 2120 | { |
2078 | struct usb_request *req = bh->outreq; | 2121 | struct usb_request *req = bh->outreq; |
2079 | struct fsg_bulk_cb_wrap *cbw = req->buf; | 2122 | struct fsg_bulk_cb_wrap *cbw = req->buf; |
2123 | struct fsg_common *common = fsg->common; | ||
2080 | 2124 | ||
2081 | /* Was this a real packet? Should it be ignored? */ | 2125 | /* Was this a real packet? Should it be ignored? */ |
2082 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) | 2126 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) |
@@ -2113,7 +2157,7 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2113 | 2157 | ||
2114 | /* We can do anything we want here, so let's stall the | 2158 | /* We can do anything we want here, so let's stall the |
2115 | * bulk pipes if we are allowed to. */ | 2159 | * bulk pipes if we are allowed to. */ |
2116 | if (fsg->can_stall) { | 2160 | if (common->can_stall) { |
2117 | fsg_set_halt(fsg, fsg->bulk_out); | 2161 | fsg_set_halt(fsg, fsg->bulk_out); |
2118 | halt_bulk_in_endpoint(fsg); | 2162 | halt_bulk_in_endpoint(fsg); |
2119 | } | 2163 | } |
@@ -2121,39 +2165,41 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |||
2121 | } | 2165 | } |
2122 | 2166 | ||
2123 | /* Save the command for later */ | 2167 | /* Save the command for later */ |
2124 | fsg->common->cmnd_size = cbw->Length; | 2168 | common->cmnd_size = cbw->Length; |
2125 | memcpy(fsg->common->cmnd, cbw->CDB, fsg->common->cmnd_size); | 2169 | memcpy(common->cmnd, cbw->CDB, common->cmnd_size); |
2126 | if (cbw->Flags & USB_BULK_IN_FLAG) | 2170 | if (cbw->Flags & USB_BULK_IN_FLAG) |
2127 | fsg->data_dir = DATA_DIR_TO_HOST; | 2171 | common->data_dir = DATA_DIR_TO_HOST; |
2128 | else | 2172 | else |
2129 | fsg->data_dir = DATA_DIR_FROM_HOST; | 2173 | common->data_dir = DATA_DIR_FROM_HOST; |
2130 | fsg->data_size = le32_to_cpu(cbw->DataTransferLength); | 2174 | common->data_size = le32_to_cpu(cbw->DataTransferLength); |
2131 | if (fsg->data_size == 0) | 2175 | if (common->data_size == 0) |
2132 | fsg->data_dir = DATA_DIR_NONE; | 2176 | common->data_dir = DATA_DIR_NONE; |
2133 | fsg->common->lun = cbw->Lun; | 2177 | common->lun = cbw->Lun; |
2134 | fsg->tag = cbw->Tag; | 2178 | common->tag = cbw->Tag; |
2135 | return 0; | 2179 | return 0; |
2136 | } | 2180 | } |
2137 | 2181 | ||
2138 | 2182 | ||
2139 | static int get_next_command(struct fsg_dev *fsg) | 2183 | static int get_next_command(struct fsg_common *common) |
2140 | { | 2184 | { |
2141 | struct fsg_buffhd *bh; | 2185 | struct fsg_buffhd *bh; |
2142 | int rc = 0; | 2186 | int rc = 0; |
2143 | 2187 | ||
2144 | /* Wait for the next buffer to become available */ | 2188 | /* Wait for the next buffer to become available */ |
2145 | bh = fsg->common->next_buffhd_to_fill; | 2189 | bh = common->next_buffhd_to_fill; |
2146 | while (bh->state != BUF_STATE_EMPTY) { | 2190 | while (bh->state != BUF_STATE_EMPTY) { |
2147 | rc = sleep_thread(fsg); | 2191 | rc = sleep_thread(common); |
2148 | if (rc) | 2192 | if (rc) |
2149 | return rc; | 2193 | return rc; |
2150 | } | 2194 | } |
2151 | 2195 | ||
2152 | /* Queue a request to read a Bulk-only CBW */ | 2196 | /* Queue a request to read a Bulk-only CBW */ |
2153 | set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN); | 2197 | set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); |
2154 | bh->outreq->short_not_ok = 1; | 2198 | bh->outreq->short_not_ok = 1; |
2155 | start_transfer(fsg, fsg->bulk_out, bh->outreq, | 2199 | START_TRANSFER_OR(common, bulk_out, bh->outreq, |
2156 | &bh->outreq_busy, &bh->state); | 2200 | &bh->outreq_busy, &bh->state) |
2201 | /* Don't know what to do if common->fsg is NULL */ | ||
2202 | return -EIO; | ||
2157 | 2203 | ||
2158 | /* We will drain the buffer in software, which means we | 2204 | /* We will drain the buffer in software, which means we |
2159 | * can reuse it for the next filling. No need to advance | 2205 | * can reuse it for the next filling. No need to advance |
@@ -2161,12 +2207,12 @@ static int get_next_command(struct fsg_dev *fsg) | |||
2161 | 2207 | ||
2162 | /* Wait for the CBW to arrive */ | 2208 | /* Wait for the CBW to arrive */ |
2163 | while (bh->state != BUF_STATE_FULL) { | 2209 | while (bh->state != BUF_STATE_FULL) { |
2164 | rc = sleep_thread(fsg); | 2210 | rc = sleep_thread(common); |
2165 | if (rc) | 2211 | if (rc) |
2166 | return rc; | 2212 | return rc; |
2167 | } | 2213 | } |
2168 | smp_rmb(); | 2214 | smp_rmb(); |
2169 | rc = received_cbw(fsg, bh); | 2215 | rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; |
2170 | bh->state = BUF_STATE_EMPTY; | 2216 | bh->state = BUF_STATE_EMPTY; |
2171 | 2217 | ||
2172 | return rc; | 2218 | return rc; |
@@ -2175,25 +2221,25 @@ static int get_next_command(struct fsg_dev *fsg) | |||
2175 | 2221 | ||
2176 | /*-------------------------------------------------------------------------*/ | 2222 | /*-------------------------------------------------------------------------*/ |
2177 | 2223 | ||
2178 | static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep, | 2224 | static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep, |
2179 | const struct usb_endpoint_descriptor *d) | 2225 | const struct usb_endpoint_descriptor *d) |
2180 | { | 2226 | { |
2181 | int rc; | 2227 | int rc; |
2182 | 2228 | ||
2183 | ep->driver_data = fsg; | 2229 | ep->driver_data = common; |
2184 | rc = usb_ep_enable(ep, d); | 2230 | rc = usb_ep_enable(ep, d); |
2185 | if (rc) | 2231 | if (rc) |
2186 | ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc); | 2232 | ERROR(common, "can't enable %s, result %d\n", ep->name, rc); |
2187 | return rc; | 2233 | return rc; |
2188 | } | 2234 | } |
2189 | 2235 | ||
2190 | static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep, | 2236 | static int alloc_request(struct fsg_common *common, struct usb_ep *ep, |
2191 | struct usb_request **preq) | 2237 | struct usb_request **preq) |
2192 | { | 2238 | { |
2193 | *preq = usb_ep_alloc_request(ep, GFP_ATOMIC); | 2239 | *preq = usb_ep_alloc_request(ep, GFP_ATOMIC); |
2194 | if (*preq) | 2240 | if (*preq) |
2195 | return 0; | 2241 | return 0; |
2196 | ERROR(fsg, "can't allocate request for %s\n", ep->name); | 2242 | ERROR(common, "can't allocate request for %s\n", ep->name); |
2197 | return -ENOMEM; | 2243 | return -ENOMEM; |
2198 | } | 2244 | } |
2199 | 2245 | ||
@@ -2202,83 +2248,96 @@ static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep, | |||
2202 | * Call with altsetting < 0 to disable the interface. The only other | 2248 | * Call with altsetting < 0 to disable the interface. The only other |
2203 | * available altsetting is 0, which enables the interface. | 2249 | * available altsetting is 0, which enables the interface. |
2204 | */ | 2250 | */ |
2205 | static int do_set_interface(struct fsg_dev *fsg, int altsetting) | 2251 | static int do_set_interface(struct fsg_common *common, int altsetting) |
2206 | { | 2252 | { |
2207 | int rc = 0; | 2253 | int rc = 0; |
2208 | int i; | 2254 | int i; |
2209 | const struct usb_endpoint_descriptor *d; | 2255 | const struct usb_endpoint_descriptor *d; |
2210 | 2256 | ||
2211 | if (fsg->running) | 2257 | if (common->running) |
2212 | DBG(fsg, "reset interface\n"); | 2258 | DBG(common, "reset interface\n"); |
2213 | 2259 | ||
2214 | reset: | 2260 | reset: |
2215 | /* Deallocate the requests */ | 2261 | /* Deallocate the requests */ |
2216 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | 2262 | if (common->prev_fsg) { |
2217 | struct fsg_buffhd *bh = &fsg->common->buffhds[i]; | 2263 | struct fsg_dev *fsg = common->prev_fsg; |
2264 | |||
2265 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | ||
2266 | struct fsg_buffhd *bh = &common->buffhds[i]; | ||
2218 | 2267 | ||
2219 | if (bh->inreq) { | 2268 | if (bh->inreq) { |
2220 | usb_ep_free_request(fsg->bulk_in, bh->inreq); | 2269 | usb_ep_free_request(fsg->bulk_in, bh->inreq); |
2221 | bh->inreq = NULL; | 2270 | bh->inreq = NULL; |
2271 | } | ||
2272 | if (bh->outreq) { | ||
2273 | usb_ep_free_request(fsg->bulk_out, bh->outreq); | ||
2274 | bh->outreq = NULL; | ||
2275 | } | ||
2222 | } | 2276 | } |
2223 | if (bh->outreq) { | 2277 | |
2224 | usb_ep_free_request(fsg->bulk_out, bh->outreq); | 2278 | /* Disable the endpoints */ |
2225 | bh->outreq = NULL; | 2279 | if (fsg->bulk_in_enabled) { |
2280 | usb_ep_disable(fsg->bulk_in); | ||
2281 | fsg->bulk_in_enabled = 0; | ||
2282 | } | ||
2283 | if (fsg->bulk_out_enabled) { | ||
2284 | usb_ep_disable(fsg->bulk_out); | ||
2285 | fsg->bulk_out_enabled = 0; | ||
2226 | } | 2286 | } |
2227 | } | ||
2228 | 2287 | ||
2229 | /* Disable the endpoints */ | 2288 | common->prev_fsg = 0; |
2230 | if (fsg->bulk_in_enabled) { | ||
2231 | usb_ep_disable(fsg->bulk_in); | ||
2232 | fsg->bulk_in_enabled = 0; | ||
2233 | } | ||
2234 | if (fsg->bulk_out_enabled) { | ||
2235 | usb_ep_disable(fsg->bulk_out); | ||
2236 | fsg->bulk_out_enabled = 0; | ||
2237 | } | 2289 | } |
2238 | 2290 | ||
2239 | fsg->running = 0; | 2291 | common->running = 0; |
2240 | if (altsetting < 0 || rc != 0) | 2292 | if (altsetting < 0 || rc != 0) |
2241 | return rc; | 2293 | return rc; |
2242 | 2294 | ||
2243 | DBG(fsg, "set interface %d\n", altsetting); | 2295 | DBG(common, "set interface %d\n", altsetting); |
2244 | |||
2245 | /* Enable the endpoints */ | ||
2246 | d = fsg_ep_desc(fsg->gadget, | ||
2247 | &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); | ||
2248 | rc = enable_endpoint(fsg, fsg->bulk_in, d); | ||
2249 | if (rc != 0) | ||
2250 | goto reset; | ||
2251 | fsg->bulk_in_enabled = 1; | ||
2252 | |||
2253 | d = fsg_ep_desc(fsg->gadget, | ||
2254 | &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); | ||
2255 | rc = enable_endpoint(fsg, fsg->bulk_out, d); | ||
2256 | if (rc != 0) | ||
2257 | goto reset; | ||
2258 | fsg->bulk_out_enabled = 1; | ||
2259 | fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize); | ||
2260 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); | ||
2261 | |||
2262 | /* Allocate the requests */ | ||
2263 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | ||
2264 | struct fsg_buffhd *bh = &fsg->common->buffhds[i]; | ||
2265 | 2296 | ||
2266 | rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq); | 2297 | if (fsg_is_set(common)) { |
2267 | if (rc != 0) | 2298 | struct fsg_dev *fsg = common->fsg; |
2299 | common->prev_fsg = common->fsg; | ||
2300 | |||
2301 | /* Enable the endpoints */ | ||
2302 | d = fsg_ep_desc(common->gadget, | ||
2303 | &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); | ||
2304 | rc = enable_endpoint(common, fsg->bulk_in, d); | ||
2305 | if (rc) | ||
2268 | goto reset; | 2306 | goto reset; |
2269 | rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq); | 2307 | fsg->bulk_in_enabled = 1; |
2270 | if (rc != 0) | 2308 | |
2309 | d = fsg_ep_desc(common->gadget, | ||
2310 | &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); | ||
2311 | rc = enable_endpoint(common, fsg->bulk_out, d); | ||
2312 | if (rc) | ||
2271 | goto reset; | 2313 | goto reset; |
2272 | bh->inreq->buf = bh->outreq->buf = bh->buf; | 2314 | fsg->bulk_out_enabled = 1; |
2273 | bh->inreq->context = bh->outreq->context = bh; | 2315 | common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize); |
2274 | bh->inreq->complete = bulk_in_complete; | 2316 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); |
2275 | bh->outreq->complete = bulk_out_complete; | ||
2276 | } | ||
2277 | 2317 | ||
2278 | fsg->running = 1; | 2318 | /* Allocate the requests */ |
2279 | for (i = 0; i < fsg->common->nluns; ++i) | 2319 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
2280 | fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED; | 2320 | struct fsg_buffhd *bh = &common->buffhds[i]; |
2281 | return rc; | 2321 | |
2322 | rc = alloc_request(common, fsg->bulk_in, &bh->inreq); | ||
2323 | if (rc) | ||
2324 | goto reset; | ||
2325 | rc = alloc_request(common, fsg->bulk_out, &bh->outreq); | ||
2326 | if (rc) | ||
2327 | goto reset; | ||
2328 | bh->inreq->buf = bh->outreq->buf = bh->buf; | ||
2329 | bh->inreq->context = bh->outreq->context = bh; | ||
2330 | bh->inreq->complete = bulk_in_complete; | ||
2331 | bh->outreq->complete = bulk_out_complete; | ||
2332 | } | ||
2333 | |||
2334 | common->running = 1; | ||
2335 | for (i = 0; i < common->nluns; ++i) | ||
2336 | common->luns[i].unit_attention_data = SS_RESET_OCCURRED; | ||
2337 | return rc; | ||
2338 | } else { | ||
2339 | return -EIO; | ||
2340 | } | ||
2282 | } | 2341 | } |
2283 | 2342 | ||
2284 | 2343 | ||
@@ -2290,23 +2349,23 @@ reset: | |||
2290 | * configurations might not work with our current power sources. | 2349 | * configurations might not work with our current power sources. |
2291 | * For now we just assume the gadget is always self-powered. | 2350 | * For now we just assume the gadget is always self-powered. |
2292 | */ | 2351 | */ |
2293 | static int do_set_config(struct fsg_dev *fsg, u8 new_config) | 2352 | static int do_set_config(struct fsg_common *common, u8 new_config) |
2294 | { | 2353 | { |
2295 | int rc = 0; | 2354 | int rc = 0; |
2296 | 2355 | ||
2297 | /* Disable the single interface */ | 2356 | /* Disable the single interface */ |
2298 | if (fsg->config != 0) { | 2357 | if (common->config != 0) { |
2299 | DBG(fsg, "reset config\n"); | 2358 | DBG(common, "reset config\n"); |
2300 | fsg->config = 0; | 2359 | common->config = 0; |
2301 | rc = do_set_interface(fsg, -1); | 2360 | rc = do_set_interface(common, -1); |
2302 | } | 2361 | } |
2303 | 2362 | ||
2304 | /* Enable the interface */ | 2363 | /* Enable the interface */ |
2305 | if (new_config != 0) { | 2364 | if (new_config != 0) { |
2306 | fsg->config = new_config; | 2365 | common->config = new_config; |
2307 | rc = do_set_interface(fsg, 0); | 2366 | rc = do_set_interface(common, 0); |
2308 | if (rc != 0) | 2367 | if (rc != 0) |
2309 | fsg->config = 0; /* Reset on errors */ | 2368 | common->config = 0; /* Reset on errors */ |
2310 | } | 2369 | } |
2311 | return rc; | 2370 | return rc; |
2312 | } | 2371 | } |
@@ -2318,22 +2377,26 @@ static int do_set_config(struct fsg_dev *fsg, u8 new_config) | |||
2318 | static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | 2377 | static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
2319 | { | 2378 | { |
2320 | struct fsg_dev *fsg = fsg_from_func(f); | 2379 | struct fsg_dev *fsg = fsg_from_func(f); |
2321 | fsg->new_config = 1; | 2380 | fsg->common->prev_fsg = fsg->common->fsg; |
2322 | raise_exception(fsg, FSG_STATE_CONFIG_CHANGE); | 2381 | fsg->common->fsg = fsg; |
2382 | fsg->common->new_config = 1; | ||
2383 | raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); | ||
2323 | return 0; | 2384 | return 0; |
2324 | } | 2385 | } |
2325 | 2386 | ||
2326 | static void fsg_disable(struct usb_function *f) | 2387 | static void fsg_disable(struct usb_function *f) |
2327 | { | 2388 | { |
2328 | struct fsg_dev *fsg = fsg_from_func(f); | 2389 | struct fsg_dev *fsg = fsg_from_func(f); |
2329 | fsg->new_config = 0; | 2390 | fsg->common->prev_fsg = fsg->common->fsg; |
2330 | raise_exception(fsg, FSG_STATE_CONFIG_CHANGE); | 2391 | fsg->common->fsg = fsg; |
2392 | fsg->common->new_config = 0; | ||
2393 | raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); | ||
2331 | } | 2394 | } |
2332 | 2395 | ||
2333 | 2396 | ||
2334 | /*-------------------------------------------------------------------------*/ | 2397 | /*-------------------------------------------------------------------------*/ |
2335 | 2398 | ||
2336 | static void handle_exception(struct fsg_dev *fsg) | 2399 | static void handle_exception(struct fsg_common *common) |
2337 | { | 2400 | { |
2338 | siginfo_t info; | 2401 | siginfo_t info; |
2339 | int sig; | 2402 | int sig; |
@@ -2352,113 +2415,121 @@ static void handle_exception(struct fsg_dev *fsg) | |||
2352 | if (!sig) | 2415 | if (!sig) |
2353 | break; | 2416 | break; |
2354 | if (sig != SIGUSR1) { | 2417 | if (sig != SIGUSR1) { |
2355 | if (fsg->state < FSG_STATE_EXIT) | 2418 | if (common->state < FSG_STATE_EXIT) |
2356 | DBG(fsg, "Main thread exiting on signal\n"); | 2419 | DBG(common, "Main thread exiting on signal\n"); |
2357 | raise_exception(fsg, FSG_STATE_EXIT); | 2420 | raise_exception(common, FSG_STATE_EXIT); |
2358 | } | 2421 | } |
2359 | } | 2422 | } |
2360 | 2423 | ||
2361 | /* Cancel all the pending transfers */ | 2424 | /* Cancel all the pending transfers */ |
2362 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | 2425 | if (fsg_is_set(common)) { |
2363 | bh = &fsg->common->buffhds[i]; | ||
2364 | if (bh->inreq_busy) | ||
2365 | usb_ep_dequeue(fsg->bulk_in, bh->inreq); | ||
2366 | if (bh->outreq_busy) | ||
2367 | usb_ep_dequeue(fsg->bulk_out, bh->outreq); | ||
2368 | } | ||
2369 | |||
2370 | /* Wait until everything is idle */ | ||
2371 | for (;;) { | ||
2372 | int num_active = 0; | ||
2373 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | 2426 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
2374 | bh = &fsg->common->buffhds[i]; | 2427 | bh = &common->buffhds[i]; |
2375 | num_active += bh->inreq_busy + bh->outreq_busy; | 2428 | if (bh->inreq_busy) |
2429 | usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); | ||
2430 | if (bh->outreq_busy) | ||
2431 | usb_ep_dequeue(common->fsg->bulk_out, | ||
2432 | bh->outreq); | ||
2376 | } | 2433 | } |
2377 | if (num_active == 0) | ||
2378 | break; | ||
2379 | if (sleep_thread(fsg)) | ||
2380 | return; | ||
2381 | } | ||
2382 | 2434 | ||
2383 | /* Clear out the controller's fifos */ | 2435 | /* Wait until everything is idle */ |
2384 | if (fsg->bulk_in_enabled) | 2436 | for (;;) { |
2385 | usb_ep_fifo_flush(fsg->bulk_in); | 2437 | int num_active = 0; |
2386 | if (fsg->bulk_out_enabled) | 2438 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
2387 | usb_ep_fifo_flush(fsg->bulk_out); | 2439 | bh = &common->buffhds[i]; |
2440 | num_active += bh->inreq_busy + bh->outreq_busy; | ||
2441 | } | ||
2442 | if (num_active == 0) | ||
2443 | break; | ||
2444 | if (sleep_thread(common)) | ||
2445 | return; | ||
2446 | } | ||
2447 | |||
2448 | /* Clear out the controller's fifos */ | ||
2449 | if (common->fsg->bulk_in_enabled) | ||
2450 | usb_ep_fifo_flush(common->fsg->bulk_in); | ||
2451 | if (common->fsg->bulk_out_enabled) | ||
2452 | usb_ep_fifo_flush(common->fsg->bulk_out); | ||
2453 | } | ||
2388 | 2454 | ||
2389 | /* Reset the I/O buffer states and pointers, the SCSI | 2455 | /* Reset the I/O buffer states and pointers, the SCSI |
2390 | * state, and the exception. Then invoke the handler. */ | 2456 | * state, and the exception. Then invoke the handler. */ |
2391 | spin_lock_irq(&fsg->lock); | 2457 | spin_lock_irq(&common->lock); |
2392 | 2458 | ||
2393 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | 2459 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
2394 | bh = &fsg->common->buffhds[i]; | 2460 | bh = &common->buffhds[i]; |
2395 | bh->state = BUF_STATE_EMPTY; | 2461 | bh->state = BUF_STATE_EMPTY; |
2396 | } | 2462 | } |
2397 | fsg->common->next_buffhd_to_fill = &fsg->common->buffhds[0]; | 2463 | common->next_buffhd_to_fill = &common->buffhds[0]; |
2398 | fsg->common->next_buffhd_to_drain = &fsg->common->buffhds[0]; | 2464 | common->next_buffhd_to_drain = &common->buffhds[0]; |
2399 | exception_req_tag = fsg->exception_req_tag; | 2465 | exception_req_tag = common->exception_req_tag; |
2400 | new_config = fsg->new_config; | 2466 | new_config = common->new_config; |
2401 | old_state = fsg->state; | 2467 | old_state = common->state; |
2402 | 2468 | ||
2403 | if (old_state == FSG_STATE_ABORT_BULK_OUT) | 2469 | if (old_state == FSG_STATE_ABORT_BULK_OUT) |
2404 | fsg->state = FSG_STATE_STATUS_PHASE; | 2470 | common->state = FSG_STATE_STATUS_PHASE; |
2405 | else { | 2471 | else { |
2406 | for (i = 0; i < fsg->common->nluns; ++i) { | 2472 | for (i = 0; i < common->nluns; ++i) { |
2407 | curlun = &fsg->common->luns[i]; | 2473 | curlun = &common->luns[i]; |
2408 | curlun->prevent_medium_removal = 0; | 2474 | curlun->prevent_medium_removal = 0; |
2409 | curlun->sense_data = SS_NO_SENSE; | 2475 | curlun->sense_data = SS_NO_SENSE; |
2410 | curlun->unit_attention_data = SS_NO_SENSE; | 2476 | curlun->unit_attention_data = SS_NO_SENSE; |
2411 | curlun->sense_data_info = 0; | 2477 | curlun->sense_data_info = 0; |
2412 | curlun->info_valid = 0; | 2478 | curlun->info_valid = 0; |
2413 | } | 2479 | } |
2414 | fsg->state = FSG_STATE_IDLE; | 2480 | common->state = FSG_STATE_IDLE; |
2415 | } | 2481 | } |
2416 | spin_unlock_irq(&fsg->lock); | 2482 | spin_unlock_irq(&common->lock); |
2417 | 2483 | ||
2418 | /* Carry out any extra actions required for the exception */ | 2484 | /* Carry out any extra actions required for the exception */ |
2419 | switch (old_state) { | 2485 | switch (old_state) { |
2420 | case FSG_STATE_ABORT_BULK_OUT: | 2486 | case FSG_STATE_ABORT_BULK_OUT: |
2421 | send_status(fsg); | 2487 | send_status(common); |
2422 | spin_lock_irq(&fsg->lock); | 2488 | spin_lock_irq(&common->lock); |
2423 | if (fsg->state == FSG_STATE_STATUS_PHASE) | 2489 | if (common->state == FSG_STATE_STATUS_PHASE) |
2424 | fsg->state = FSG_STATE_IDLE; | 2490 | common->state = FSG_STATE_IDLE; |
2425 | spin_unlock_irq(&fsg->lock); | 2491 | spin_unlock_irq(&common->lock); |
2426 | break; | 2492 | break; |
2427 | 2493 | ||
2428 | case FSG_STATE_RESET: | 2494 | case FSG_STATE_RESET: |
2429 | /* In case we were forced against our will to halt a | 2495 | /* In case we were forced against our will to halt a |
2430 | * bulk endpoint, clear the halt now. (The SuperH UDC | 2496 | * bulk endpoint, clear the halt now. (The SuperH UDC |
2431 | * requires this.) */ | 2497 | * requires this.) */ |
2432 | if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) | 2498 | if (!fsg_is_set(common)) |
2433 | usb_ep_clear_halt(fsg->bulk_in); | 2499 | break; |
2500 | if (test_and_clear_bit(IGNORE_BULK_OUT, | ||
2501 | &common->fsg->atomic_bitflags)) | ||
2502 | usb_ep_clear_halt(common->fsg->bulk_in); | ||
2434 | 2503 | ||
2435 | if (fsg->ep0_req_tag == exception_req_tag) | 2504 | if (common->ep0_req_tag == exception_req_tag) |
2436 | ep0_queue(fsg); /* Complete the status stage */ | 2505 | ep0_queue(common); /* Complete the status stage */ |
2437 | 2506 | ||
2438 | /* Technically this should go here, but it would only be | 2507 | /* Technically this should go here, but it would only be |
2439 | * a waste of time. Ditto for the INTERFACE_CHANGE and | 2508 | * a waste of time. Ditto for the INTERFACE_CHANGE and |
2440 | * CONFIG_CHANGE cases. */ | 2509 | * CONFIG_CHANGE cases. */ |
2441 | /* for (i = 0; i < fsg->common->nluns; ++i) */ | 2510 | /* for (i = 0; i < common->nluns; ++i) */ |
2442 | /* fsg->common->luns[i].unit_attention_data = */ | 2511 | /* common->luns[i].unit_attention_data = */ |
2443 | /* SS_RESET_OCCURRED; */ | 2512 | /* SS_RESET_OCCURRED; */ |
2444 | break; | 2513 | break; |
2445 | 2514 | ||
2446 | case FSG_STATE_CONFIG_CHANGE: | 2515 | case FSG_STATE_CONFIG_CHANGE: |
2447 | rc = do_set_config(fsg, new_config); | 2516 | rc = do_set_config(common, new_config); |
2448 | if (fsg->ep0_req_tag != exception_req_tag) | 2517 | if (common->ep0_req_tag != exception_req_tag) |
2449 | break; | 2518 | break; |
2450 | if (rc != 0) /* STALL on errors */ | 2519 | if (rc != 0) { /* STALL on errors */ |
2451 | fsg_set_halt(fsg, fsg->ep0); | 2520 | DBG(common, "ep0 set halt\n"); |
2452 | else /* Complete the status stage */ | 2521 | usb_ep_set_halt(common->ep0); |
2453 | ep0_queue(fsg); | 2522 | } else { /* Complete the status stage */ |
2523 | ep0_queue(common); | ||
2524 | } | ||
2454 | break; | 2525 | break; |
2455 | 2526 | ||
2456 | case FSG_STATE_EXIT: | 2527 | case FSG_STATE_EXIT: |
2457 | case FSG_STATE_TERMINATED: | 2528 | case FSG_STATE_TERMINATED: |
2458 | do_set_config(fsg, 0); /* Free resources */ | 2529 | do_set_config(common, 0); /* Free resources */ |
2459 | spin_lock_irq(&fsg->lock); | 2530 | spin_lock_irq(&common->lock); |
2460 | fsg->state = FSG_STATE_TERMINATED; /* Stop the thread */ | 2531 | common->state = FSG_STATE_TERMINATED; /* Stop the thread */ |
2461 | spin_unlock_irq(&fsg->lock); | 2532 | spin_unlock_irq(&common->lock); |
2462 | break; | 2533 | break; |
2463 | 2534 | ||
2464 | case FSG_STATE_INTERFACE_CHANGE: | 2535 | case FSG_STATE_INTERFACE_CHANGE: |
@@ -2474,9 +2545,9 @@ static void handle_exception(struct fsg_dev *fsg) | |||
2474 | 2545 | ||
2475 | /*-------------------------------------------------------------------------*/ | 2546 | /*-------------------------------------------------------------------------*/ |
2476 | 2547 | ||
2477 | static int fsg_main_thread(void *fsg_) | 2548 | static int fsg_main_thread(void *common_) |
2478 | { | 2549 | { |
2479 | struct fsg_dev *fsg = fsg_; | 2550 | struct fsg_common *common = common_; |
2480 | 2551 | ||
2481 | /* Allow the thread to be killed by a signal, but set the signal mask | 2552 | /* Allow the thread to be killed by a signal, but set the signal mask |
2482 | * to block everything but INT, TERM, KILL, and USR1. */ | 2553 | * to block everything but INT, TERM, KILL, and USR1. */ |
@@ -2494,45 +2565,45 @@ static int fsg_main_thread(void *fsg_) | |||
2494 | set_fs(get_ds()); | 2565 | set_fs(get_ds()); |
2495 | 2566 | ||
2496 | /* The main loop */ | 2567 | /* The main loop */ |
2497 | while (fsg->state != FSG_STATE_TERMINATED) { | 2568 | while (common->state != FSG_STATE_TERMINATED) { |
2498 | if (exception_in_progress(fsg) || signal_pending(current)) { | 2569 | if (exception_in_progress(common) || signal_pending(current)) { |
2499 | handle_exception(fsg); | 2570 | handle_exception(common); |
2500 | continue; | 2571 | continue; |
2501 | } | 2572 | } |
2502 | 2573 | ||
2503 | if (!fsg->running) { | 2574 | if (!common->running) { |
2504 | sleep_thread(fsg); | 2575 | sleep_thread(common); |
2505 | continue; | 2576 | continue; |
2506 | } | 2577 | } |
2507 | 2578 | ||
2508 | if (get_next_command(fsg)) | 2579 | if (get_next_command(common)) |
2509 | continue; | 2580 | continue; |
2510 | 2581 | ||
2511 | spin_lock_irq(&fsg->lock); | 2582 | spin_lock_irq(&common->lock); |
2512 | if (!exception_in_progress(fsg)) | 2583 | if (!exception_in_progress(common)) |
2513 | fsg->state = FSG_STATE_DATA_PHASE; | 2584 | common->state = FSG_STATE_DATA_PHASE; |
2514 | spin_unlock_irq(&fsg->lock); | 2585 | spin_unlock_irq(&common->lock); |
2515 | 2586 | ||
2516 | if (do_scsi_command(fsg) || finish_reply(fsg)) | 2587 | if (do_scsi_command(common) || finish_reply(common)) |
2517 | continue; | 2588 | continue; |
2518 | 2589 | ||
2519 | spin_lock_irq(&fsg->lock); | 2590 | spin_lock_irq(&common->lock); |
2520 | if (!exception_in_progress(fsg)) | 2591 | if (!exception_in_progress(common)) |
2521 | fsg->state = FSG_STATE_STATUS_PHASE; | 2592 | common->state = FSG_STATE_STATUS_PHASE; |
2522 | spin_unlock_irq(&fsg->lock); | 2593 | spin_unlock_irq(&common->lock); |
2523 | 2594 | ||
2524 | if (send_status(fsg)) | 2595 | if (send_status(common)) |
2525 | continue; | 2596 | continue; |
2526 | 2597 | ||
2527 | spin_lock_irq(&fsg->lock); | 2598 | spin_lock_irq(&common->lock); |
2528 | if (!exception_in_progress(fsg)) | 2599 | if (!exception_in_progress(common)) |
2529 | fsg->state = FSG_STATE_IDLE; | 2600 | common->state = FSG_STATE_IDLE; |
2530 | spin_unlock_irq(&fsg->lock); | 2601 | spin_unlock_irq(&common->lock); |
2531 | } | 2602 | } |
2532 | 2603 | ||
2533 | spin_lock_irq(&fsg->lock); | 2604 | spin_lock_irq(&common->lock); |
2534 | fsg->thread_task = NULL; | 2605 | common->thread_task = NULL; |
2535 | spin_unlock_irq(&fsg->lock); | 2606 | spin_unlock_irq(&common->lock); |
2536 | 2607 | ||
2537 | /* XXX */ | 2608 | /* XXX */ |
2538 | /* If we are exiting because of a signal, unregister the | 2609 | /* If we are exiting because of a signal, unregister the |
@@ -2541,7 +2612,7 @@ static int fsg_main_thread(void *fsg_) | |||
2541 | /* usb_gadget_unregister_driver(&fsg_driver); */ | 2612 | /* usb_gadget_unregister_driver(&fsg_driver); */ |
2542 | 2613 | ||
2543 | /* Let the unbind and cleanup routines know the thread has exited */ | 2614 | /* Let the unbind and cleanup routines know the thread has exited */ |
2544 | complete_and_exit(&fsg->thread_notifier, 0); | 2615 | complete_and_exit(&common->thread_notifier, 0); |
2545 | } | 2616 | } |
2546 | 2617 | ||
2547 | 2618 | ||
@@ -2600,7 +2671,21 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, | |||
2600 | memset(common, 0, sizeof common); | 2671 | memset(common, 0, sizeof common); |
2601 | common->free_storage_on_release = 0; | 2672 | common->free_storage_on_release = 0; |
2602 | } | 2673 | } |
2674 | |||
2603 | common->gadget = gadget; | 2675 | common->gadget = gadget; |
2676 | common->ep0 = gadget->ep0; | ||
2677 | common->ep0req = cdev->req; | ||
2678 | |||
2679 | /* Maybe allocate device-global string IDs, and patch descriptors */ | ||
2680 | if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { | ||
2681 | rc = usb_string_id(cdev); | ||
2682 | if (rc < 0) { | ||
2683 | kfree(common); | ||
2684 | return ERR_PTR(rc); | ||
2685 | } | ||
2686 | fsg_strings[FSG_STRING_INTERFACE].id = rc; | ||
2687 | fsg_intf_desc.iInterface = rc; | ||
2688 | } | ||
2604 | 2689 | ||
2605 | /* Create the LUNs, open their backing files, and register the | 2690 | /* Create the LUNs, open their backing files, and register the |
2606 | * LUN devices in sysfs. */ | 2691 | * LUN devices in sysfs. */ |
@@ -2697,11 +2782,23 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, | |||
2697 | * disable stalls. | 2782 | * disable stalls. |
2698 | */ | 2783 | */ |
2699 | common->can_stall = cfg->can_stall && | 2784 | common->can_stall = cfg->can_stall && |
2700 | !(gadget_is_sh(fsg->gadget) || gadget_is_at91(fsg->gadget)); | 2785 | !(gadget_is_sh(common->gadget) || |
2786 | gadget_is_at91(common->gadget)); | ||
2701 | 2787 | ||
2702 | 2788 | ||
2703 | common->thread_name = OR(cfg->thread_name, "file-storage"); | 2789 | spin_lock_init(&common->lock); |
2704 | kref_init(&common->ref); | 2790 | kref_init(&common->ref); |
2791 | |||
2792 | |||
2793 | /* Tell the thread to start working */ | ||
2794 | common->thread_task = | ||
2795 | kthread_create(fsg_main_thread, common, | ||
2796 | OR(cfg->thread_name, "file-storage")); | ||
2797 | if (IS_ERR(common->thread_task)) { | ||
2798 | rc = PTR_ERR(common->thread_task); | ||
2799 | goto error_release; | ||
2800 | } | ||
2801 | init_completion(&common->thread_notifier); | ||
2705 | #undef OR | 2802 | #undef OR |
2706 | 2803 | ||
2707 | 2804 | ||
@@ -2731,15 +2828,21 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, | |||
2731 | } | 2828 | } |
2732 | kfree(pathbuf); | 2829 | kfree(pathbuf); |
2733 | 2830 | ||
2831 | DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task)); | ||
2832 | |||
2833 | wake_up_process(common->thread_task); | ||
2834 | |||
2734 | return common; | 2835 | return common; |
2735 | 2836 | ||
2736 | 2837 | ||
2737 | error_luns: | 2838 | error_luns: |
2738 | common->nluns = i + 1; | 2839 | common->nluns = i + 1; |
2739 | error_release: | 2840 | error_release: |
2841 | common->state = FSG_STATE_TERMINATED; /* The thread is dead */ | ||
2740 | /* Call fsg_common_release() directly, ref might be not | 2842 | /* Call fsg_common_release() directly, ref might be not |
2741 | * initialised */ | 2843 | * initialised */ |
2742 | fsg_common_release(&common->ref); | 2844 | fsg_common_release(&common->ref); |
2845 | complete(&common->thread_notifier); | ||
2743 | return ERR_PTR(rc); | 2846 | return ERR_PTR(rc); |
2744 | } | 2847 | } |
2745 | 2848 | ||
@@ -2751,6 +2854,15 @@ static void fsg_common_release(struct kref *ref) | |||
2751 | unsigned i = common->nluns; | 2854 | unsigned i = common->nluns; |
2752 | struct fsg_lun *lun = common->luns; | 2855 | struct fsg_lun *lun = common->luns; |
2753 | 2856 | ||
2857 | /* If the thread isn't already dead, tell it to exit now */ | ||
2858 | if (common->state != FSG_STATE_TERMINATED) { | ||
2859 | raise_exception(common, FSG_STATE_EXIT); | ||
2860 | wait_for_completion(&common->thread_notifier); | ||
2861 | |||
2862 | /* The cleanup routine waits for this completion also */ | ||
2863 | complete(&common->thread_notifier); | ||
2864 | } | ||
2865 | |||
2754 | /* Beware tempting for -> do-while optimization: when in error | 2866 | /* Beware tempting for -> do-while optimization: when in error |
2755 | * recovery nluns may be zero. */ | 2867 | * recovery nluns may be zero. */ |
2756 | 2868 | ||
@@ -2775,17 +2887,6 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f) | |||
2775 | struct fsg_dev *fsg = fsg_from_func(f); | 2887 | struct fsg_dev *fsg = fsg_from_func(f); |
2776 | 2888 | ||
2777 | DBG(fsg, "unbind\n"); | 2889 | DBG(fsg, "unbind\n"); |
2778 | clear_bit(REGISTERED, &fsg->atomic_bitflags); | ||
2779 | |||
2780 | /* If the thread isn't already dead, tell it to exit now */ | ||
2781 | if (fsg->state != FSG_STATE_TERMINATED) { | ||
2782 | raise_exception(fsg, FSG_STATE_EXIT); | ||
2783 | wait_for_completion(&fsg->thread_notifier); | ||
2784 | |||
2785 | /* The cleanup routine waits for this completion also */ | ||
2786 | complete(&fsg->thread_notifier); | ||
2787 | } | ||
2788 | |||
2789 | fsg_common_put(fsg->common); | 2890 | fsg_common_put(fsg->common); |
2790 | kfree(fsg); | 2891 | kfree(fsg); |
2791 | } | 2892 | } |
@@ -2800,8 +2901,6 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f) | |||
2800 | struct usb_ep *ep; | 2901 | struct usb_ep *ep; |
2801 | 2902 | ||
2802 | fsg->gadget = gadget; | 2903 | fsg->gadget = gadget; |
2803 | fsg->ep0 = gadget->ep0; | ||
2804 | fsg->ep0req = c->cdev->req; | ||
2805 | 2904 | ||
2806 | /* New interface */ | 2905 | /* New interface */ |
2807 | i = usb_interface_id(c, f); | 2906 | i = usb_interface_id(c, f); |
@@ -2814,13 +2913,13 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f) | |||
2814 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); | 2913 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); |
2815 | if (!ep) | 2914 | if (!ep) |
2816 | goto autoconf_fail; | 2915 | goto autoconf_fail; |
2817 | ep->driver_data = fsg; /* claim the endpoint */ | 2916 | ep->driver_data = fsg->common; /* claim the endpoint */ |
2818 | fsg->bulk_in = ep; | 2917 | fsg->bulk_in = ep; |
2819 | 2918 | ||
2820 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); | 2919 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); |
2821 | if (!ep) | 2920 | if (!ep) |
2822 | goto autoconf_fail; | 2921 | goto autoconf_fail; |
2823 | ep->driver_data = fsg; /* claim the endpoint */ | 2922 | ep->driver_data = fsg->common; /* claim the endpoint */ |
2824 | fsg->bulk_out = ep; | 2923 | fsg->bulk_out = ep; |
2825 | 2924 | ||
2826 | if (gadget_is_dualspeed(gadget)) { | 2925 | if (gadget_is_dualspeed(gadget)) { |
@@ -2832,40 +2931,12 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f) | |||
2832 | f->hs_descriptors = fsg_hs_function; | 2931 | f->hs_descriptors = fsg_hs_function; |
2833 | } | 2932 | } |
2834 | 2933 | ||
2835 | |||
2836 | /* maybe allocate device-global string IDs, and patch descriptors */ | ||
2837 | if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { | ||
2838 | i = usb_string_id(c->cdev); | ||
2839 | if (i < 0) | ||
2840 | return i; | ||
2841 | fsg_strings[FSG_STRING_INTERFACE].id = i; | ||
2842 | fsg_intf_desc.iInterface = i; | ||
2843 | } | ||
2844 | |||
2845 | |||
2846 | fsg->thread_task = kthread_create(fsg_main_thread, fsg, | ||
2847 | fsg->common->thread_name); | ||
2848 | if (IS_ERR(fsg->thread_task)) { | ||
2849 | rc = PTR_ERR(fsg->thread_task); | ||
2850 | goto out; | ||
2851 | } | ||
2852 | |||
2853 | DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task)); | ||
2854 | |||
2855 | set_bit(REGISTERED, &fsg->atomic_bitflags); | ||
2856 | |||
2857 | /* Tell the thread to start working */ | ||
2858 | wake_up_process(fsg->thread_task); | ||
2859 | return 0; | 2934 | return 0; |
2860 | 2935 | ||
2861 | autoconf_fail: | 2936 | autoconf_fail: |
2862 | ERROR(fsg, "unable to autoconfigure all endpoints\n"); | 2937 | ERROR(fsg, "unable to autoconfigure all endpoints\n"); |
2863 | rc = -ENOTSUPP; | 2938 | rc = -ENOTSUPP; |
2864 | |||
2865 | out: | ||
2866 | fsg->state = FSG_STATE_TERMINATED; /* The thread is dead */ | ||
2867 | fsg_unbind(c, f); | 2939 | fsg_unbind(c, f); |
2868 | complete(&fsg->thread_notifier); | ||
2869 | return rc; | 2940 | return rc; |
2870 | } | 2941 | } |
2871 | 2942 | ||
@@ -2888,10 +2959,6 @@ static int fsg_add(struct usb_composite_dev *cdev, | |||
2888 | if (unlikely(!fsg)) | 2959 | if (unlikely(!fsg)) |
2889 | return -ENOMEM; | 2960 | return -ENOMEM; |
2890 | 2961 | ||
2891 | spin_lock_init(&fsg->lock); | ||
2892 | init_completion(&fsg->thread_notifier); | ||
2893 | |||
2894 | fsg->cdev = cdev; | ||
2895 | fsg->function.name = FSG_DRIVER_DESC; | 2962 | fsg->function.name = FSG_DRIVER_DESC; |
2896 | fsg->function.strings = fsg_strings_array; | 2963 | fsg->function.strings = fsg_strings_array; |
2897 | fsg->function.descriptors = fsg_fs_function; | 2964 | fsg->function.descriptors = fsg_fs_function; |
@@ -2907,7 +2974,6 @@ static int fsg_add(struct usb_composite_dev *cdev, | |||
2907 | * from this function. So instead of incrementing counter now | 2974 | * from this function. So instead of incrementing counter now |
2908 | * and decrement in error recovery we increment it only when | 2975 | * and decrement in error recovery we increment it only when |
2909 | * call to usb_add_function() was successful. */ | 2976 | * call to usb_add_function() was successful. */ |
2910 | fsg->can_stall = common->can_stall; | ||
2911 | 2977 | ||
2912 | rc = usb_add_function(c, &fsg->function); | 2978 | rc = usb_add_function(c, &fsg->function); |
2913 | 2979 | ||