⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 usb driver for 2.6.17
💻 C
📖 第 1 页 / 共 2 页
字号:
		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	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#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	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 endpoints we need.	 * We are expecting a minimum of 2 endpoints - in and out (bulk).	 * An optional interrupt is OK (necessary for CBI protocol).	 * We will ignore any others.	 */	for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {		ep = &altsetting->endpoint[i].desc;		/* Is it a BULK endpoint? */		if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)				== USB_ENDPOINT_XFER_BULK) {			/* BULK in or out? */			if (ep->bEndpointAddress & USB_DIR_IN)				ep_in = ep;			else				ep_out = ep;		}		/* Is it an interrupt endpoint? */		else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)				== USB_ENDPOINT_XFER_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){	/* Prevent new USB transfers, stop the current command, and	 * interrupt a SCSI-scan or device-reset delay */	set_bit(US_FLIDX_DISCONNECTING, &us->flags);	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. */	/* Wait for the current command to finish, then remove the host */	mutex_lock(&us->dev_mutex);	mutex_unlock(&us->dev_mutex);	/* 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. */	if (us->srb) {		us->srb->result = DID_NO_CONNECT << 16;		scsi_lock(us_to_host(us));		us->srb->scsi_done(us->srb);		us->srb = NULL;		scsi_unlock(us_to_host(us));	}	/* Now we own no commands so it's safe to remove the SCSI host */	scsi_remove_host(us_to_host(us));}/* 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;	printk(KERN_DEBUG		"usb-storage: device found at %d\n", us->pusb_dev->devnum);	/* Wait for the timeout to expire or for a disconnect */	if (delay_use > 0) {		printk(KERN_DEBUG "usb-storage: waiting for device "				"to settle before scanning\n");retry:		wait_event_interruptible_timeout(us->delay_wait,				test_bit(US_FLIDX_DISCONNECTING, &us->flags),				delay_use * HZ);		if (try_to_freeze())			goto retry;	}	/* If the device is still connected, perform the scanning */	if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {		/* 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);			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);}/* 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.	 */	get_device_info(us, id);	/* 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;	/* 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	.id_table =	storage_usb_ids,};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");		usb_usual_set_present(USB_US_TYPE_STOR);	}	return retval;}static void __exit usb_stor_exit(void){	US_DEBUGP("usb_stor_exit() called\n");	/* 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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -