Linux 实现USB自动挂载

我的问题:USB驱动都正常,USB插入和拔出也能正常识别.可是就是不能自动mount到/data/usb下,网上查了很多,贴出如下,可是都不适合我,我系统没有装udev,哪位大侠能帮帮我,如何实现USB自动挂载?

方法1: 

 

[c-sharp]  view plain copy
  1. #include <sys/types.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <dirent.h>  
  5. #include <unistd.h>  
  6.   
  7. int main(void)  
  8. {  
  9.       DIR *dir,*udisk_dir;  
  10.     struct dirent *next,*udisk_next;  
  11.     int dir_number=0,disk_num=0;  
  12.     int udisk_flag=0;  
  13.   
  14.     while(1)  
  15.         {  
  16.             //打开目录  
  17.             dir = opendir("/proc/bus/usb/001");  
  18.             if (!dir)  
  19.                 {  
  20.                 fprintf(stderr, "Cannot open /dev/scsi/n");  
  21.                         exit(1);  
  22.                 }  
  23.             //读取目录内文件名  
  24.             while((next = readdir(dir)) != NULL)  
  25.                 {  
  26.                     dir_number++;  
  27.                 }  
  28.             closedir(dir);  
  29.             switch(udisk_flag)  
  30.                 {  
  31.                     case 0:  
  32.                         {     
  33.                             //如果文件名大于2(除了 "." "..")说明U盘已经存在  
  34.                             if(dir_number>3)  
  35.                                 {  
  36.                                     udisk_dir = opendir("/dev/scsi/host0/bus0/target0/lun0");  
  37.                                     if (!udisk_dir)  
  38.                                             {  
  39.                                             fprintf(stderr, "Cannot open /dev/scsi/n");  
  40.                                                     exit(1);  
  41.                                             }  
  42.                                     while((udisk_next = readdir(udisk_dir)) != NULL)  
  43.                                             {  
  44.                                                 disk_num++;  
  45.                                             }  
  46.                                           
  47.                                         dir_number=0;  
  48.                                         udisk_flag=1;  
  49.                                         //如果文件个数大于4,说明U盘是由N块flash组成,需要挂载disc  
  50.                                         if(disk_num>4)  
  51.                                                 system("mknod /dev/udisk b 8 0");  
  52.                                     else  
  53.                                                 system("mknod /dev/udisk b 8 1");  
  54.                                     system("mount -t vfat /dev/udisk /mnt/usb");  
  55.                                     printf("The udisk has installed/r/n");  
  56.                                 }  
  57.                             break;  
  58.                         }  
  59.                     case 1:  
  60.                             {  
  61.                             if(dir_number==3)  
  62.                                 {  
  63.                                     dir_number=0;  
  64.                                     udisk_flag=0;  
  65.                                 system("umount /mnt/usb");  
  66.                                 system("rm -rf /dev/udisk");  
  67.                                 system("/test/udisk stop");  
  68.                                 system("/test/udisk start");  
  69.                                 printf("The udisk has uninstalled/r/n");  
  70.                                 }  
  71.                             break;  
  72.                         }   
  73.                     default:  
  74.                             break;  
  75.                 }  
  76.             //软计数器清零  
  77.             dir_number=0;   
  78.             disk_num=0;  
  79.             sleep(1);   
  80.         }  
  81.     return 0;  
  82. }  

方法2: 编写usb_scanner _driver.如下:

