/* Driver for USB Mass Storage compliant devices
 *
 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
 *
 * Current development and maintenance by:
 *   (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
 *
 * Developed with the assistance of:
 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
 *   (c) 2003 Alan Stern (stern@rowland.harvard.edu)
 *
 * Initial work by:
 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 *
 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
 *   (c) 2000 Yggdrasil Computing, Inc.
 *
 * This driver is based on the 'USB Mass Storage Class' document. This
 * describes in detail the protocol used to communicate with such
 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 * mind when they created this document.  The commands are all very
 * similar to commands in the SCSI-II and ATAPI specifications.
 *
 * It is important to note that in a number of cases this class
 * exhibits class-specific exemptions from the USB specification.
 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 * that they are used to communicate wait, failed and OK on commands.
 *
 * Also, for certain devices, the interrupt endpoint is used to convey
 * status of a command.
 *
 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
 * information about this driver.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/freezer.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
#include <linux/utsname.h>
/* 2008.7.22 Sony Corporation [PROC_ACCESS] ADD: 64bit integer arithmetic functions. */
#include <linux/calc64.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>

#include "usb.h"
#include "scsiglue.h"
#include "transport.h"
#include "protocol.h"
#include "debug.h"
#include "initializers.h"

#ifdef CONFIG_USB_STORAGE_USBAT
#include "shuttle_usbat.h"
#endif
#ifdef CONFIG_USB_STORAGE_SDDR09
#include "sddr09.h"
#endif
#ifdef CONFIG_USB_STORAGE_SDDR55
#include "sddr55.h"
#endif
#ifdef CONFIG_USB_STORAGE_DPCM
#include "dpcm.h"
#endif
#ifdef CONFIG_USB_STORAGE_FREECOM
#include "freecom.h"
#endif
#ifdef CONFIG_USB_STORAGE_ISD200
#include "isd200.h"
#endif
#ifdef CONFIG_USB_STORAGE_DATAFAB
#include "datafab.h"
#endif
#ifdef CONFIG_USB_STORAGE_JUMPSHOT
#include "jumpshot.h"
#endif
#ifdef CONFIG_USB_STORAGE_ONETOUCH
#include "onetouch.h"
#endif
#ifdef CONFIG_USB_STORAGE_ALAUDA
#include "alauda.h"
#endif
#ifdef CONFIG_USB_STORAGE_KARMA
#include "karma.h"
#endif

/* Some informational data */
MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
MODULE_LICENSE("GPL");

/* 2008.1.7 Sony Corporation [PROC_ACCESS] NOTE: Replaced /sys/modules/usb-storage/\
   parameters/delay_use into /proc/drivers/usb-storage/timeout/connect_wait
*/
static unsigned int delay_use = 5;
module_param(delay_use, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");


/* These are used to make sure the module doesn't unload before all the
 * threads have exited.
 */
static atomic_t total_threads = ATOMIC_INIT(0);
static DECLARE_COMPLETION(threads_gone);


/*
 * The entries in this table correspond, line for line,
 * with the entries of us_unusual_dev_list[].
 */
#ifndef CONFIG_USB_LIBUSUAL

#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
		    vendorName, productName,useProtocol, useTransport, \
		    initFunction, flags) \
{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
  .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }

#define USUAL_DEV(useProto, useTrans, useType) \
{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
  .driver_info = (USB_US_TYPE_STOR<<24) }

static struct usb_device_id storage_usb_ids [] = {

#	include "unusual_devs.h"
#undef UNUSUAL_DEV
#undef USUAL_DEV
	/* Terminating entry */
	{ }
};

MODULE_DEVICE_TABLE (usb, storage_usb_ids);
#endif /* CONFIG_USB_LIBUSUAL */

/* This is the list of devices we recognize, along with their flag data */

/* The vendor name should be kept at eight characters or less, and
 * the product name should be kept at 16 characters or less. If a device
 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
 * normally generated by a device thorugh the INQUIRY response will be
 * taken from this list, and this is the reason for the above size
 * restriction. However, if the flag is not present, then you
 * are free to use as many characters as you like.
 */

#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
		    vendor_name, product_name, use_protocol, use_transport, \
		    init_function, Flags) \
{ \
	.vendorName = vendor_name,	\
	.productName = product_name,	\
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
	.initFunction = init_function,	\
}

#define USUAL_DEV(use_protocol, use_transport, use_type) \
{ \
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
}

static struct us_unusual_dev us_unusual_dev_list[] = {
#	include "unusual_devs.h" 
#	undef UNUSUAL_DEV
#	undef USUAL_DEV

	/* Terminating entry */
	{ NULL }
};


#ifdef CONFIG_PM	/* Minimal support for suspend and resume */

static int storage_suspend(struct usb_interface *iface, pm_message_t message)
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Wait until no command is running */
	mutex_lock(&us->dev_mutex);

	US_DEBUGP("%s\n", __FUNCTION__);
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_SUSPEND);
	iface->dev.power.power_state.event = message.event;

	/* When runtime PM is working, we'll set a flag to indicate
	 * whether we should autoresume when a SCSI request arrives. */

	mutex_unlock(&us->dev_mutex);
	return 0;
}

static int storage_resume(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

	mutex_lock(&us->dev_mutex);

	US_DEBUGP("%s\n", __FUNCTION__);
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_RESUME);
	iface->dev.power.power_state.event = PM_EVENT_ON;

	mutex_unlock(&us->dev_mutex);
	return 0;
}

#endif /* CONFIG_PM */

/*
 * The next two routines get called just before and just after
 * a USB port reset, whether from this driver or a different one.
 */

static void storage_pre_reset(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

	US_DEBUGP("%s\n", __FUNCTION__);

	/* Make sure no command runs during the reset */
	mutex_lock(&us->dev_mutex);
}

