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

📄 usb.c

📁 底层驱动开发
💻 C
📖 第 1 页 / 共 3 页
字号:
		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;	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);	if (!us->current_urb) {		US_DEBUGP("URB allocation failed\n");		return -ENOMEM;	}	/* Lock the device while we carry out the next two operations */	down(&us->dev_semaphore);	/* For bulk-only devices, determine the max LUN value */	if (us->protocol == US_PR_BULK) {		p = usb_stor_Bulk_max_lun(us);		if (p < 0) {			up(&us->dev_semaphore);			return p;		}		us->max_lun = p;	}	/* Just before we start our control thread, initialize	 * the device if it needs initialization */	if (us->unusual_dev->initFunction)		us->unusual_dev->initFunction(us);	up(&us->dev_semaphore);	/* Start up our control thread */	p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);	if (p < 0) {		printk(KERN_WARNING USB_STORAGE 		       "Unable to start control thread\n");		return p;	}	us->pid = p;	atomic_inc(&total_threads);	/* Wait for the thread to start */	wait_for_completion(&(us->notify));	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__);	/* 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 */	down(&us->dev_semaphore);	up(&us->dev_semaphore);	/* 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;	/*	 * This thread doesn't need any user-level access,	 * so get rid of all our resources.	 */	lock_kernel();	daemonize("usb-stor-scan");	unlock_kernel();	/* Acquire a reference to the host, so it won't be deallocated	 * until we're ready to exit */	scsi_host_get(us_to_host(us));	/* Signal that we've started the thread */	complete(&(us->notify));	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)) {		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;	const int id_index = id - storage_usb_ids; 	int result;	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));	init_MUTEX(&(us->dev_semaphore));	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_index);#ifdef CONFIG_USB_STORAGE_SDDR09	if (us->protocol == US_PR_EUSB_SDDR09 ||			us->protocol == US_PR_DPCM_USB) {		/* set the configuration -- STALL is an acceptable response here */		if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {			US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev				->actconfig->desc.bConfigurationValue);			goto BadDevice;		}		result = usb_reset_configuration(us->pusb_dev);		US_DEBUGP("Result of usb_reset_configuration is %d\n", result);		if (result == -EPIPE) {			US_DEBUGP("-- stall on control interface\n");		} else if (result != 0) {			/* it's not a stall, but another error -- time to bail */			US_DEBUGP("-- Unknown error.  Rejecting device\n");			goto BadDevice;		}	}#endif	/* 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 */	result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);	if (result < 0) {		printk(KERN_WARNING USB_STORAGE 		       "Unable to start the device-scanning thread\n");		quiesce_and_remove_host(us);		goto BadDevice;	}	atomic_inc(&total_threads);	/* Wait for the thread to start */	wait_for_completion(&(us->notify));	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 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");	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);	}}module_init(usb_stor_init);module_exit(usb_stor_exit);

⌨️ 快捷键说明

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