VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / core / inode.c
1 /*****************************************************************************/
2
3 /*
4  *      inode.c  --  Inode/Dentry functions for the USB device file system.
5  *
6  *      Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7  *      Copyright (C) 2001,2002 Greg Kroah-Hartman (greg@kroah.com)
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  History:
24  *   0.1  04.01.2000  Created
25  *   0.2  10.12.2001  converted to use the vfs layer better
26  */
27
28 /*****************************************************************************/
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/fs.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/init.h>
36 #include <linux/proc_fs.h>
37 #include <linux/usb.h>
38 #include <linux/namei.h>
39 #include <linux/usbdevice_fs.h>
40 #include <linux/smp_lock.h>
41 #include <linux/parser.h>
42 #include <asm/byteorder.h>
43
44 static struct super_operations usbfs_ops;
45 static struct file_operations default_file_operations;
46 static struct inode_operations usbfs_dir_inode_operations;
47 static struct vfsmount *usbdevfs_mount;
48 static struct vfsmount *usbfs_mount;
49 static int usbdevfs_mount_count;        /* = 0 */
50 static int usbfs_mount_count;   /* = 0 */
51 static int ignore_mount = 0;
52
53 static struct dentry *devices_usbdevfs_dentry;
54 static struct dentry *devices_usbfs_dentry;
55 static int num_buses;   /* = 0 */
56
57 static uid_t devuid;    /* = 0 */
58 static uid_t busuid;    /* = 0 */
59 static uid_t listuid;   /* = 0 */
60 static gid_t devgid;    /* = 0 */
61 static gid_t busgid;    /* = 0 */
62 static gid_t listgid;   /* = 0 */
63 static umode_t devmode = S_IWUSR | S_IRUGO;
64 static umode_t busmode = S_IXUGO | S_IRUGO;
65 static umode_t listmode = S_IRUGO;
66
67 enum {
68         Opt_devuid, Opt_devgid, Opt_devmode,
69         Opt_busuid, Opt_busgid, Opt_busmode,
70         Opt_listuid, Opt_listgid, Opt_listmode,
71         Opt_err,
72 };
73
74 static match_table_t tokens = {
75         {Opt_devuid, "devuid=%u"},
76         {Opt_devgid, "devgid=%u"},
77         {Opt_devmode, "devmode=%o"},
78         {Opt_busuid, "busuid=%u"},
79         {Opt_busgid, "busgid=%u"},
80         {Opt_busmode, "busmode=%o"},
81         {Opt_listuid, "listuid=%u"},
82         {Opt_listgid, "listgid=%u"},
83         {Opt_listmode, "listmode=%o"},
84         {Opt_err, NULL}
85 };
86
87 static int parse_options(struct super_block *s, char *data)
88 {
89         char *p;
90         int option;
91
92         /* (re)set to defaults. */
93         devuid = 0;
94         busuid = 0;
95         listuid = 0;
96         devgid = 0;
97         busgid = 0;
98         listgid = 0;
99         devmode = S_IWUSR | S_IRUGO;
100         busmode = S_IXUGO | S_IRUGO;
101         listmode = S_IRUGO;
102
103         while ((p = strsep(&data, ",")) != NULL) {
104                 substring_t args[MAX_OPT_ARGS];
105                 int token;
106                 if (!*p)
107                         continue;
108
109                 token = match_token(p, tokens, args);
110                 switch (token) {
111                 case Opt_devuid:
112                         if (match_int(&args[0], &option))
113                                return -EINVAL;
114                         devuid = option;
115                         break;
116                 case Opt_devgid:
117                         if (match_int(&args[0], &option))
118                                return -EINVAL;
119                         devgid = option;
120                         break;
121                 case Opt_devmode:
122                         if (match_octal(&args[0], &option))
123                                 return -EINVAL;
124                         devmode = option & S_IRWXUGO;
125                         break;
126                 case Opt_busuid:
127                         if (match_int(&args[0], &option))
128                                return -EINVAL;
129                         busuid = option;
130                         break;
131                 case Opt_busgid:
132                         if (match_int(&args[0], &option))
133                                return -EINVAL;
134                         busgid = option;
135                         break;
136                 case Opt_busmode:
137                         if (match_octal(&args[0], &option))
138                                 return -EINVAL;
139                         busmode = option & S_IRWXUGO;
140                         break;
141                 case Opt_listuid:
142                         if (match_int(&args[0], &option))
143                                return -EINVAL;
144                         listuid = option;
145                         break;
146                 case Opt_listgid:
147                         if (match_int(&args[0], &option))
148                                return -EINVAL;
149                         listgid = option;
150                         break;
151                 case Opt_listmode:
152                         if (match_octal(&args[0], &option))
153                                 return -EINVAL;
154                         listmode = option & S_IRWXUGO;
155                         break;
156                 default:
157                         err("usbfs: unrecognised mount option \"%s\" "
158                             "or missing value\n", p);
159                         return -EINVAL;
160                 }
161         }
162
163         return 0;
164 }
165
166 static void update_special(struct dentry *special)
167 {
168         special->d_inode->i_uid = listuid;
169         special->d_inode->i_gid = listgid;
170         special->d_inode->i_mode = S_IFREG | listmode;
171 }
172
173 static void update_dev(struct dentry *dev)
174 {
175         dev->d_inode->i_uid = devuid;
176         dev->d_inode->i_gid = devgid;
177         dev->d_inode->i_mode = S_IFREG | devmode;
178 }
179
180 static void update_bus(struct dentry *bus)
181 {
182         struct dentry *dev = NULL;
183
184         bus->d_inode->i_uid = busuid;
185         bus->d_inode->i_gid = busgid;
186         bus->d_inode->i_mode = S_IFDIR | busmode;
187
188         down(&bus->d_inode->i_sem);
189
190         list_for_each_entry(dev, &bus->d_subdirs, d_child)
191                 if (dev->d_inode)
192                         update_dev(dev);
193
194         up(&bus->d_inode->i_sem);
195 }
196
197 static void update_sb(struct super_block *sb)
198 {
199         struct dentry *root = sb->s_root;
200         struct dentry *bus = NULL;
201
202         if (!root)
203                 return;
204
205         down(&root->d_inode->i_sem);
206
207         list_for_each_entry(bus, &root->d_subdirs, d_child) {
208                 if (bus->d_inode) {
209                         switch (S_IFMT & bus->d_inode->i_mode) {
210                         case S_IFDIR:
211                                 update_bus(bus);
212                                 break;
213                         case S_IFREG:
214                                 update_special(bus);
215                                 break;
216                         default:
217                                 warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode);
218                                 break;
219                         }
220                 }
221         }
222
223         up(&root->d_inode->i_sem);
224 }
225
226 static int remount(struct super_block *sb, int *flags, char *data)
227 {
228         /* If this is not a real mount,
229          * i.e. it's a simple_pin_fs from create_special_files,
230          * then ignore it.
231          */
232         if (ignore_mount)
233                 return 0;
234
235         if (parse_options(sb, data)) {
236                 warn("usbfs: mount parameter error:");
237                 return -EINVAL;
238         }
239
240         if (usbfs_mount && usbfs_mount->mnt_sb)
241                 update_sb(usbfs_mount->mnt_sb);
242
243         if (usbdevfs_mount && usbdevfs_mount->mnt_sb)
244                 update_sb(usbdevfs_mount->mnt_sb);
245
246         return 0;
247 }
248
249 static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
250 {
251         struct inode *inode = new_inode(sb);
252
253         if (inode) {
254                 inode->i_mode = mode;
255                 inode->i_uid = current->fsuid;
256                 inode->i_gid = current->fsgid;
257                 inode->i_blksize = PAGE_CACHE_SIZE;
258                 inode->i_blocks = 0;
259                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
260                 switch (mode & S_IFMT) {
261                 default:
262                         init_special_inode(inode, mode, dev);
263                         break;
264                 case S_IFREG:
265                         inode->i_fop = &default_file_operations;
266                         break;
267                 case S_IFDIR:
268                         inode->i_op = &usbfs_dir_inode_operations;
269                         inode->i_fop = &simple_dir_operations;
270
271                         /* directory inodes start off with i_nlink == 2 (for "." entry) */
272                         inode->i_nlink++;
273                         break;
274                 }
275         }
276         return inode; 
277 }
278
279 /* SMP-safe */
280 static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
281                         dev_t dev)
282 {
283         struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
284         int error = -EPERM;
285
286         if (dentry->d_inode)
287                 return -EEXIST;
288
289         if (inode) {
290                 d_instantiate(dentry, inode);
291                 dget(dentry);
292                 error = 0;
293         }
294         return error;
295 }
296
297 static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
298 {
299         int res;
300
301         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
302         res = usbfs_mknod (dir, dentry, mode, 0);
303         if (!res)
304                 dir->i_nlink++;
305         return res;
306 }
307
308 static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
309 {
310         mode = (mode & S_IALLUGO) | S_IFREG;
311         return usbfs_mknod (dir, dentry, mode, 0);
312 }
313
314 static inline int usbfs_positive (struct dentry *dentry)
315 {
316         return dentry->d_inode && !d_unhashed(dentry);
317 }
318
319 static int usbfs_empty (struct dentry *dentry)
320 {
321         struct list_head *list;
322
323         spin_lock(&dcache_lock);
324
325         list_for_each(list, &dentry->d_subdirs) {
326                 struct dentry *de = list_entry(list, struct dentry, d_child);
327                 if (usbfs_positive(de)) {
328                         spin_unlock(&dcache_lock);
329                         return 0;
330                 }
331         }
332
333         spin_unlock(&dcache_lock);
334         return 1;
335 }
336
337 static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
338 {
339         struct inode *inode = dentry->d_inode;
340         down(&inode->i_sem);
341         dentry->d_inode->i_nlink--;
342         dput(dentry);
343         up(&inode->i_sem);
344         d_delete(dentry);
345         return 0;
346 }
347
348 static void d_unhash(struct dentry *dentry)
349 {
350         dget(dentry);
351         spin_lock(&dcache_lock);
352         switch (atomic_read(&dentry->d_count)) {
353         default:
354                 spin_unlock(&dcache_lock);
355                 shrink_dcache_parent(dentry);
356                 spin_lock(&dcache_lock);
357                 if (atomic_read(&dentry->d_count) != 2)
358                         break;
359         case 2:
360                 __d_drop(dentry);
361         }
362         spin_unlock(&dcache_lock);
363 }
364
365 static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
366 {
367         int error = -ENOTEMPTY;
368         struct inode * inode = dentry->d_inode;
369
370         down(&inode->i_sem);
371         d_unhash(dentry);
372         if (usbfs_empty(dentry)) {
373                 dentry->d_inode->i_nlink -= 2;
374                 dput(dentry);
375                 inode->i_flags |= S_DEAD;
376                 dir->i_nlink--;
377                 error = 0;
378         }
379         up(&inode->i_sem);
380         if (!error)
381                 d_delete(dentry);
382         dput(dentry);
383         return error;
384 }
385
386
387 /* default file operations */
388 static ssize_t default_read_file (struct file *file, char __user *buf,
389                                   size_t count, loff_t *ppos)
390 {
391         return 0;
392 }
393
394 static ssize_t default_write_file (struct file *file, const char __user *buf,
395                                    size_t count, loff_t *ppos)
396 {
397         return count;
398 }
399
400 static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
401 {
402         loff_t retval = -EINVAL;
403
404         down(&file->f_dentry->d_inode->i_sem);
405         switch(orig) {
406         case 0:
407                 if (offset > 0) {
408                         file->f_pos = offset;
409                         retval = file->f_pos;
410                 } 
411                 break;
412         case 1:
413                 if ((offset + file->f_pos) > 0) {
414                         file->f_pos += offset;
415                         retval = file->f_pos;
416                 } 
417                 break;
418         default:
419                 break;
420         }
421         up(&file->f_dentry->d_inode->i_sem);
422         return retval;
423 }
424
425 static int default_open (struct inode *inode, struct file *file)
426 {
427         if (inode->u.generic_ip)
428                 file->private_data = inode->u.generic_ip;
429
430         return 0;
431 }
432
433 static struct file_operations default_file_operations = {
434         .read =         default_read_file,
435         .write =        default_write_file,
436         .open =         default_open,
437         .llseek =       default_file_lseek,
438 };
439
440 static struct inode_operations usbfs_dir_inode_operations = {
441         .lookup =       simple_lookup,
442 };
443
444 static struct super_operations usbfs_ops = {
445         .statfs =       simple_statfs,
446         .drop_inode =   generic_delete_inode,
447         .remount_fs =   remount,
448 };
449
450 static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
451 {
452         struct inode *inode;
453         struct dentry *root;
454
455         sb->s_blocksize = PAGE_CACHE_SIZE;
456         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
457         sb->s_magic = USBDEVICE_SUPER_MAGIC;
458         sb->s_op = &usbfs_ops;
459         inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
460
461         if (!inode) {
462                 dbg("%s: could not get inode!",__FUNCTION__);
463                 return -ENOMEM;
464         }
465
466         root = d_alloc_root(inode);
467         if (!root) {
468                 dbg("%s: could not get root dentry!",__FUNCTION__);
469                 iput(inode);
470                 return -ENOMEM;
471         }
472         sb->s_root = root;
473         return 0;
474 }
475
476 static struct dentry * get_dentry(struct dentry *parent, const char *name)
477 {               
478         struct qstr qstr;
479
480         qstr.name = name;
481         qstr.len = strlen(name);
482         qstr.hash = full_name_hash(name,qstr.len);
483         return lookup_hash(&qstr,parent);
484 }               
485
486
487 /*
488  * fs_create_by_name - create a file, given a name
489  * @name:       name of file
490  * @mode:       type of file
491  * @parent:     dentry of directory to create it in
492  * @dentry:     resulting dentry of file
493  *
494  * This function handles both regular files and directories.
495  */
496 static int fs_create_by_name (const char *name, mode_t mode,
497                               struct dentry *parent, struct dentry **dentry)
498 {
499         int error = 0;
500
501         /* If the parent is not specified, we create it in the root.
502          * We need the root dentry to do this, which is in the super 
503          * block. A pointer to that is in the struct vfsmount that we
504          * have around.
505          */
506         if (!parent ) {
507                 if (usbfs_mount && usbfs_mount->mnt_sb) {
508                         parent = usbfs_mount->mnt_sb->s_root;
509                 }
510         }
511
512         if (!parent) {
513                 dbg("Ah! can not find a parent!");
514                 return -EFAULT;
515         }
516
517         *dentry = NULL;
518         down(&parent->d_inode->i_sem);
519         *dentry = get_dentry (parent, name);
520         if (!IS_ERR(dentry)) {
521                 if ((mode & S_IFMT) == S_IFDIR)
522                         error = usbfs_mkdir (parent->d_inode, *dentry, mode);
523                 else 
524                         error = usbfs_create (parent->d_inode, *dentry, mode);
525         } else
526                 error = PTR_ERR(dentry);
527         up(&parent->d_inode->i_sem);
528
529         return error;
530 }
531
532 static struct dentry *fs_create_file (const char *name, mode_t mode,
533                                       struct dentry *parent, void *data,
534                                       struct file_operations *fops,
535                                       uid_t uid, gid_t gid)
536 {
537         struct dentry *dentry;
538         int error;
539
540         dbg("creating file '%s'",name);
541
542         error = fs_create_by_name (name, mode, parent, &dentry);
543         if (error) {
544                 dentry = NULL;
545         } else {
546                 if (dentry->d_inode) {
547                         if (data)
548                                 dentry->d_inode->u.generic_ip = data;
549                         if (fops)
550                                 dentry->d_inode->i_fop = fops;
551                         dentry->d_inode->i_uid = uid;
552                         dentry->d_inode->i_gid = gid;
553                 }
554         }
555
556         return dentry;
557 }
558
559 static void fs_remove_file (struct dentry *dentry)
560 {
561         struct dentry *parent = dentry->d_parent;
562         
563         if (!parent || !parent->d_inode)
564                 return;
565
566         down(&parent->d_inode->i_sem);
567         if (usbfs_positive(dentry)) {
568                 if (dentry->d_inode) {
569                         if (S_ISDIR(dentry->d_inode->i_mode))
570                                 usbfs_rmdir(parent->d_inode, dentry);
571                         else
572                                 usbfs_unlink(parent->d_inode, dentry);
573                 dput(dentry);
574                 }
575         }
576         up(&parent->d_inode->i_sem);
577 }
578
579 /* --------------------------------------------------------------------- */
580
581
582
583 /*
584  * The usbdevfs name is now deprecated (as of 2.5.1).
585  * It will be removed when the 2.7.x development cycle is started.
586  * You have been warned :)
587  */
588 static struct file_system_type usbdevice_fs_type;
589
590 static struct super_block *usb_get_sb(struct file_system_type *fs_type,
591         int flags, const char *dev_name, void *data)
592 {
593         return get_sb_single(fs_type, flags, data, usbfs_fill_super);
594 }
595
596 static struct file_system_type usbdevice_fs_type = {
597         .owner =        THIS_MODULE,
598         .name =         "usbdevfs",
599         .get_sb =       usb_get_sb,
600         .kill_sb =      kill_litter_super,
601 };
602
603 static struct file_system_type usb_fs_type = {
604         .owner =        THIS_MODULE,
605         .name =         "usbfs",
606         .get_sb =       usb_get_sb,
607         .kill_sb =      kill_litter_super,
608 };
609
610 /* --------------------------------------------------------------------- */
611
612 static int create_special_files (void)
613 {
614         struct dentry *parent;
615         int retval;
616
617         /* the simple_pin_fs calls will call remount with no options
618          * without this flag that would overwrite the real mount options (if any)
619          */
620         ignore_mount = 1;
621
622         /* create the devices special file */
623         retval = simple_pin_fs("usbdevfs", &usbdevfs_mount, &usbdevfs_mount_count);
624         if (retval) {
625                 err ("Unable to get usbdevfs mount");
626                 goto exit;
627         }
628
629         retval = simple_pin_fs("usbfs", &usbfs_mount, &usbfs_mount_count);
630         if (retval) {
631                 err ("Unable to get usbfs mount");
632                 goto error_clean_usbdevfs_mount;
633         }
634
635         ignore_mount = 0;
636
637         parent = usbfs_mount->mnt_sb->s_root;
638         devices_usbfs_dentry = fs_create_file ("devices",
639                                                listmode | S_IFREG, parent,
640                                                NULL, &usbdevfs_devices_fops,
641                                                listuid, listgid);
642         if (devices_usbfs_dentry == NULL) {
643                 err ("Unable to create devices usbfs file");
644                 retval = -ENODEV;
645                 goto error_clean_mounts;
646         }
647
648         parent = usbdevfs_mount->mnt_sb->s_root;
649         devices_usbdevfs_dentry = fs_create_file ("devices",
650                                                   listmode | S_IFREG, parent,
651                                                   NULL, &usbdevfs_devices_fops,
652                                                   listuid, listgid);
653         if (devices_usbdevfs_dentry == NULL) {
654                 err ("Unable to create devices usbfs file");
655                 retval = -ENODEV;
656                 goto error_remove_file;
657         }
658
659         goto exit;
660         
661 error_remove_file:
662         fs_remove_file (devices_usbfs_dentry);
663         devices_usbfs_dentry = NULL;
664
665 error_clean_mounts:
666         simple_release_fs(&usbfs_mount, &usbfs_mount_count);
667
668 error_clean_usbdevfs_mount:
669         simple_release_fs(&usbdevfs_mount, &usbdevfs_mount_count);
670
671 exit:
672         return retval;
673 }
674
675 static void remove_special_files (void)
676 {
677         if (devices_usbdevfs_dentry)
678                 fs_remove_file (devices_usbdevfs_dentry);
679         if (devices_usbfs_dentry)
680                 fs_remove_file (devices_usbfs_dentry);
681         devices_usbdevfs_dentry = NULL;
682         devices_usbfs_dentry = NULL;
683         simple_release_fs(&usbdevfs_mount, &usbdevfs_mount_count);
684         simple_release_fs(&usbfs_mount, &usbfs_mount_count);
685 }
686
687 void usbfs_update_special (void)
688 {
689         struct inode *inode;
690
691         if (devices_usbdevfs_dentry) {
692                 inode = devices_usbdevfs_dentry->d_inode;
693                 if (inode)
694                         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
695         }
696         if (devices_usbfs_dentry) {
697                 inode = devices_usbfs_dentry->d_inode;
698                 if (inode)
699                         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
700         }
701 }
702
703 void usbfs_add_bus(struct usb_bus *bus)
704 {
705         struct dentry *parent;
706         char name[8];
707         int retval;
708
709         /* create the special files if this is the first bus added */
710         if (num_buses == 0) {
711                 retval = create_special_files();
712                 if (retval)
713                         return;
714         }
715         ++num_buses;
716
717         sprintf (name, "%03d", bus->busnum);
718
719         parent = usbfs_mount->mnt_sb->s_root;
720         bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
721                                             bus, NULL, busuid, busgid);
722         if (bus->usbfs_dentry == NULL) {
723                 err ("error creating usbfs bus entry");
724                 return;
725         }
726
727         parent = usbdevfs_mount->mnt_sb->s_root;
728         bus->usbdevfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
729                                                bus, NULL, busuid, busgid);
730         if (bus->usbdevfs_dentry == NULL) {
731                 err ("error creating usbdevfs bus entry");
732                 return;
733         }
734
735         usbfs_update_special();
736         usbdevfs_conn_disc_event();
737 }
738
739
740 void usbfs_remove_bus(struct usb_bus *bus)
741 {
742         if (bus->usbfs_dentry) {
743                 fs_remove_file (bus->usbfs_dentry);
744                 bus->usbfs_dentry = NULL;
745         }
746         if (bus->usbdevfs_dentry) {
747                 fs_remove_file (bus->usbdevfs_dentry);
748                 bus->usbdevfs_dentry = NULL;
749         }
750
751         --num_buses;
752         if (num_buses <= 0) {
753                 remove_special_files();
754                 num_buses = 0;
755         }
756
757         usbfs_update_special();
758         usbdevfs_conn_disc_event();
759 }
760
761 void usbfs_add_device(struct usb_device *dev)
762 {
763         char name[8];
764         int i;
765         int i_size;
766
767         sprintf (name, "%03d", dev->devnum);
768         dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
769                                             dev->bus->usbfs_dentry, dev,
770                                             &usbdevfs_device_file_operations,
771                                             devuid, devgid);
772         if (dev->usbfs_dentry == NULL) {
773                 err ("error creating usbfs device entry");
774                 return;
775         }
776         dev->usbdevfs_dentry = fs_create_file (name, devmode | S_IFREG,
777                                                dev->bus->usbdevfs_dentry, dev,
778                                                &usbdevfs_device_file_operations,
779                                                devuid, devgid);
780         if (dev->usbdevfs_dentry == NULL) {
781                 err ("error creating usbdevfs device entry");
782                 return;
783         }
784
785         /* Set the size of the device's file to be
786          * equal to the size of the device descriptors. */
787         i_size = sizeof (struct usb_device_descriptor);
788         for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
789                 struct usb_config_descriptor *config =
790                         (struct usb_config_descriptor *)dev->rawdescriptors[i];
791                 i_size += le16_to_cpu (config->wTotalLength);
792         }
793         if (dev->usbfs_dentry->d_inode)
794                 dev->usbfs_dentry->d_inode->i_size = i_size;
795         if (dev->usbdevfs_dentry->d_inode)
796                 dev->usbdevfs_dentry->d_inode->i_size = i_size;
797
798         usbfs_update_special();
799         usbdevfs_conn_disc_event();
800 }
801
802 void usbfs_remove_device(struct usb_device *dev)
803 {
804         struct dev_state *ds;
805         struct siginfo sinfo;
806
807         if (dev->usbfs_dentry) {
808                 fs_remove_file (dev->usbfs_dentry);
809                 dev->usbfs_dentry = NULL;
810         }
811         if (dev->usbdevfs_dentry) {
812                 fs_remove_file (dev->usbdevfs_dentry);
813                 dev->usbdevfs_dentry = NULL;
814         }
815         while (!list_empty(&dev->filelist)) {
816                 ds = list_entry(dev->filelist.next, struct dev_state, list);
817                 list_del_init(&ds->list);
818                 if (ds->discsignr) {
819                         sinfo.si_signo = SIGPIPE;
820                         sinfo.si_errno = EPIPE;
821                         sinfo.si_code = SI_ASYNCIO;
822                         sinfo.si_addr = ds->disccontext;
823                         send_sig_info(ds->discsignr, &sinfo, ds->disctask);
824                 }
825         }
826         usbfs_update_special();
827         usbdevfs_conn_disc_event();
828 }
829
830 /* --------------------------------------------------------------------- */
831
832 #ifdef CONFIG_PROC_FS           
833 static struct proc_dir_entry *usbdir = NULL;
834 #endif  
835
836 int __init usbfs_init(void)
837 {
838         int retval;
839
840         retval = usb_register(&usbdevfs_driver);
841         if (retval)
842                 return retval;
843
844         retval = register_filesystem(&usb_fs_type);
845         if (retval) {
846                 usb_deregister(&usbdevfs_driver);
847                 return retval;
848         }
849         retval = register_filesystem(&usbdevice_fs_type);
850         if (retval) {
851                 unregister_filesystem(&usb_fs_type);
852                 usb_deregister(&usbdevfs_driver);
853                 return retval;
854         }
855
856 #ifdef CONFIG_PROC_FS           
857         /* create mount point for usbdevfs */
858         usbdir = proc_mkdir("usb", proc_bus);
859 #endif  
860
861         return 0;
862 }
863
864 void usbfs_cleanup(void)
865 {
866         usb_deregister(&usbdevfs_driver);
867         unregister_filesystem(&usb_fs_type);
868         unregister_filesystem(&usbdevice_fs_type);
869 #ifdef CONFIG_PROC_FS   
870         if (usbdir)
871                 remove_proc_entry("usb", proc_bus);
872 #endif
873 }
874