static void storage_post_reset(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

	US_DEBUGP("%s\n", __FUNCTION__);

	/* Report the reset to the SCSI core */
	scsi_lock(us_to_host(us));
	usb_stor_report_bus_reset(us);
	scsi_unlock(us_to_host(us));

	/* FIXME: Notify the subdrivers that they need to reinitialize
	 * the device */
	mutex_unlock(&us->dev_mutex);
}

/*
 * fill_inquiry_response takes an unsigned char array (which must
 * be at least 36 characters) and populates the vendor name,
 * product name, and revision fields. Then the array is copied
 * into the SCSI command's response buffer (oddly enough
 * called request_buffer). data_len contains the length of the
 * data array, which again must be at least 36.
 */

void fill_inquiry_response(struct us_data *us, unsigned char *data,
		unsigned int data_len)
{
	if (data_len<36) // You lose.
		return;

	if(data[0]&0x20) { /* USB device currently not connected. Return
			      peripheral qualifier 001b ("...however, the
			      physical device is not currently connected
			      to this logical unit") and leave vendor and
			      product identification empty. ("If the target
			      does store some of the INQUIRY data on the
			      device, it may return zeros or ASCII spaces 
			      (20h) in those fields until the data is
			      available from the device."). */
		memset(data+8,0,28);
	} else {
		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
		memcpy(data+8, us->unusual_dev->vendorName, 
			strlen(us->unusual_dev->vendorName) > 8 ? 8 :
			strlen(us->unusual_dev->vendorName));
		memcpy(data+16, us->unusual_dev->productName, 
			strlen(us->unusual_dev->productName) > 16 ? 16 :
			strlen(us->unusual_dev->productName));
		data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
		data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
		data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
		data[35] = 0x30 + ((bcdDevice) & 0x0F);
	}

	usb_stor_set_xfer_buf(data, data_len, us->srb);
}

static int usb_stor_control_thread(void * __us)
{
	struct us_data *us = (struct us_data *)__us;
	struct Scsi_Host *host = us_to_host(us);

	current->flags |= PF_NOFREEZE;

	for(;;) {
		US_DEBUGP("*** thread sleeping.\n");
		if(down_interruptible(&us->sema))
			break;
			
		US_DEBUGP("*** thread awakened.\n");

		/* lock the device pointers */
		mutex_lock(&(us->dev_mutex));

		/* if the device has disconnected, we are free to exit */
		if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
			US_DEBUGP("-- exiting\n");
			mutex_unlock(&us->dev_mutex);
			break;
		}

		/* lock access to the state */
		scsi_lock(host);

		/* has the command timed out *already* ? */
		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
			us->srb->result = DID_ABORT << 16;
			goto SkipForAbort;
		}

		scsi_unlock(host);

		/* reject the command if the direction indicator 
		 * is UNKNOWN
		 */
		if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
			US_DEBUGP("UNKNOWN data direction\n");
			us->srb->result = DID_ERROR << 16;
		}

		/* reject if target != 0 or if LUN is higher than
		 * the maximum known LUN
		 */
		else if (us->srb->device->id && 
				!(us->flags & US_FL_SCM_MULT_TARG)) {
			US_DEBUGP("Bad target number (%d:%d)\n",
				  us->srb->device->id, us->srb->device->lun);
			us->srb->result = DID_BAD_TARGET << 16;
		}

		else if (us->srb->device->lun > us->max_lun) {
			US_DEBUGP("Bad LUN (%d:%d)\n",
				  us->srb->device->id, us->srb->device->lun);
			us->srb->result = DID_BAD_TARGET << 16;
		}

		/* Handle those devices which need us to fake 
		 * their inquiry data */
		else if ((us->srb->cmnd[0] == INQUIRY) &&
			    (us->flags & US_FL_FIX_INQUIRY)) {
			unsigned char data_ptr[36] = {
			    0x00, 0x80, 0x02, 0x02,
			    0x1F, 0x00, 0x00, 0x00};

			US_DEBUGP("Faking INQUIRY command\n");
			fill_inquiry_response(us, data_ptr, 36);
			us->srb->result = SAM_STAT_GOOD;
		}

		/* we've got a command, let's do it! */
		else {
			US_DEBUG(usb_stor_show_command(us->srb));
			us->proto_handler(us->srb, us);
		}

		/* lock access to the state */
		scsi_lock(host);

		/* did the command already complete because of a disconnect? */
		if (!us->srb)
			;		/* nothing to do */

		/* indicate that the command is done */
		else if (us->srb->result != DID_ABORT << 16) {
			US_DEBUGP("scsi cmd done, result=0x%x\n", 
				   us->srb->result);
			us->srb->scsi_done(us->srb);
		} else {
SkipForAbort:
			US_DEBUGP("scsi command aborted\n");
		}

		/* If an abort request was received we need to signal that
		 * the abort has finished.  The proper test for this is
		 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
		 * the timeout might have occurred after the command had
		 * already completed with a different result code. */
		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
			complete(&(us->notify));

			/* Allow USB transfers to resume */
			clear_bit(US_FLIDX_ABORTING, &us->flags);
			clear_bit(US_FLIDX_TIMED_OUT, &us->flags);
		}

		/* finished working on this command */
		us->srb = NULL;
		scsi_unlock(host);

		/* unlock the device pointers */
		mutex_unlock(&us->dev_mutex);
	} /* for (;;) */

	scsi_host_put(host);

	/* notify the exit routine that we're actually exiting now 
	 *
	 * complete()/wait_for_completion() is similar to up()/down(),
	 * except that complete() is safe in the case where the structure
	 * is getting deleted in a parallel mode of execution (i.e. just
	 * after the down() -- that's necessary for the thread-shutdown
	 * case.
	 *
	 * complete_and_exit() goes even further than this -- it is safe in
	 * the case that the thread of the caller is going away (not just
	 * the structure) -- this is necessary for the module-remove case.
	 * This is important in preemption kernels, which transfer the flow
	 * of execution immediately upon a complete().
	 */
	complete_and_exit(&threads_gone, 0);
}	

