//probe书中有教 static int our_probe(struct usb_interface *intf, const struct usb_device_id *id){ struct us_data *us; int result; //device struct device *dev; //scsihost struct Scsi_Host *host; //检测id是否符合 和intf struct us_unusual_dev *unusual_dev; unusual_dev=(id - usb_storage_usb_ids) + us_unusual_dev_list; if(usb_usual_check_type(id,USB_US_TYPE_STOR) || usb_usual_ignore_device(intf)) return -ENXIO; printk(KERN_ALERT "probe usb usb detected!\n"); //分配个host host = scsi_host_alloc(&usb_stor_host_template,sizeof(*us)); if(!host){ dev_warn(&intf->dev,"fail to allocate the scsi host\n"); return -ENOMEM; } //host中的一些初始化 host->max_cmd_len = 16; host-> sg_tablesize = usb_stor_sg_tablesize(intf); us= host_to_us(host);//us 作为host中 的us //分内存? memset(us,0,sizeof(struct us_data)); mutex_init(&(us->dev_mutex)); init_completion(&us->cmnd_ready); init_completion(&(us->notify)); init_waitqueue_head(&us->delay_wait); INIT_DELAYED_WORK(&us->scan_dwork,usb_stor_scan_dwork); result = associate_dev(us,intf); if(result) goto Bad; result = get_device_info(us,id,unusual_dev); if(result) goto Bad; //transport protocol get_transport(us); get_protocol(us); if(!us->transport ||!us->proto_handler){ result=-ENXIO; goto Bad; } printk(KERN_ALERT"Transport: %s\n",us->transport_name); printk(KERN_ALERT"Protocol: %s\n",us->transport_name); dev = &us->pusb_intf->dev; //设置max lun if(us->fflags & US_FL_SINGLE_LUN) us->max_lun =0; //endpoint get pipe result = get_pipes(us); if(result) goto Bad; //如果u盘前十个指令错误,重置 if (us->fflags & US_FL_INITIAL_READ10) set_bit(US_FLIDX_REDO_READ10, &us->dflags); //申请子资源,添加进host result=usb_stor_acquire_sesources(us); if(result) goto Bad; snprintf(us->scsi_name,sizeof(us->scsi_name),"our-usb-storage%s",dev_name(&us->pusb_intf->dev)); result= scsi_add_host(us_to_host(us),dev); if(result){ printk(KERN_ALERT"UNable to add the host\n"); goto Bad; } //scsi设备延时探测 usb_autopm_get_interface_no_resume(us->pusb_intf); set_bit(US_FLIDX_SCAN_PENDING,&us->dflags); if(delay_use>0) dev_dbg(dev,"waiting for device before scanning\n"); queue_delayed_work(system_freezable_wq,&us->scan_dwork,delay_use * HZ); return 0; Bad: printk(KERN_ALERT "probe false!\n"); release_everything(us); return result; }
static int rts51x_probe(struct usb_interface *intf, const struct usb_device_id *id) { struct Scsi_Host *host; struct rts51x_chip *chip; struct rts51x_usb *rts51x; int result; struct task_struct *th; RTS51X_DEBUGP("%s detected\n", RTS51X_NAME); rts51x = kzalloc(sizeof(struct rts51x_usb), GFP_KERNEL); if (!rts51x) { printk(KERN_WARNING RTS51X_TIP "Unable to allocate rts51x_usb\n"); return -ENOMEM; } host = scsi_host_alloc(&rts51x_host_template, sizeof(*chip)); if (!host) { printk(KERN_WARNING RTS51X_TIP "Unable to allocate the scsi host\n"); kfree(rts51x); return -ENOMEM; } host->max_cmd_len = 16; chip = host_to_rts51x(host); memset(chip, 0, sizeof(struct rts51x_chip)); chip->vendor_id = id->idVendor; chip->product_id = id->idProduct; mutex_init(&(rts51x->dev_mutex)); init_completion(&rts51x->cmnd_ready); init_completion(&rts51x->control_exit); init_completion(&rts51x->polling_exit); init_completion(&(rts51x->notify)); #ifdef SCSI_SCAN_DELAY init_waitqueue_head(&rts51x->delay_wait); init_completion(&rts51x->scanning_done); #endif chip->usb = rts51x; result = associate_dev(chip, intf); if (result) goto BadDevice; result = get_pipes(chip); if (result) goto BadDevice; result = rts51x_acquire_resources(chip); if (result) goto BadDevice; th = kthread_run(rts51x_control_thread, chip, RTS51X_CTL_THREAD); if (IS_ERR(th)) { printk(KERN_WARNING RTS51X_TIP "Unable to start control thread\n"); result = PTR_ERR(th); goto BadDevice; } rts51x->ctl_thread = th; result = scsi_add_host(rts51x_to_host(chip), &rts51x->pusb_intf->dev); if (result) { printk(KERN_WARNING RTS51X_TIP "Unable to add the scsi host\n"); goto BadDevice; } #ifdef SCSI_SCAN_DELAY th = kthread_create(rts51x_scan_thread, chip, RTS51X_SCAN_THREAD); if (IS_ERR(th)) { printk(KERN_WARNING RTS51X_TIP "Unable to start the device-scanning thread\n"); complete(&rts51x->scanning_done); quiesce_and_remove_host(chip); result = PTR_ERR(th); goto BadDevice; } wake_up_process(th); #else scsi_scan_host(rts51x_to_host(chip)); #endif th = kthread_run(rts51x_polling_thread, chip, RTS51X_POLLING_THREAD); if (IS_ERR(th)) { printk(KERN_WARNING RTS51X_TIP "Unable to start polling thread\n"); result = PTR_ERR(th); goto BadDevice; } rts51x->polling_thread = th; #ifdef CONFIG_PM if (ss_en) { rts51x->pusb_intf->needs_remote_wakeup = needs_remote_wakeup; SET_PM_USAGE_CNT(chip, 1); RTS51X_DEBUGP("pm_usage_cnt = %d\n", GET_PM_USAGE_CNT(chip)); } #endif return 0; BadDevice: RTS51X_DEBUGP("rts51x_probe() failed\n"); release_everything(chip); return result; }