[c-sharp]  view plain copy
  1. /* -*- linux-c -*- */  
  2.   
  3. /*  
  4.  * Driver for USB Scanners (linux-2.6) 
  5.  * 
  6.  * Copyright (C) 1999, 2000, 2001, 2002 David E. Nelson 
  7.  * Copyright (C) 2002, 2003 Henning Meier-Geinitz 
  8.  * 
  9.  *  Performance: 
  10.  * 
  11.  *    System: Pentium 120, 80 MB RAM, OHCI, Linux 2.3.23, HP 4100C USB Scanner 
  12.  *            300 dpi scan of the entire bed 
  13.  *      24 Bit Color ~ 70 secs - 3.6 Mbit/sec 
  14.  *       8 Bit Gray ~ 17 secs - 4.2 Mbit/sec */  
  15.   
  16. /* 
  17.  * For documentation, see Documentation/usb/scanner.txt. 
  18.  * Website: http://www.meier-geinitz.de/kernel/ 
  19.  * Please contact the maintainer if your scanner is not detected by this 
  20.  * driver automatically. 
  21.  */  
  22.  
  23.  
  24. #include <asm/byteorder.h>  
  25.   
  26. /*  
  27.  * Scanner definitions, macros, module info,  
  28.  * debug/ioctl/data_dump enable, and other constants. 
  29.  */   
  30. #include "scanner.h"  
  31.   
  32. static void  
  33. irq_scanner(struct urb *urb, struct pt_regs *regs)  
  34. {  
  35.   
  36. /* 
  37.  * For the meantime, this is just a placeholder until I figure out what 
  38.  * all I want to do with it -- or somebody else for that matter. 
  39.  */  
  40.   
  41.     struct scn_usb_data *scn;  
  42.     unsigned char *data;  
  43.     int status;  
  44.   
  45.     scn = urb->context;  
  46.   
  47.     data = &scn->button;  
  48.     data += 0;      /* Keep gcc from complaining about unused var */  
  49.   
  50.     switch (urb->status) {  
  51.     case 0:  
  52.         /* success */  
  53.         break;  
  54.     case -ECONNRESET:  
  55.     case -ENOENT:  
  56.     case -ESHUTDOWN:  
  57.         /* this urb is terminated, clean up */  
  58.         dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);  
  59.         return;  
  60.     default:  
  61.         dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);  
  62.             return;   
  63.     }  
  64.   
  65.     dbg("irq_scanner(%d): data:%x", scn->scn_minor, *data);  
  66.   
  67.     status = usb_submit_urb (urb, GFP_ATOMIC);  
  68.     if (status)  
  69.         err ("%s - usb_submit_urb failed with result %d",  
  70.              __FUNCTION__, status);  
  71. }  
  72.   
  73. static int  
  74. open_scanner(struct inode * inode, struct file * file)  
  75. {  
  76.     struct scn_usb_data *scn;  
  77.     struct usb_device *dev;  
  78.     struct usb_interface *intf;  
  79.   
  80.     int scn_minor;  
  81.   
  82.     int err=0;  
  83.   
  84.     down(&scn_mutex);  
  85.   
  86.     scn_minor = USB_SCN_MINOR(inode);  
  87.   
  88.     dbg("open_scanner: scn_minor:%d", scn_minor);  
  89.   
  90.     intf = usb_find_interface(&scanner_driver, scn_minor);  
  91.     if (!intf) {  
  92.         up(&scn_mutex);  
  93.         err("open_scanner(%d): Unable to access minor data", scn_minor);  
  94.         return -ENODEV;  
  95.     }  
  96.     scn = usb_get_intfdata(intf);  
  97.     kobject_get(&scn->kobj);  
  98.   
  99.     dev = scn->scn_dev;  
  100.   
  101.     down(&(scn->sem));   /* Now protect the scn_usb_data structure */   
  102.   
  103.     up(&scn_mutex); /* Now handled by the above */  
  104.   
  105.     if (!dev) {  
  106.         err("open_scanner(%d): Scanner device not present", scn_minor);  
  107.         err = -ENODEV;  
  108.         goto out_error;  
  109.     }  
  110.   
  111.     if (!scn->present) {  
  112.         err("open_scanner(%d): Scanner is not present", scn_minor);  
  113.         err = -ENODEV;  
  114.         goto out_error;  
  115.     }  
  116.   
  117.     if (scn->isopen) {  
  118.         dbg("open_scanner(%d): Scanner device is already open", scn_minor);  
  119.         err = -EBUSY;  
  120.         goto out_error;  
  121.     }  
  122.   
  123.     init_waitqueue_head(&scn->rd_wait_q);  
  124.   
  125.     scn->isopen = 1;  
  126.   
  127.     file->private_data = scn; /* Used by the read and write methods */  
  128.   
  129.   
  130. out_error:  
  131.   
  132.     up(&(scn->sem)); /* Wake up any possible contending processes */  
  133.   
  134.     return err;  
  135. }  
  136.   
  137. static int  
  138. close_scanner(struct inode * inode, struct file * file)  
  139. {  
  140.     struct scn_usb_data *scn = file->private_data;  
  141.   
  142.     int scn_minor;  
  143.   
  144.     scn_minor = USB_SCN_MINOR (inode);  
  145.   
  146.     dbg("close_scanner: scn_minor:%d", scn_minor);  
  147.   
  148.     down(&scn_mutex);  
  149.     down(&(scn->sem));  
  150.   
  151.     scn->isopen = 0;  
  152.   
  153.     file->private_data = NULL;  
  154.   
  155.     up(&scn_mutex);  
  156.     up(&(scn->sem));  
  157.   
  158.     kobject_put(&scn->kobj);  
  159.   
  160.     return 0;  
  161. }  
  162.   
  163. static ssize_t  
  164. write_scanner(struct file * file, const char * buffer,  
  165.               size_t count, loff_t *ppos)  
  166. {  
  167.     struct scn_usb_data *scn;  
  168.     struct usb_device *dev;  
  169.   
  170.     ssize_t bytes_written = 0; /* Overall count of bytes written */  
  171.     ssize_t ret = 0;  
  172.   
  173.     int scn_minor;  
  174.   
  175.     int this_write;     /* Number of bytes to write */  
  176.     int partial;        /* Number of bytes successfully written */  
  177.     int result = 0;  
  178.   
  179.     char *obuf;  
  180.   
  181.     scn = file->private_data;  
  182.   
  183.     down(&(scn->sem));  
  184.   
  185.     if (!scn->bulk_out_ep) {  
  186.         /* This scanner does not have a bulk-out endpoint */  
  187.         up(&(scn->sem));  
  188.         return -EINVAL;  
  189.     }  
  190.   
  191.     scn_minor = scn->scn_minor;  
  192.   
  193.     obuf = scn->obuf;  
  194.   
  195.     dev = scn->scn_dev;  
  196.   
  197.     file->f_dentry->d_inode->i_atime = CURRENT_TIME;  
  198.   
  199.     while (count > 0) {  
  200.   
  201.         if (signal_pending(current)) {  
  202.             ret = -ERESTARTSYS;  
  203.             break;  
  204.         }  
  205.   
  206.         this_write = (count >= OBUF_SIZE) ? OBUF_SIZE : count;  
  207.   
  208.         if (copy_from_user(scn->obuf, buffer, this_write)) {  
  209.             ret = -EFAULT;  
  210.             break;  
  211.         }  
  212.   
  213.         result = usb_bulk_msg(dev,usb_sndbulkpipe(dev, scn->bulk_out_ep), obuf, this_write, &partial, 60*HZ);  
  214.         dbg("write stats(%d): result:%d this_write:%d partial:%d", scn_minor, result, this_write, partial);  
  215.   
  216.         if (result == -ETIMEDOUT) { /* NAK -- shouldn't happen */  
  217.             warn("write_scanner: NAK received.");  
  218.             ret = result;  
  219.             break;  
  220.         } else if (result < 0) { /* We should not get any I/O errors */  
  221.             warn("write_scanner(%d): funky result: %d. Consult Documentataion/usb/scanner.txt.", scn_minor, result);  
  222.             ret = -EIO;  
  223.             break;  
  224.         }  
  225.  
  226. #ifdef WR_DATA_DUMP  
  227.         if (partial) {  
  228.             unsigned char cnt, cnt_max;  
  229.             cnt_max = (partial > 24) ? 24 : partial;  
  230.             printk(KERN_DEBUG "dump(%d): ", scn_minor);  
  231.             for (cnt=0; cnt < cnt_max; cnt++) {  
  232.                 printk("%X ", obuf[cnt]);  
  233.             }  
  234.             printk("/n");  
  235.         }  
  236. #endif  
  237.         if (partial != this_write) { /* Unable to write all contents of obuf */  
  238.             ret = -EIO;  
  239.             break;  
  240.         }  
  241.   
  242.         if (partial) { /* Data written */  
  243.             buffer += partial;  
  244.             count -= partial;  
  245.             bytes_written += partial;  
  246.         } else { /* No data written */  
  247.             ret = 0;  
  248.             break;  
  249.         }  
  250.     }  
  251.     up(&(scn->sem));  
  252.     mdelay(5);      /* This seems to help with SANE queries */  
  253.     return ret ? ret : bytes_written;  
  254. }  
  255.   
  256. static ssize_t  
  257. read_scanner(struct file * file, char * buffer,  
  258.              size_t count, loff_t *ppos)  
  259. {  
  260.     struct scn_usb_data *scn;  
  261.     struct usb_device *dev;  
  262.   
  263.     ssize_t bytes_read; /* Overall count of bytes_read */  
  264.     ssize_t ret;  
  265.   
  266.     int scn_minor;  
  267.     int partial;        /* Number of bytes successfully read */  
  268.     int this_read;      /* Max number of bytes to read */  
  269.     int result;  
  270.     int rd_expire = RD_EXPIRE;  
  271.   
  272.     char *ibuf;  
  273.   
  274.     scn = file->private_data;  
  275.   
  276.     down(&(scn->sem));  
  277.   
  278.     scn_minor = scn->scn_minor;  
  279.   
  280.     ibuf = scn->ibuf;  
  281.   
  282.     dev = scn->scn_dev;  
  283.   
  284.     bytes_read = 0;  
  285.     ret = 0;  
  286.   
  287.     file->f_dentry->d_inode->i_atime = CURRENT_TIME; /* Update the 
  288.                                                             atime of 
  289.                                                             the device 
  290.                                                             node */  
  291.     while (count > 0) {  
  292.         if (signal_pending(current)) {  
  293.             ret = -ERESTARTSYS;  
  294.             break;  
  295.         }  
  296.   
  297.         this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;  
  298.   
  299.         result = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, scn->bulk_in_ep), ibuf, this_read, &partial, scn->rd_nak_timeout);  
  300.         dbg("read stats(%d): result:%d this_read:%d partial:%d count:%d", scn_minor, result, this_read, partial, count);  
  301.   
  302. /* 
  303.  * Scanners are sometimes inheriently slow since they are mechanical 
  304.  * in nature.  USB bulk reads tend to timeout while the scanner is 
  305.  * positioning, resetting, warming up the lamp, etc if the timeout is 
  306.  * set too low.  A very long timeout parameter for bulk reads was used 
  307.  * to overcome this limitation, but this sometimes resulted in folks 
  308.  * having to wait for the timeout to expire after pressing Ctrl-C from 
  309.  * an application. The user was sometimes left with the impression 
  310.  * that something had hung or crashed when in fact the USB read was 
  311.  * just waiting on data.  So, the below code retains the same long 
  312.  * timeout period, but splits it up into smaller parts so that 
  313.  * Ctrl-C's are acted upon in a reasonable amount of time. 
  314.  */  
  315.   
  316.         if (result == -ETIMEDOUT) { /* NAK */  
  317.             if (!partial) { /* No data */  
  318.                 if (--rd_expire <= 0) {  /* Give it up */  
  319.                     warn("read_scanner(%d): excessive NAK's received", scn_minor);  
  320.                     ret = result;  
  321.                     break;  
  322.                 } else { /* Keep trying to read data */  
  323.                     interruptible_sleep_on_timeout(&scn->rd_wait_q, scn->rd_nak_timeout);  
  324.                     continue;  
  325.                 }  
  326.             } else { /* Timeout w/ some data */  
  327.                 goto data_recvd;  
  328.             }  
  329.         }  
  330.           
  331.         if (result == -EPIPE) { /* No hope */  
  332.             if(usb_clear_halt(dev, scn->bulk_in_ep)) {  
  333.                 err("read_scanner(%d): Failure to clear endpoint halt condition (%Zd).", scn_minor, ret);  
  334.             }  
  335.             ret = result;  
  336.             break;  
  337.         } else if ((result < 0) && (result != -EREMOTEIO)) {  
  338.             warn("read_scanner(%d): funky result:%d. Consult Documentation/usb/scanner.txt.", scn_minor, (int)result);  
  339.             ret = -EIO;  
  340.             break;  
  341.         }  
  342.   
  343.     data_recvd:  
  344.  
  345. #ifdef RD_DATA_DUMP  
  346.         if (partial) {  
  347.             unsigned char cnt, cnt_max;  
  348.             cnt_max = (partial > 24) ? 24 : partial;  
  349.             printk(KERN_DEBUG "dump(%d): ", scn_minor);  
  350.             for (cnt=0; cnt < cnt_max; cnt++) {  
  351.                 printk("%X ", ibuf[cnt]);  
  352.             }  
  353.             printk("/n");  
  354.         }  
  355. #endif  
  356.   
  357.         if (partial) { /* Data returned */  
  358.             if (copy_to_user(buffer, ibuf, partial)) {  
  359.                 ret = -EFAULT;  
  360.                 break;  
  361.             }  
  362.             count -= this_read; /* Compensate for short reads */  
  363.             bytes_read += partial; /* Keep tally of what actually was read */  
  364.             buffer += partial;  
  365.         } else {  
  366.             ret = 0;  
  367.             break;  
  368.         }  
  369.     }  
  370.     up(&(scn->sem));  
  371.     return ret ? ret : bytes_read;  
  372. }  
  373.   
  374. static int  
  375. ioctl_scanner(struct inode *inode, struct file *file,  
  376.           unsigned int cmd, unsigned long arg)  
  377. {  
  378.     struct usb_device *dev;  
  379.     struct scn_usb_data *scn = file->private_data;  
  380.     int retval = -ENOTTY;  
  381.     int scn_minor;  
  382.   
  383.     scn_minor = USB_SCN_MINOR(inode);  
  384.     down(&(scn->sem));  
  385.   
  386.     dev = scn->scn_dev;  
  387.   
  388.     switch (cmd)  
  389.     {  
  390.     case SCANNER_IOCTL_VENDOR :  
  391.         retval = (put_user(dev->descriptor.idVendor, (unsigned int *) arg));  
  392.         break;  
  393.     case SCANNER_IOCTL_PRODUCT :  
  394.         retval = (put_user(dev->descriptor.idProduct, (unsigned int *) arg));  
  395.         break;  
  396.     case SCANNER_IOCTL_CTRLMSG:  
  397.     {  
  398.         struct ctrlmsg_ioctl {  
  399.             struct usb_ctrlrequest  req;  
  400.             void            *data;  
  401.         } cmsg;  
  402.         int pipe, nb, ret;  
  403.         unsigned char buf[64];  
  404.         retval = 0;  
  405.   
  406.         if (copy_from_user(&cmsg, (void *)arg, sizeof(cmsg))) {  
  407.             retval = -EFAULT;  
  408.             break;  
  409.         }  
  410.   
  411.         nb = cmsg.req.wLength;  
  412.   
  413.         if (nb > sizeof(buf)) {  
  414.             retval = -EINVAL;  
  415.             break;  
  416.         }  
  417.   
  418.         if ((cmsg.req.bRequestType & 0x80) == 0) {  
  419.             pipe = usb_sndctrlpipe(dev, 0);  
  420.             if (nb > 0 && copy_from_user(buf, cmsg.data, nb)) {  
  421.                 retval = -EFAULT;  
  422.                 break;  
  423.             }  
  424.         } else {  
  425.             pipe = usb_rcvctrlpipe(dev, 0);  
  426.         }  
  427.   
  428.         ret = usb_control_msg(dev, pipe, cmsg.req.bRequest,  
  429.                       cmsg.req.bRequestType,  
  430.                       cmsg.req.wValue,  
  431.                       cmsg.req.wIndex,  
  432.                       buf, nb, HZ);  
  433.   
  434.         if (ret < 0) {  
  435.             err("ioctl_scanner(%d): control_msg returned %d/n", scn_minor, ret);  
  436.             retval = -EIO;  
  437.             break;  
  438.         }  
  439.   
  440.         if (nb > 0 && (cmsg.req.bRequestType & 0x80) && copy_to_user(cmsg.data, buf, nb))  
  441.             retval = -EFAULT;  
  442.   
  443.         break;  
  444.     }  
  445.     default:  
  446.         break;  
  447.     }  
  448.     up(&(scn->sem));  
  449.     return retval;  
  450. }  
  451.   
  452. static void destroy_scanner (struct kobject *kobj)  
  453. {  
  454.     struct scn_usb_data *scn;  
  455.   
  456.     dbg ("%s", __FUNCTION__);  
  457.   
  458.     scn = to_scanner(kobj);  
  459.   
  460.     down (&scn_mutex);  
  461.     down (&(scn->sem));  
  462.   
  463.     usb_driver_release_interface(&scanner_driver,  
  464.         scn->scn_dev->actconfig->interface[scn->ifnum]);  
  465.   
  466.     kfree(scn->ibuf);  
  467.     kfree(scn->obuf);  
  468.   
  469.     usb_free_urb(scn->scn_irq);  
  470.     usb_put_dev(scn->scn_dev);  
  471.     up (&(scn->sem));  
  472.     kfree (scn);  
  473.     up (&scn_mutex);  
  474. }  
  475.   
  476. static struct kobj_type scanner_kobj_type = {  
  477.     .release = destroy_scanner,  
  478. };  
  479.   
  480. static struct  
  481. file_operations usb_scanner_fops = {  
  482.     .owner =    THIS_MODULE,  
  483.     .read =     read_scanner,  
  484.     .write =    write_scanner,  
  485.     .ioctl =    ioctl_scanner,  
  486.     .open =     open_scanner,  
  487.     .release =  close_scanner,  
  488. };  
  489.   
  490. static struct usb_class_driver scanner_class = {  
  491.     .name =     "usb/scanner%d",  
  492.     .fops =     &usb_scanner_fops,  
  493.     .mode =     S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,  
  494.     .minor_base =   SCN_BASE_MNR,  
  495. };  
  496.   
  497. static int  
  498. probe_scanner(struct usb_interface *intf,  
  499.           const struct usb_device_id *id)  
  500. {  
  501.     struct usb_device *dev = interface_to_usbdev (intf);  
  502.     struct scn_usb_data *scn;  
  503.     struct usb_host_interface *interface;  
  504.     struct usb_endpoint_descriptor *endpoint;  
  505.   
  506.     int ep_cnt;  
  507.     int ix;  
  508.     int retval;  
  509.   
  510.     char valid_device = 0;  
  511.     char have_bulk_in, have_bulk_out, have_intr;  
  512.     char name[14];  
  513.   
  514.     dbg("probe_scanner: USB dev address:%p", dev);  
  515.   
  516. /* 
  517.  * 1. Check Vendor/Product 
  518.  * 2. Determine/Assign Bulk Endpoints 
  519.  * 3. Determine/Assign Intr Endpoint 
  520.  */  
  521.   
  522. /* 
  523.  * There doesn't seem to be an imaging class defined in the USB 
  524.  * Spec. (yet).  If there is, HP isn't following it and it doesn't 
  525.  * look like anybody else is either.  Therefore, we have to test the 
  526.  * Vendor and Product ID's to see what we have.  Also, other scanners 
  527.  * may be able to use this driver by specifying both vendor and 
  528.  * product ID's as options to the scanner module in conf.modules. 
  529.  * 
  530.  * NOTE: Just because a product is supported here does not mean that 
  531.  * applications exist that support the product.  It's in the hopes 
  532.  * that this will allow developers a means to produce applications 
  533.  * that will support USB products. 
  534.  * 
  535.  * Until we detect a device which is pleasing, we silently punt. 
  536.  */  
  537.   
  538.     for (ix = 0; ix < sizeof (scanner_device_ids) / sizeof (struct usb_device_id); ix++) {  
  539.         if ((dev->descriptor.idVendor == scanner_device_ids [ix].idVendor) &&  
  540.             (dev->descriptor.idProduct == scanner_device_ids [ix].idProduct)) {  
  541.             valid_device = 1;  
  542.             break;  
  543.                 }  
  544.     }  
  545.     if (dev->descriptor.idVendor == vendor &&   /* User specified */  
  546.         dev->descriptor.idProduct == product) { /* User specified */  
  547.         valid_device = 1;  
  548.     }  
  549.   
  550.     if (!valid_device)  
  551.         return -ENODEV; /* We didn't find anything pleasing */  
  552.   
  553. /* 
  554.  * After this point we can be a little noisy about what we are trying to 
  555.  *  configure. 
  556.  */  
  557.   
  558.     if (dev->descriptor.bNumConfigurations != 1) {  
  559.         info("probe_scanner: Only one device configuration is supported.");  
  560.         return -ENODEV;  
  561.     }  
  562.   
  563.     interface = intf->altsetting;  
  564.    
  565.     if (interface[0].desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&  
  566.         interface[0].desc.bInterfaceClass != USB_CLASS_PER_INTERFACE &&  
  567.         interface[0].desc.bInterfaceClass != USB_CLASS_CDC_DATA &&  
  568.         interface[0].desc.bInterfaceClass != SCN_CLASS_SCANJET) {  
  569.         dbg("probe_scanner: This interface doesn't look like a scanner (class=0x%x)."interface[0].desc.bInterfaceClass);  
  570.         return -ENODEV;  
  571.     }  
  572.   
  573. /* 
  574.  * Start checking for bulk and interrupt endpoints. We are only using the first 
  575.  * one of each type of endpoint. If we have an interrupt endpoint go ahead and 
  576.  * setup the handler. FIXME: This is a future enhancement... 
  577.  */  
  578.   
  579.     dbg("probe_scanner: Number of Endpoints:%d", (intinterface->desc.bNumEndpoints);  
  580.   
  581.     ep_cnt = have_bulk_in = have_bulk_out = have_intr = 0;  
  582.   
  583.     while (ep_cnt < interface->desc.bNumEndpoints) {  
  584.         endpoint = &interface->endpoint[ep_cnt].desc;  
  585.   
  586.         if (IS_EP_BULK_IN(endpoint)) {  
  587.             ep_cnt++;  
  588.             if (have_bulk_in) {  
  589.                 info ("probe_scanner: ignoring additional bulk_in_ep:%d", ep_cnt);  
  590.                 continue;  
  591.             }  
  592.             have_bulk_in = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;  
  593.             dbg("probe_scanner: bulk_in_ep:%d", have_bulk_in);  
  594.             continue;  
  595.         }  
  596.   
  597.         if (IS_EP_BULK_OUT(endpoint)) {  
  598.             ep_cnt++;  
  599.             if (have_bulk_out) {  
  600.                 info ("probe_scanner: ignoring additional bulk_out_ep:%d", ep_cnt);  
  601.                 continue;  
  602.             }  
  603.             have_bulk_out = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;  
  604.             dbg("probe_scanner: bulk_out_ep:%d", have_bulk_out);  
  605.             continue;  
  606.         }  
  607.   
  608.         if (IS_EP_INTR(endpoint)) {  
  609.             ep_cnt++;  
  610.             if (have_intr) {  
  611.                 info ("probe_scanner: ignoring additional intr_ep:%d", ep_cnt);  
  612.                 continue;  
  613.             }  
  614.             have_intr = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;  
  615.             dbg("probe_scanner: intr_ep:%d", have_intr);  
  616.             continue;  
  617.         }  
  618.         info("probe_scanner: Undetected endpoint -- consult Documentation/usb/scanner.txt.");  
  619.         return -EIO;    /* Shouldn't ever get here unless we have something weird */  
  620.     }  
  621.   
  622.   
  623. /* 
  624.  * Perform a quick check to make sure that everything worked as it 
  625.  * should have. 
  626.  */  
  627.   
  628.     if (!have_bulk_in) {  
  629.         err("probe_scanner: One bulk-in endpoint required.");  
  630.         return -EIO;  
  631.     }  
  632.   
  633. /* 
  634.  * Determine a minor number and initialize the structure associated 
  635.  * with it.  The problem with this is that we are counting on the fact 
  636.  * that the user will sequentially add device nodes for the scanner 
  637.  * devices.  */  
  638.       
  639.     down(&scn_mutex);  
  640.   
  641.     retval = usb_register_dev(intf, &scanner_class);  
  642.     if (retval) {  
  643.         err ("Not able to get a minor for this device.");  
  644.         up(&scn_mutex);  
  645.         return -ENOMEM;  
  646.     }  
  647.   
  648.     dbg("probe_scanner: Allocated minor:%d", intf->minor);  
  649.   
  650.     if (!(scn = kmalloc (sizeof (struct scn_usb_data), GFP_KERNEL))) {  
  651.         err("probe_scanner: Out of memory.");  
  652.         up(&scn_mutex);  
  653.         return -ENOMEM;  
  654.     }  
  655.     memset (scn, 0, sizeof(struct scn_usb_data));  
  656.     kobject_init(&scn->kobj);  
  657.     scn->kobj.ktype = &scanner_kobj_type;  
  658.   
  659.     scn->scn_irq = usb_alloc_urb(0, GFP_KERNEL);  
  660.     if (!scn->scn_irq) {  
  661.         usb_deregister_dev(intf, &scanner_class);  
  662.         kfree(scn);  
  663.         up(&scn_mutex);  
  664.         return -ENOMEM;  
  665.     }  
  666.   
  667.     init_MUTEX(&(scn->sem)); /* Initializes to unlocked */  
  668.   
  669.     dbg ("probe_scanner(%d): Address of scn:%p", intf->minor, scn);  
  670.   
  671. /* Ok, if we detected an interrupt EP, setup a handler for it */  
  672.     if (have_intr) {  
  673.         dbg("probe_scanner(%d): Configuring IRQ handler for intr EP:%d", intf->minor, have_intr);  
  674.         usb_fill_int_urb(scn->scn_irq, dev,  
  675.                  usb_rcvintpipe(dev, have_intr),  
  676.                  &scn->button, 1, irq_scanner, scn,  
  677.                  // endpoint[(int)have_intr].bInterval);  
  678.                  250);  
  679.   
  680.         retval = usb_submit_urb(scn->scn_irq, GFP_KERNEL);  
  681.         if (retval) {  
  682.             err("probe_scanner(%d): Unable to allocate INT URB.", intf->minor);  
  683.             usb_deregister_dev(intf, &scanner_class);  
  684.                     kfree(scn);  
  685.             up(&scn_mutex);  
  686.                     return retval;  
  687.             }  
  688.     }  
  689.   
  690.   
  691. /* Ok, now initialize all the relevant values */  
  692.     if (!(scn->obuf = (char *)kmalloc(OBUF_SIZE, GFP_KERNEL))) {  
  693.         err("probe_scanner(%d): Not enough memory for the output buffer.", intf->minor);  
  694.         if (have_intr)  
  695.             usb_unlink_urb(scn->scn_irq);  
  696.         usb_free_urb(scn->scn_irq);  
  697.         usb_deregister_dev(intf, &scanner_class);  
  698.         kfree(scn);  
  699.         up(&scn_mutex);  
  700.         return -ENOMEM;  
  701.     }  
  702.     dbg("probe_scanner(%d): obuf address:%p", intf->minor, scn->obuf);  
  703.   
  704.     if (!(scn->ibuf = (char *)kmalloc(IBUF_SIZE, GFP_KERNEL))) {  
  705.         err("probe_scanner(%d): Not enough memory for the input buffer.", intf->minor);  
  706.         if (have_intr)  
  707.             usb_unlink_urb(scn->scn_irq);  
  708.         usb_free_urb(scn->scn_irq);  
  709.         usb_deregister_dev(intf, &scanner_class);  
  710.         kfree(scn->obuf);  
  711.         kfree(scn);  
  712.         up(&scn_mutex);  
  713.         return -ENOMEM;  
  714.     }  
  715.     dbg("probe_scanner(%d): ibuf address:%p", intf->minor, scn->ibuf);  
  716.       
  717.   
  718.     switch (dev->descriptor.idVendor) { /* Scanner specific read timeout parameters */  
  719.     case 0x04b8:        /* Seiko/Epson */  
  720.         scn->rd_nak_timeout = HZ * 60;  
  721.         break;  
  722.     case 0x055f:        /* Mustek */  
  723.     case 0x0400:        /* Another Mustek */  
  724.         scn->rd_nak_timeout = HZ * 1;  
  725.     default:  
  726.         scn->rd_nak_timeout = RD_NAK_TIMEOUT;  
  727.     }  
  728.   
  729.   
  730.     if (read_timeout > 0) {  /* User specified read timeout overrides everything */  
  731.         info("probe_scanner: User specified USB read timeout - %d", read_timeout);  
  732.         scn->rd_nak_timeout = read_timeout;  
  733.     }  
  734.   
  735.   
  736.     usb_get_dev(dev);  
  737.     scn->bulk_in_ep = have_bulk_in;  
  738.     scn->bulk_out_ep = have_bulk_out;  
  739.     scn->intr_ep = have_intr;  
  740.     scn->present = 1;  
  741.     scn->scn_dev = dev;  
  742.     scn->scn_minor = intf->minor;  
  743.     scn->isopen = 0;  
  744.   
  745.     snprintf(name, sizeof(name), scanner_class.name,  
  746.          intf->minor - scanner_class.minor_base);  
  747.   
  748.     info ("USB scanner device (0x%04x/0x%04x) now attached to %s",  
  749.           dev->descriptor.idVendor, dev->descriptor.idProduct, name);  
  750.   
  751.     usb_set_intfdata(intf, scn);  
  752.     up(&scn_mutex);  
  753.       
  754.     return 0;  
  755. }  
  756.   
  757. static void  
  758. disconnect_scanner(struct usb_interface *intf)  
  759. {  
  760.     struct scn_usb_data *scn = usb_get_intfdata(intf);  
  761.   
  762.     /* disable open() */  
  763.     dbg("%s: De-allocating minor:%d", __FUNCTION__, scn->scn_minor);  
  764.     usb_deregister_dev(intf, &scanner_class);  
  765.   
  766.     usb_set_intfdata(intf, NULL);  
  767.     if(scn->intr_ep) {  
  768.         dbg("%s(%d): Unlinking IRQ URB", __FUNCTION__, scn->scn_minor);  
  769.         usb_unlink_urb(scn->scn_irq);  
  770.     }  
  771.   
  772.     if (scn)  
  773.         kobject_put(&scn->kobj);  
  774. }  
  775.   
  776. /* we want to look at all devices, as the vendor/product id can change 
  777.  * depending on the command line argument */  
  778. static struct usb_device_id ids[] = {  
  779.     {.driver_info = 42},  
  780.     {}  
  781. };  
  782.   
  783. static struct  
  784. usb_driver scanner_driver = {  
  785.     .owner =    THIS_MODULE,  
  786.     .name =     "usbscanner",  
  787.     .probe =    probe_scanner,  
  788.     .disconnect =   disconnect_scanner,  
  789.     .id_table = ids,  
  790. };  
  791.   
  792. static void __exit  
  793. usb_scanner_exit(void)  
  794. {  
  795.     usb_deregister(&scanner_driver);  
  796. }  
  797.   
  798. static int __init  
  799. usb_scanner_init (void)  
  800. {  
  801.     int retval;  
  802.     retval = usb_register(&scanner_driver);  
  803.     if (retval)  
  804.         goto out;  
  805.   
  806.     info(DRIVER_VERSION ":" DRIVER_DESC);  
  807.     if (vendor != -1 && product != -1)  
  808.         info("probe_scanner: User specified USB scanner -- Vendor:Product - %x:%x", vendor, product);  
  809.  out:  
  810.     return retval;  
  811. }  
  812.   
  813. module_init(usb_scanner_init);  
  814. module_exit(usb_scanner_exit);  

 我的系统配置:

1.LINUX2.6.14.1 FOR S3C2410+DM9000+UDA1341+64M nandflash+64M sdram

2.u-boot1.1.3

3.cramfs+yaffs2

4.nfs+tftp

我把我的linuxrc/rcS/rc.local贴出来,大家帮忙分析分析.谢谢!

[root@zmf26 rootfs]# cat linuxrc

#!/bin/ash
echo "------next to run /linuxrc"
/bin/mount -f -t cramfs -o remount,ro /dev/mtdblock/3 /
/bin/mount -t tmpfs tmpfs /dev/shm
/bin/mount -f -t sysfs sysfs /sys
/bin/mount -t proc none /proc
/bin/mount -t devpts devpts /dev/pts

# mount devpts in order to use telnetd
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s

echo "------next to mount /tmp,/tmp/app,cp /etc/tmp/boa /tmp/app"
/bin/mount -t tmpfs none /root
/bin/cp /etc/udev/usbmount.sh /root/

/bin/mount -t tmpfs none /var
/bin/mkdir -p /var/lib
/bin/mkdir -p /var/run
/bin/mkdir -p /var/log/boa
/bin/cp -rf /etc/tmp/www /var/
/bin/mkdir -p /var/www/cgi-bin

/bin/mount -t ramfs ramfs /tmp
/bin/mkdir /tmp/app
/bin/mount -f -t ramfs ramfs /tmp/app
/bin/cp -rf /etc/tmp/boa /tmp/app/

echo "------next to mount yaffs2 /user,/data"
/bin/mount -t yaffs2 /dev/mtdblock/4 /user
/bin/mount -t yaffs2 /dev/mtdblock/5 /data

exec /usr/etc/rc.local

echo "------next to run /sbin/init"
exec /sbin/init

 

[root@zmf26 rootfs]# cat etc/init.d/rcS
#!/bin/ash
echo "------next to run /etc/inid.d/rcS"
/bin/mount -a
exec /linuxrc
#exec /usr/etc/rc.local


[root@zmf26 rootfs]# cat usr/etc/rc.local 
#!/bin/ash
echo "------next to run /usr/etc/rc.local "
#./usr/etc/profile
WEBPATH=/tmp/app
/sbin/inetd

#/sbin/ifconfig eth0 192.168.0.99
#/sbin/ifconfig eth0 down
#/sbin/ifconfig eth0 hw ether 00:00:f1:21:53:f5
#/sbin/pump -i eth0
/sbin/ifconfig eth0 192.168.0.99 netmask 255.255.255.0 up
/sbin/route add default gw 192.168.0.1 
/sbin/inetd
echo "------ifconfig eth0 192.168.0.99 complete"

#cd /dev
#ln -s fb/0 fb0
#ln -s vc/0 tty0               
#ln -s vc/1 tty1               
#ln -s vc/2 tty2
#echo "------ ln -s vc/2 tty2 complete"

/sbin/inetd /etc/inetd.conf

if [ -f /tmp/app/boa/boa ];
then
/tmp/app/boa/boa -c /etc/boa/&
echo "------run /tmp/app/boa/boa&"
else
echo "------error:/tmp/app/boa file dose'n exist!"
fi

if [ -f $WEBPATH/powercontrolboard ];
then
$WEBPATH/powercontrolboard&
echo "------run $WEBPATH/powercontrolboard&"
else
echo "------error:$WEBPATH/powercontrolboard file dose'n exist!"
fi

if [ -f $WEBPATH/boa/http/cgi-bin/smart_arm.cgi ];
then
echo "------$WEBPATH/boa/http/cgi-bin/smart_arm.cgi file exist!"
else
echo "------error:$WEBPATH/boa/http/cgi-bin/smart_arm.cgi file dose'n exist!" 
fi

启动信息如下:

Uncompressing Linux............................................................................................. done, booting the kernel.
Linux version 2.6.14 (root@zmf26.WORKGROUP) (gcc version 3.4.1) #108 Wed Aug 5 03:02:40 CST 2009
CPU: ARM920Tid(wb) [41129200] revision 0 (ARMv4T)
Machine: SMDK2410
Memory policy: ECC disabled, Data cache writeback
CPU S3C2410A (id 0x32410002)
S3C2410: core 200.000 MHz, memory 100.000 MHz, peripheral 50.000 MHz
S3C2410 Clocks, (c) 2004 Simtec Electronics
CLOCK: Slow mode (1.500 MHz), fast, MPLL on, UPLL on
CPU0: D VIVT write-back cache
CPU0: I cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
CPU0: D cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
Built 1 zonelists
Kernel command line: mem=64M console=ttySAC0,115200 root=/dev/nfs nfsroot=192.168.0.57:/home/arm/dev_home/rootfs ip=192.168.0.99:192.168.0.57:192.168.0.1:255.255.255.0::eth0:off
irq: clearing subpending status 00000002
PID hash table entries: 512 (order: 9, 8192 bytes)
timer tcon=00500000, tcnt a2c1, tcfg 00000200,00000000, usec 00001eb8
Console: colour dummy device 80x30
Dentry cache hash table entries: 16384 (order: 4, 65536 bytes)
Inode-cache hash table entries: 8192 (order: 3, 32768 bytes)
Memory: 64MB = 64MB total
Memory: 61824KB available (2330K code, 559K data, 96K init)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
softlockup thread 0 started up.
NET: Registered protocol family 16
S3C2410: Initialising architecture
SCSI subsystem initialized
usbcore: registered new driver usbfs
usbcore: registered new driver hub
S3C2410 DMA Driver, (c) 2003-2004 Simtec Electronics
DMA channel 0 at c4800000, irq 33
DMA channel 1 at c4800040, irq 34
DMA channel 2 at c4800080, irq 35
DMA channel 3 at c48000c0, irq 36
NetWinder Floating Point Emulator V0.97 (double precision)
devfs: 2004-01-31 Richard Gooch (rgooch@atnf.csiro.au)
devfs: boot_options: 0x1
NTFS driver 2.1.24 [Flags: R/W DEBUG].
yaffs Aug  2 2009 17:07:52 Installing. 
Initializing Cryptographic API
Console: switching to colour frame buffer device 80x25
fb0: Virtual frame buffer device, using 1024K of video memory
S3C2410 RTC, (c) 2004 Simtec Electronics
/dev/keybutton inited: newkoom company:zhengmeifu@sina.com
/dev/ledrpm inited: newkoom company:zhengmeifu@sina.com
s3c2410_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2410
s3c2410_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2410
s3c2410_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2410
io scheduler noop registered
io scheduler anticipatory registered
io scheduler deadline registered
io scheduler cfq registered
RAMDISK driver initialized: 8 RAM disks of 4096K size 1024 blocksize
nbd: registered device at major 43
usbcore: registered new driver ub
dm9000 Ethernet Driver
dm9000 io_base is : c4910300
eth0: dm9000 at c4910300,c4912304 IRQ 19 MAC: 02:e1:3d:a4:d7:f7
netconsole: not configured, aborting
S3C24XX NAND Driver, (c) 2004 Simtec Electronics
s3c2410-nand: mapped registers at c4980000
s3c2410-nand: timing: Tacls 10ns, Twrph0 40ns, Twrph1 10ns
NAND device: Manufacturer ID: 0xec, Chip ID: 0x76 (Samsung NAND 64MiB 3,3V 8-bit)
Scanning device for bad blocks
Bad eraseblock 3777 at 0x03b04000
Bad eraseblock 3778 at 0x03b08000
Bad eraseblock 3779 at 0x03b0c000
Bad eraseblock 3780 at 0x03b10000
Bad eraseblock 3781 at 0x03b14000
Bad eraseblock 3782 at 0x03b18000
Bad eraseblock 3783 at 0x03b1c000
Bad eraseblock 3784 at 0x03b20000
Bad eraseblock 3785 at 0x03b24000
Bad eraseblock 3786 at 0x03b28000
Bad eraseblock 3787 at 0x03b2c000
Bad eraseblock 3789 at 0x03b34000
Bad eraseblock 3791 at 0x03b3c000
Bad eraseblock 3794 at 0x03b48000
Bad eraseblock 3827 at 0x03bcc000
Bad eraseblock 3828 at 0x03bd0000
Bad eraseblock 3830 at 0x03bd8000
Bad eraseblock 3895 at 0x03cdc000
Bad eraseblock 4094 at 0x03ff8000
Creating 6 MTD partitions on "NAND 64MiB 3,3V 8-bit":
0x00000000-0x000e0000 : "bootloader"
0x000e0000-0x00100000 : "param"
0x00100000-0x00400000 : "kernel"
0x00400000-0x02200000 : "root"
0x02200000-0x02c00000 : "user"
0x02c00000-0x04000000 : "data"
usbmon: debugfs is not available
s3c2410-ohci s3c2410-ohci: S3C24XX OHCI
s3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1
s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000
usb usb1: Product: S3C24XX OHCI
usb usb1: Manufacturer: Linux 2.6.14 ohci_hcd
usb usb1: SerialNumber: s3c24xx
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 2 ports detected
Initializing USB Mass Storage driver...
usb 1-1: new full speed USB device using s3c2410-ohci and address 2
usb 1-1: Product: DataTraveler 2.0
usb 1-1: Manufacturer: Kingston
usb 1-1: SerialNumber: 5B82190008A7
ub(1.2): GetMaxLUN returned 0, using 1 LUNs
 /dev/ub/a: unknown partition table
usbcore: registered new driver usb-storage
USB Mass Storage support registered.
UDA1341 audio driver initialized
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 4096 (order: 2, 16384 bytes)
TCP bind hash table entries: 4096 (order: 2, 16384 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
TCP reno registered
TCP bic registered
eth0: link down
IP-Config: Complete:
      device=eth0, addr=192.168.0.99, mask=255.255.255.0, gw=192.168.0.1,
     host=192.168.0.99, domain=, nis-domain=(none),
     bootserver=192.168.0.57, rootserver=192.168.0.57, rootpath=
Looking up port of RPC 100003/2 on 192.168.0.57
eth0: link up, 100Mbps, full-duplex, lpa 0x41E1
Looking up port of RPC 100005/1 on 192.168.0.57
VFS: Mounted root (nfs filesystem).
Mounted devfs on /dev
Freeing init memory: 96K
init started:  BusyBox v1.1.3 (2009.07.28-04:02+0000) multi-call binary
init started:  BusyBox v1.1.3 (2009.07.28-04:02+0000) multi-call binary
Starting pid 205, console /dev/tts/0: '/etc/init.d/rcS'
------next to run /etc/inid.d/rcS
mount: Mounting none on /dev/pts failed: No such file or directory
------next to run /linuxrc
mount: Mounting none on /proc failed: Device or resource busy
mount: Mounting devpts on /dev/pts failed: Device or resource busy
------next to mount /tmp,/tmp/app,cp /etc/tmp/boa /tmp/app
------next to mount yaffs2 /user,/data
yaffs: dev is 32505860 name is "mtdblock4"
yaffs: passed flags ""
yaffs: Attempting MTD mount on 31.4, "mtdblock4"
yaffs: auto selecting yaffs1
yaffs: dev is 32505861 name is "mtdblock5"
yaffs: passed flags ""
yaffs: Attempting MTD mount on 31.5, "mtdblock5"
yaffs: auto selecting yaffs1
block 962 is bad
block 963 is bad
block 964 is bad
block 965 is bad
block 966 is bad
block 967 is bad
block 968 is bad
block 969 is bad
block 970 is bad
block 971 is bad
block 972 is bad
block 974 is bad
block 976 is bad
block 979 is bad
block 1012 is bad
block 1013 is bad
block 1015 is bad
block 1080 is bad
block 1279 is bad
------next to run /usr/etc/rc.local 
route: SIOC[ADD|DEL]RT: File exists
------ifconfig eth0 192.168.0.99 complete
------run /tmp/app/boa/boa&
------error:/tmp/app/powercontrolboard file dose'n exist!
------error:/tmp/app/boa/http/cgi-bin/smart_arm.cgi file dose'n exist!

Please press Enter to activate this console.

转自:http://blog.csdn.net/zhengmeifu/article/details/4539030

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值