/***********************************************************************
 * Device probing and disconnecting
 ***********************************************************************/

/* Associate our private data with the USB device */
static int associate_dev(struct us_data *us, struct usb_interface *intf)
{
	US_DEBUGP("-- %s\n", __FUNCTION__);

	/* Fill in the device-related fields */
	us->pusb_dev = interface_to_usbdev(intf);
	us->pusb_intf = intf;
	us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
	US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
			le16_to_cpu(us->pusb_dev->descriptor.idVendor),
			le16_to_cpu(us->pusb_dev->descriptor.idProduct),
			le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
	US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
			intf->cur_altsetting->desc.bInterfaceSubClass,
			intf->cur_altsetting->desc.bInterfaceProtocol);

	/* Store our private data in the interface */
	usb_set_intfdata(intf, us);

	/* Allocate the device-related DMA-mapped buffers */
	us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
			GFP_KERNEL, &us->cr_dma);
	if (!us->cr) {
		US_DEBUGP("usb_ctrlrequest allocation failed\n");
		return -ENOMEM;
	}

	us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
			GFP_KERNEL, &us->iobuf_dma);
	if (!us->iobuf) {
		US_DEBUGP("I/O buffer allocation failed\n");
		return -ENOMEM;
	}

	us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
	if (!us->sensebuf) {
		US_DEBUGP("Sense buffer allocation failed\n");
		return -ENOMEM;
	}
	return 0;
}

/* Find an unusual_dev descriptor (always succeeds in the current code) */
static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
{
	const int id_index = id - storage_usb_ids;
	return &us_unusual_dev_list[id_index];
}

/* Get the unusual_devs entries and the string descriptors */
static int get_device_info(struct us_data *us, const struct usb_device_id *id)
{
	struct usb_device *dev = us->pusb_dev;
	struct usb_interface_descriptor *idesc =
		&us->pusb_intf->cur_altsetting->desc;
	struct us_unusual_dev *unusual_dev = find_unusual(id);

	/* Store the entries */
	us->unusual_dev = unusual_dev;
	us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
			idesc->bInterfaceSubClass :
			unusual_dev->useProtocol;
	us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
			idesc->bInterfaceProtocol :
			unusual_dev->useTransport;
	us->flags = USB_US_ORIG_FLAGS(id->driver_info);

	if (us->flags & US_FL_IGNORE_DEVICE) {
		printk(KERN_INFO USB_STORAGE "device ignored\n");
		return -ENODEV;
	}

	/*
	 * This flag is only needed when we're in high-speed, so let's
	 * disable it if we're in full-speed
	 */
	if (dev->speed != USB_SPEED_HIGH)
		us->flags &= ~US_FL_GO_SLOW;

	/* Log a message if a non-generic unusual_dev entry contains an
	 * unnecessary subclass or protocol override.  This may stimulate
	 * reports from users that will help us remove unneeded entries
	 * from the unusual_devs.h table.
	 */
	if (id->idVendor || id->idProduct) {
		static const char *msgs[3] = {
			"an unneeded SubClass entry",
			"an unneeded Protocol entry",
			"unneeded SubClass and Protocol entries"};
		struct usb_device_descriptor *ddesc = &dev->descriptor;
		int msg = -1;

		if (unusual_dev->useProtocol != US_SC_DEVICE &&
			us->subclass == idesc->bInterfaceSubClass)
			msg += 1;
		if (unusual_dev->useTransport != US_PR_DEVICE &&
			us->protocol == idesc->bInterfaceProtocol)
			msg += 2;
		if (msg >= 0 && !(us->flags & US_FL_NEED_OVERRIDE))
			printk(KERN_NOTICE USB_STORAGE "This device "
				"(%04x,%04x,%04x S %02x P %02x)"
				" has %s in unusual_devs.h (kernel"
				" %s)\n"
				"   Please send a copy of this message to "
				"<linux-usb-devel@lists.sourceforge.net>\n",
				le16_to_cpu(ddesc->idVendor),
				le16_to_cpu(ddesc->idProduct),
				le16_to_cpu(ddesc->bcdDevice),
				idesc->bInterfaceSubClass,
				idesc->bInterfaceProtocol,
				msgs[msg],
				utsname()->release);
	}

	return 0;
}

/* Get the transport settings */
static int get_transport(struct us_data *us)
{
	switch (us->protocol) {
	case US_PR_CB:
		us->transport_name = "Control/Bulk";
		us->transport = usb_stor_CB_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

	case US_PR_CBI:
		us->transport_name = "Control/Bulk/Interrupt";
		us->transport = usb_stor_CBI_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

	case US_PR_BULK:
		us->transport_name = "Bulk";
		us->transport = usb_stor_Bulk_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		break;

#ifdef CONFIG_USB_STORAGE_USBAT
	case US_PR_USBAT:
		us->transport_name = "Shuttle USBAT";
		us->transport = usbat_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_SDDR09
	case US_PR_EUSB_SDDR09:
		us->transport_name = "EUSB/SDDR09";
		us->transport = sddr09_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_SDDR55
	case US_PR_SDDR55:
		us->transport_name = "SDDR55";
		us->transport = sddr55_transport;
		us->transport_reset = sddr55_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_DPCM
	case US_PR_DPCM_USB:
		us->transport_name = "Control/Bulk-EUSB/SDDR09";
		us->transport = dpcm_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_FREECOM
	case US_PR_FREECOM:
		us->transport_name = "Freecom";
		us->transport = freecom_transport;
		us->transport_reset = usb_stor_freecom_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_DATAFAB
	case US_PR_DATAFAB:
		us->transport_name  = "Datafab Bulk-Only";
		us->transport = datafab_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_JUMPSHOT
	case US_PR_JUMPSHOT:
		us->transport_name  = "Lexar Jumpshot Control/Bulk";
		us->transport = jumpshot_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_ALAUDA
	case US_PR_ALAUDA:
		us->transport_name  = "Alauda Control/Bulk";
		us->transport = alauda_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_KARMA
	case US_PR_KARMA:
		us->transport_name = "Rio Karma/Bulk";
		us->transport = rio_karma_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		break;
#endif

	default:
		return -EIO;
	}
	US_DEBUGP("Transport: %s\n", us->transport_name);

	/* fix for single-lun devices */
	if (us->flags & US_FL_SINGLE_LUN)
		us->max_lun = 0;
	return 0;
}

/* Get the protocol settings */
static int get_protocol(struct us_data *us)
{
	switch (us->subclass) {
	case US_SC_RBC:
		us->protocol_name = "Reduced Block Commands (RBC)";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

	case US_SC_8020:
		us->protocol_name = "8020i";
		us->proto_handler = usb_stor_ATAPI_command;
		us->max_lun = 0;
		break;

	case US_SC_QIC:
		us->protocol_name = "QIC-157";
		us->proto_handler = usb_stor_qic157_command;
		us->max_lun = 0;
		break;

	case US_SC_8070:
		us->protocol_name = "8070i";
		us->proto_handler = usb_stor_ATAPI_command;
		us->max_lun = 0;
		break;

	case US_SC_SCSI:
		us->protocol_name = "Transparent SCSI";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

	case US_SC_UFI:
		us->protocol_name = "Uniform Floppy Interface (UFI)";
		us->proto_handler = usb_stor_ufi_command;
		break;

#ifdef CONFIG_USB_STORAGE_ISD200
	case US_SC_ISD200:
		us->protocol_name = "ISD200 ATA/ATAPI";
		us->proto_handler = isd200_ata_command;
		break;
#endif

	default:
		return -EIO;
	}
	US_DEBUGP("Protocol: %s\n", us->protocol_name);
	return 0;
}

/* Get the pipe settings */
static int get_pipes(struct us_data *us)
{
	struct usb_host_interface *altsetting =
		us->pusb_intf->cur_altsetting;
	int i;
	struct usb_endpoint_descriptor *ep;
	struct usb_endpoint_descriptor *ep_in = NULL;
	struct usb_endpoint_descriptor *ep_out = NULL;
	struct usb_endpoint_descriptor *ep_int = NULL;

	/*
	 * Find the first endpoint of each type we need.
	 * We are expecting a minimum of 2 endpoints - in and out (bulk).
	 * An optional interrupt-in is OK (necessary for CBI protocol).
	 * We will ignore any others.
	 */
	for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
		ep = &altsetting->endpoint[i].desc;

		if (usb_endpoint_xfer_bulk(ep)) {
			if (usb_endpoint_dir_in(ep)) {
				if (!ep_in)
					ep_in = ep;
			} else {
				if (!ep_out)
					ep_out = ep;
			}
		}

		else if (usb_endpoint_is_int_in(ep)) {
			if (!ep_int)
				ep_int = ep;
		}
	}

	if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
		US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
		return -EIO;
	}

	/* Calculate and store the pipe values */
	us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
	us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
	us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
		ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev, 
		ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	if (ep_int) {
		us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
			ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
		us->ep_bInterval = ep_int->bInterval;
	}
	return 0;
}

/* Initialize all the dynamic resources we need */
static int usb_stor_acquire_resources(struct us_data *us)
{
	int p;
	struct task_struct *th;

	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!us->current_urb) {
		US_DEBUGP("URB allocation failed\n");
		return -ENOMEM;
	}

	/* Just before we start our control thread, initialize
	 * the device if it needs initialization */
	if (us->unusual_dev->initFunction) {
		p = us->unusual_dev->initFunction(us);
		if (p)
			return p;
	}

	/* Start up our control thread */
	th = kthread_create(usb_stor_control_thread, us, "usb-storage");
	if (IS_ERR(th)) {
		printk(KERN_WARNING USB_STORAGE 
		       "Unable to start control thread\n");
		return PTR_ERR(th);
	}

	/* Take a reference to the host for the control thread and
	 * count it among all the threads we have launched.  Then
	 * start it up. */
	scsi_host_get(us_to_host(us));
	atomic_inc(&total_threads);
	wake_up_process(th);

	return 0;
}

/* Release all our dynamic resources */
static void usb_stor_release_resources(struct us_data *us)
{
	US_DEBUGP("-- %s\n", __FUNCTION__);

	/* Tell the control thread to exit.  The SCSI host must
	 * already have been removed so it won't try to queue
	 * any more commands.
	 */
	US_DEBUGP("-- sending exit command to thread\n");
	set_bit(US_FLIDX_DISCONNECTING, &us->flags);
	up(&us->sema);

	/* Call the destructor routine, if it exists */
	if (us->extra_destructor) {
		US_DEBUGP("-- calling extra_destructor()\n");
		us->extra_destructor(us->extra);
	}

	/* Free the extra data and the URB */
	kfree(us->extra);
	usb_free_urb(us->current_urb);
}

/* Dissociate from the USB device */
static void dissociate_dev(struct us_data *us)
{
	US_DEBUGP("-- %s\n", __FUNCTION__);

	kfree(us->sensebuf);

	/* Free the device-related DMA-mapped buffers */
	if (us->cr)
		usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
				us->cr_dma);
	if (us->iobuf)
		usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
				us->iobuf_dma);

	/* Remove our private data from the interface */
	usb_set_intfdata(us->pusb_intf, NULL);
}

/* First stage of disconnect processing: stop all commands and remove
 * the host */
static void quiesce_and_remove_host(struct us_data *us)
{
	struct Scsi_Host *host = us_to_host(us);

	/* Prevent new USB transfers, stop the current command, and
	 * interrupt a SCSI-scan or device-reset delay */
	scsi_lock(host);
	set_bit(US_FLIDX_DISCONNECTING, &us->flags);
	scsi_unlock(host);
	usb_stor_stop_transport(us);
	wake_up(&us->delay_wait);

	/* It doesn't matter if the SCSI-scanning thread is still running.
	 * The thread will exit when it sees the DISCONNECTING flag. */

	/* queuecommand won't accept any new commands and the control
	 * thread won't execute a previously-queued command.  If there
	 * is such a command pending, complete it with an error. */
	mutex_lock(&us->dev_mutex);
	if (us->srb) {
		us->srb->result = DID_NO_CONNECT << 16;
		scsi_lock(host);
		us->srb->scsi_done(us->srb);
		us->srb = NULL;
		scsi_unlock(host);
	}
	mutex_unlock(&us->dev_mutex);

	/* Now we own no commands so it's safe to remove the SCSI host */
	scsi_remove_host(host);
}

/* Second stage of disconnect processing: deallocate all resources */
static void release_everything(struct us_data *us)
{
	usb_stor_release_resources(us);
	dissociate_dev(us);

	/* Drop our reference to the host; the SCSI core will free it
	 * (and "us" along with it) when the refcount becomes 0. */
	scsi_host_put(us_to_host(us));
}

/* Thread to carry out delayed SCSI-device scanning */
static int usb_stor_scan_thread(void * __us)
{
	struct us_data *us = (struct us_data *)__us;
	unsigned long		wait_hz;

	US_DEBUGPX(KERN_INFO
		"usb-storage: %s: device found. devnum=%d.\n", __FUNCTION__, us->pusb_dev->devnum);

	/* 2008.1.10 Sony Corporation [PROC_ACCESS] Connection wait time from /proc values. */
	wait_hz=usb_stor_ms_to_hz(usb_storage_proc_entry_values.timeout.connect_wait);

	/* Wait for the timeout to expire or for a disconnect */
	if (wait_hz>0 /* delay_use > 0 */) {

		US_DEBUGPX(KERN_INFO "usb-storage: %s: waiting for device "
				"to settle before scanning. wait_hz=%lu.\n",__FUNCTION__,wait_hz);
retry:
		/* 2008.1.10 Sony Corporation [PROC_ACCESS] Connection wait time from /proc values. */
		wait_event_interruptible_timeout(us->delay_wait,
				test_bit(US_FLIDX_DISCONNECTING, &us->flags),
				wait_hz);
		if (try_to_freeze()) {
			/* may be suspended. */
			goto retry;
		}
	}

	US_DEBUGPX(KERN_INFO "%s: Go ahead. devnum=%d.\n", __FUNCTION__, us->pusb_dev->devnum);

	/* If the device is still connected, perform the scanning */
	if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
#if (defined(CONFIG_USB_STORAGE_REDUCE_SCAN))
		/* 2009.3.24 Sony Corporation [MOD_MAX_LUN] */
		struct Scsi_Host	*shost;
		shost=us_to_host(us);
#endif /* (defined(CONFIG_USB_STORAGE_REDUCE_SCAN)) */

		/* For bulk-only devices, determine the max LUN value */
		if (us->protocol == US_PR_BULK &&
				!(us->flags & US_FL_SINGLE_LUN)) {
			mutex_lock(&us->dev_mutex);
			us->max_lun = usb_stor_Bulk_max_lun(us);
#if (defined(CONFIG_USB_STORAGE_REDUCE_SCAN))
			/* Reflect max_lun to Scsi_Host. 
			   In this case, max_lun number is fixed to 1.
			   Correct max_lun number is following.
				shost->max_lun = us->max_lun+1;
			*/
			shost->max_lun = 1;
#endif /* (defined(CONFIG_USB_STORAGE_REDUCE_SCAN)) */
			mutex_unlock(&us->dev_mutex);
		}
		scsi_scan_host(us_to_host(us));
		printk(KERN_DEBUG "usb-storage: device scan complete\n");

		/* Should we unbind if no devices were detected? */
	}

	scsi_host_put(us_to_host(us));
	complete_and_exit(&threads_gone, 0);
}


#if (defined(CONFIG_USB_STORAGE_REDUCE_SCAN))
/* Maximum number of SCSI ID.
   NOTE: The value "1" is valid for SCSI bulk only protocol.
         If you wants support any other protocol, modify MAX_ID.
   NOTE: If gain max_id more than "1", may cause KERNEL PANIC
         in klist, kobject, and class_device_del. The Kernel 
         2.6.22.19 has broken sysfs and kernel object libs.
         Scanning invalid target ID or invalid LUN may corrupt
         kset and klist links.
*/
#define USB_STOR_X_BULK_ONLY_MAX_ID	(1)
#endif /* (defined(CONFIG_USB_STORAGE_REDUCE_SCAN)) */

/* Probe to see if we can drive a newly-connected USB device */
static int storage_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
	struct Scsi_Host *host;
	struct us_data *us;
	int result;
	struct task_struct *th;

	if (usb_usual_check_type(id, USB_US_TYPE_STOR))
		return -ENXIO;

	US_DEBUGP("USB Mass Storage device detected\n");

	/*
	 * Ask the SCSI layer to allocate a host structure, with extra
	 * space at the end for our private us_data structure.
	 */
	host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
	if (!host) {
		printk(KERN_WARNING USB_STORAGE
			"Unable to allocate the scsi host\n");
		return -ENOMEM;
	}

	us = host_to_us(host);
	memset(us, 0, sizeof(struct us_data));
	mutex_init(&(us->dev_mutex));
	init_MUTEX_LOCKED(&(us->sema));
	init_completion(&(us->notify));
	init_waitqueue_head(&us->delay_wait);

	/* Associate the us_data structure with the USB device */
	result = associate_dev(us, intf);
	if (result)
		goto BadDevice;

	/*
	 * Get the unusual_devs entries and the descriptors
	 *
	 * id_index is calculated in the declaration to be the index number
	 * of the match from the usb_device_id table, so we can find the
	 * corresponding entry in the private table.
	 */
	result = get_device_info(us, id);
	if (result)
		goto BadDevice;

#if (defined(CONFIG_USB_STORAGE_REDUCE_SCAN))
	/* 2009.3.24 Sony Corporation [MOD_MAX_ID] */
	if (us->protocol==US_PR_BULK) {
		/* Bulk only protocol. */
		/* Bulk only protocol has only scsi target ID {0}. */
		/* Reflect max_id to Scsi_Host. 
		   The collect max_id reduces redundant LUN scanning.
		*/
		host->max_id=USB_STOR_X_BULK_ONLY_MAX_ID;
		US_DEBUGP("NTFY: Set max_id. host=0x%p, max_id=%d.\n",host, host->max_id);
	}
#endif /* (defined(CONFIG_USB_STORAGE_REDUCE_SCAN)) */

	/* Get the transport, protocol, and pipe settings */
	result = get_transport(us);
	if (result)
		goto BadDevice;
	result = get_protocol(us);
	if (result)
		goto BadDevice;
	result = get_pipes(us);
	if (result)
		goto BadDevice;

	/* 2008.1.8 Sony Corporation [PROC_ACCESS] Add timer to cancel s/g transfer. */
	spin_lock_init(&(us->sg_timer_lock));
	init_timer(&(us->sg_timer));

	/* Acquire all the other resources and add the host */
	result = usb_stor_acquire_resources(us);
	if (result)
		goto BadDevice;
	result = scsi_add_host(host, &intf->dev);
	if (result) {
		printk(KERN_WARNING USB_STORAGE
			"Unable to add the scsi host\n");
		goto BadDevice;
	}

	/* Start up the thread for delayed SCSI-device scanning */
	th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
	if (IS_ERR(th)) {
		printk(KERN_WARNING USB_STORAGE 
		       "Unable to start the device-scanning thread\n");
		quiesce_and_remove_host(us);
		result = PTR_ERR(th);
		goto BadDevice;
	}

	/* Take a reference to the host for the scanning thread and
	 * count it among all the threads we have launched.  Then
	 * start it up. */
	scsi_host_get(us_to_host(us));
	atomic_inc(&total_threads);
	wake_up_process(th);

	return 0;

	/* We come here if there are any problems */
BadDevice:
	US_DEBUGP("storage_probe() failed\n");
	release_everything(us);
	return result;
}

/* Handle a disconnect event from the USB core */
static void storage_disconnect(struct usb_interface *intf)
{
	struct us_data *us = usb_get_intfdata(intf);

	US_DEBUGP("storage_disconnect() called\n");
	quiesce_and_remove_host(us);
	release_everything(us);
}

/***********************************************************************
 * Initialization and registration
 ***********************************************************************/

static struct usb_driver usb_storage_driver = {
	.name =		"usb-storage",
	.probe =	storage_probe,
	.disconnect =	storage_disconnect,
#ifdef CONFIG_PM
	.suspend =	storage_suspend,
	.resume =	storage_resume,
#endif
	.pre_reset =	storage_pre_reset,
	.post_reset =	storage_post_reset,
	.id_table =	storage_usb_ids,
};

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: dafault settings */
/* 2008.6.3 Sony Corporation Make shorter connect time. connect_wait=0 (old 5000). */
/* NOTE: Values are defaults in kernel 2.6.22.5. */
/* NOTE: Values are changed between kernel 2.6.11 and 2.6.22.
   mass_storage_reset  20 -> 5
   
*/
/* NOTE: connect_wait equal to 50mS reasons.
   (1) Submit control request in serial. If connect_wait equal to 0mS,
       control request "Get Max Lun" from usb-storage and "GET_DESCRIPTOR(interface-string)"
       from usb core are submitted simultaneously to HCD. Not well made HCD can't queue 
       simultaneous control request.
   (2) windows does.
*/
struct usb_storage_proc_entry_values	usb_storage_proc_entry_values={
	.timeout={
		.data_transfer=0,					/* Inf.	 at usb_stor_bulk_transfer_buf()	*/
		.clear_feature=3000,				/* 3 sec at usb_stor_clear_halt()			*/
		.get_max_lun=1000,					/* 1 sec at usb_stor_Bulk_max_lun()			*/
		.mass_storage_reset=5000,			/* 5 sec at usb_stor_reset_common()			*/
		.mass_storage_reset_wait=6000,		/* 6 sec at usb_stor_reset_common()			*/
		.control_message=0,					/* Inf. at usb_stor_ctrl_transfer(),		*/ 
											/*		   usb_stor_CBI_transport()			*/
											/*		   usb_stor_CB_transport()			*/
		.cb_adsc=0,							/* Inf.   NOT IMPLEMENTED					*/
		.cbi_adsc=0,						/* Inf.   NOT IMPLEMENTED					*/
		.cb_interrupt=0,					/* Inf.   NOT IMPLEMENTED					*/
		.cbi_interrupt=0,					/* Inf.   NOT IMPLEMENTED					*/
		.connect_wait=50					/* 50m sec at usb_stor_scan_thread()		*/
	}
};

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: pre define read access procedure */
int usb_stor_proc_entry_read_long
		(char *page, char **start, off_t off,
		 int count, int *eof, void *data);

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: pre define write access procedure */
int usb_stor_proc_entry_write_long
		(struct file *filp, const char __user *buff,
		 unsigned long len, void *data
		);

/* 2007.1.7 Sony Corporation [PROC_ACCESS] ADD: proc parametr entry template */
struct usb_storage_proc_entry	usb_storage_proc_entries[]={
	 {"data_transfer",&usb_storage_proc_entry_values.timeout.data_transfer
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"clear_feature",&usb_storage_proc_entry_values.timeout.clear_feature
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"get_max_lun",&usb_storage_proc_entry_values.timeout.get_max_lun
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"mass_storage_reset",&usb_storage_proc_entry_values.timeout.mass_storage_reset
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"mass_storage_reset_wait",&usb_storage_proc_entry_values.timeout.mass_storage_reset_wait
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"control_message",&usb_storage_proc_entry_values.timeout.control_message
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"cb_adsc",&usb_storage_proc_entry_values.timeout.cb_adsc
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"cbi_adsc",&usb_storage_proc_entry_values.timeout.cbi_adsc
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"cb_interrupt",&usb_storage_proc_entry_values.timeout.cb_interrupt
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"cbi_interrupt",&usb_storage_proc_entry_values.timeout.cbi_interrupt
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {"connect_wait",&usb_storage_proc_entry_values.timeout.connect_wait
			,usb_stor_proc_entry_read_long,usb_stor_proc_entry_write_long},
	 {/* END OF ENTRY */}
};

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: proc_dir_entry holder. */
/* procfs entry holder */
struct usb_storage_proc_fs				usb_storage_proc_fs;

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: read access procedure */
/*! proc entry read long call back function
*/
int usb_stor_proc_entry_read_long
		(char *page, char **start, off_t off,
		 int count, int *eof, void *data)
{
	char	buf[sizeof(long)*4];	/* NOTE: Keep buffer size >= (3*sizeof(long)+1). */
	int		l;

	US_DEBUGP("%s: Called.\n",__FUNCTION__);
	if (!eof) {
		/* Invalid argument */
		printk(KERN_WARNING "%s: NULL pointer eof.\n",__FUNCTION__);
		return(0);
	}
	if ((!page) || (!data)) {
		/* invalid scratch area or optional data pointer. read no string(stream). */
		printk(KERN_WARNING "%s: NULL pointer page or data. page=0x%p, data=0x%p.\n",__FUNCTION__,page,data);
		*eof=1;
		return(0);
	}
	/* print to scratch area */
	l=snprintf(buf,sizeof(buf),"%ld",*(long*)data);
	if (l>count) {
		/* too small page size. Will be it happen? */
		printk(KERN_WARNING "%s: Too small page size. l=%d, count=%d.\n",__FUNCTION__,l,count);
		*eof=1;
		return(0);
	}
	if (l<=off) {
		/* read pointer exceeds end of string(stream) */
		US_DEBUGP("%s: Offset exceeds end of string. l=%d, off=%ld.\n",__FUNCTION__,l,off);
		*eof=1;
		return(0);
	}
	/* copy string(stream) to scratch page. */
	memcpy(page,buf,l);
	if (start) {
		/* valid pointer */
		*start=page+off;
	}
	/* always reach to eof. */
	*eof=1;
	/* return length of read */
	US_DEBUGP("%s: Return string. return=%ld.\n",__FUNCTION__,l-off);
	return(l-off);
}

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: write access procedure */
/*! proc entry write long call back function
*/
int usb_stor_proc_entry_write_long
		(struct file *filp, const char __user *buff,
		 unsigned long len, void *data
		)
{
	/* NOTE: Keep buffer size >= 2+(3*sizeof(long)+2) 
		+'0'+'x'
		 ( 3 digit / byte ) * sizeof (long)
		+LF+\0 terminator
	*/
	char	work[sizeof(long)*4+2];

	US_DEBUGP("%s: Called.\n",__FUNCTION__);

	if ((!filp) || (!buff) || (!data)) {
		/* Invalid pointer. but returns written all. */
		printk(KERN_WARNING "%s: NULL pointer. filp=0x%p, buff=0x%p, data=0x%p.\n",__FUNCTION__,filp,buff,data);
		return(0 /* write failed */);
	}
	if ((sizeof(work)-1)<len) {
		/* Too long write data */
		printk(KERN_WARNING "%s: Too long write data. len=%ld.\n",__FUNCTION__,len);
		return(0 /* write failed */);
	}
	/* copy written string(stream) to work */
	if (copy_from_user(work,buff,len)!=0) {
		/* some bytes are not copied. */
		return(0 /* write failed */);
	}
	/* terminate */
	work[len]='\0';
	US_DEBUGP("%s: write string. work=\"%s\", len=%ld, data=0x%p.\n",__FUNCTION__,work,len,data);
	sscanf(work,"%li",(long*)data);	/* NOTE: li=signed long integer with radix prefix. */
	return(len);
}

#define	MS_MAX_IN_LONG	((LONG_MAX)/HZ)

/* 2008.1.7 Sony Corporation [PROC_ACCESS] ADD: dafault settings */
/* Convert milli second to HZ second. 
   HZ second is HZ/sec.
*/
int usb_stor_ms_to_hz(long milli_sec)
{
	if (milli_sec<=0) {
		/* means inf. or zero. */
		return(0);
	}

	if (milli_sec<=MS_MAX_IN_LONG) {
		/* Can calculate in long. */
		long		t;
		int			tmp;

		t=(milli_sec*HZ)/1000L;
		tmp=t;
		if (tmp<0) {
			tmp=0;
		}
		return(tmp);
	} else {
		/* Can not calculate in long. */
		long long	dividend;
		long		result;
		long		rem;
		int			tmp;

		rem=0;
		dividend=milli_sec;
		dividend*=HZ;

		result=div_long_long_rem_signed(dividend,1000L,&rem);
		tmp=result;
		if (tmp<0) {
			tmp=0;
		}
		return(tmp);
	}
	/* May not come here. */
	return(0);
}

/* 2008.1.7 Sony Corporation [PROC_ACCESS] add "Create proc entry" */
/*! Create proc access entries
*/
static void usb_stor_init_create_proc_entries(void)
{
	usb_storage_proc_fs_t		*pfs;
	unsigned					n;
	struct proc_dir_entry		*ent;
	struct proc_dir_entry		**ent_array;
	usb_storage_proc_entry_t	*ent_template;

	pfs=&usb_storage_proc_fs;
	memset(pfs,0,sizeof(*pfs));
	/* Create /proc/driver/usb-storage entry */
	pfs->proc_dir_usb_storage=proc_mkdir(usb_storage_driver.name, proc_root_driver);
	if (!(pfs->proc_dir_usb_storage)) {
		/* Can not create entry */
		printk(KERN_WARNING "%s: Can not create usb-storage directory.\n",__FUNCTION__);
		return;
	}
	/* create timeout sub directory */
	pfs->proc_dir_usb_storage_timeout=proc_mkdir(USB_STOR_PROC_SUBDIR_TIMEOUT,pfs->proc_dir_usb_storage);
	if (!(pfs->proc_dir_usb_storage_timeout)) {
		/* Can not create entry */
		printk(KERN_WARNING "%s: Can not create timeout directory.\n",__FUNCTION__);
		return;
	}
	/* count entries */
	ent_template=usb_storage_proc_entries;
	n=0;
	while ((ent_template->name)!=NULL) {
		/* Loop while scan valid template */
		ent_template++;
		n++;
	}
	US_DEBUGP("%s: Create paramenter entry. n=%d.\n",__FUNCTION__,n);
	if (n==0) {
		/* no need to create entries */
		return;
	}
	pfs->proc_entry_timeout_count=n;
	/* allocate pointer array to point entries */
	ent_array=kmalloc(sizeof(ent_array[0])*n, GFP_KERNEL);
	if (!(ent_array)) {
		/* Can not allocate array */
		printk(KERN_WARNING "%s: Can not allocate parameter entry array. \n",__FUNCTION__);
		return;
	}
	pfs->proc_entry_timeout=ent_array;
	/* clear array */
	memset(ent_array,0,sizeof(ent_array[0])*n);
	/* create parameter entries */
	ent_template=usb_storage_proc_entries;
	while ((n>0) && (ent_template->name!=NULL)) {
		ent=create_proc_entry(ent_template->name,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,pfs->proc_dir_usb_storage_timeout);
		if (ent==NULL) {
			/* can not create entry */
		printk(KERN_WARNING "%s: Can not create parameter entry. name=%s.\n",__FUNCTION__,ent_template->name);
			break;
		}
		*ent_array=ent;
		/* set entry data holder, read procedure, write procedure */
		ent->data=ent_template->data;
		ent->read_proc=ent_template->read_proc;
		ent->write_proc=ent_template->write_proc;
		ent_template++;
		ent_array++;
		n--;
	}
}

/* 2008.1.7 Sony Corporation [PROC_ACCESS] add "Destroy proc entry" */
/*! Destroy proc access entries
*/
static void usb_stor_exit_destroy_proc_entries(void)
{
	usb_storage_proc_fs_t		*pfs;
	unsigned					n;
	struct proc_dir_entry		**ent_array;
	usb_storage_proc_entry_t	*ent_template;

	pfs=&usb_storage_proc_fs;
	/* remove timeout parameter entry */
	n=pfs->proc_entry_timeout_count;
	ent_array=pfs->proc_entry_timeout;
	ent_template=usb_storage_proc_entries;
	if ((n>0) && (ent_array!=NULL)) {
		/* some parameter are created. */
		/* destroy parameter entries. NOTE: ignore errors. */
		while (n>0) {
			if (ent_template->name!=NULL) {
				remove_proc_entry(ent_template->name,pfs->proc_dir_usb_storage_timeout);
			}
			ent_template++;
			ent_array++;
			n--;
		}
	}
	/* free entry pointer array */
	kfree(pfs->proc_entry_timeout);
	/* remove timeout sub directory */
	if (pfs->proc_dir_usb_storage_timeout!=NULL) {
		/* created timeout sub directory */
		remove_proc_entry(USB_STOR_PROC_SUBDIR_TIMEOUT,pfs->proc_dir_usb_storage);
	}
	/* remove usb-storage sub directory */
	if (pfs->proc_dir_usb_storage!=NULL) {
		/* created usb-storage sub directory */
		remove_proc_entry(usb_storage_driver.name, proc_root_driver);
	}
}



static int __init usb_stor_init(void)
{
	int retval;
	printk(KERN_INFO "Initializing USB Mass Storage driver...\n");

	/* register the driver, return usb_register return code if error */
	retval = usb_register(&usb_storage_driver);
	if (retval == 0) {
		printk(KERN_INFO "USB Mass Storage support registered.\n");
		/* 2008.1.8 Sony Corporation [PROC_ACCESS] ADD: call create proc entry */
		usb_stor_init_create_proc_entries();
		usb_usual_set_present(USB_US_TYPE_STOR);
	}
	return retval;
}

static void __exit usb_stor_exit(void)
{
	US_DEBUGP("usb_stor_exit() called\n");

	/* 2008.1.8 Sony Corporation [PROC_ACCESS] ADD: call destory proc entry */
	usb_stor_exit_destroy_proc_entries();

	/* Deregister the driver
	 * This will cause disconnect() to be called for each
	 * attached unit
	 */
	US_DEBUGP("-- calling usb_deregister()\n");
	usb_deregister(&usb_storage_driver) ;

	/* Don't return until all of our control and scanning threads
	 * have exited.  Since each thread signals threads_gone as its
	 * last act, we have to call wait_for_completion the right number
	 * of times.
	 */
	while (atomic_read(&total_threads) > 0) {
		wait_for_completion(&threads_gone);
		atomic_dec(&total_threads);
	}

	usb_usual_clear_present(USB_US_TYPE_STOR);
}

module_init(usb_stor_init);
module_exit(usb_stor_exit);
