iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / usb / core / inode.c
blob83a373e9cc3624c66f09bac2c5383221e0eef8d2
1 /*****************************************************************************/
3 /*
4 * inode.c -- Inode/Dentry functions for the USB device file system.
6 * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7 * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
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.
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.
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.
23 * History:
24 * 0.1 04.01.2000 Created
25 * 0.2 10.12.2001 converted to use the vfs layer better
28 /*****************************************************************************/
30 #include <linux/module.h>
31 #include <linux/fs.h>
32 #include <linux/mount.h>
33 #include <linux/pagemap.h>
34 #include <linux/init.h>
35 #include <linux/proc_fs.h>
36 #include <linux/usb.h>
37 #include <linux/namei.h>
38 #include <linux/usbdevice_fs.h>
39 #include <linux/parser.h>
40 #include <linux/notifier.h>
41 #include <linux/seq_file.h>
42 #include <asm/byteorder.h>
43 #include "usb.h"
44 #include "hcd.h"
46 #define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
47 #define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
48 #define USBFS_DEFAULT_LISTMODE S_IRUGO
50 static struct super_operations usbfs_ops;
51 static const struct file_operations default_file_operations;
52 static struct vfsmount *usbfs_mount;
53 static int usbfs_mount_count; /* = 0 */
54 static int ignore_mount = 0;
56 static struct dentry *devices_usbfs_dentry;
57 static int num_buses; /* = 0 */
59 static uid_t devuid; /* = 0 */
60 static uid_t busuid; /* = 0 */
61 static uid_t listuid; /* = 0 */
62 static gid_t devgid; /* = 0 */
63 static gid_t busgid; /* = 0 */
64 static gid_t listgid; /* = 0 */
65 static umode_t devmode = USBFS_DEFAULT_DEVMODE;
66 static umode_t busmode = USBFS_DEFAULT_BUSMODE;
67 static umode_t listmode = USBFS_DEFAULT_LISTMODE;
69 static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
71 if (devuid != 0)
72 seq_printf(seq, ",devuid=%u", devuid);
73 if (devgid != 0)
74 seq_printf(seq, ",devgid=%u", devgid);
75 if (devmode != USBFS_DEFAULT_DEVMODE)
76 seq_printf(seq, ",devmode=%o", devmode);
77 if (busuid != 0)
78 seq_printf(seq, ",busuid=%u", busuid);
79 if (busgid != 0)
80 seq_printf(seq, ",busgid=%u", busgid);
81 if (busmode != USBFS_DEFAULT_BUSMODE)
82 seq_printf(seq, ",busmode=%o", busmode);
83 if (listuid != 0)
84 seq_printf(seq, ",listuid=%u", listuid);
85 if (listgid != 0)
86 seq_printf(seq, ",listgid=%u", listgid);
87 if (listmode != USBFS_DEFAULT_LISTMODE)
88 seq_printf(seq, ",listmode=%o", listmode);
90 return 0;
93 enum {
94 Opt_devuid, Opt_devgid, Opt_devmode,
95 Opt_busuid, Opt_busgid, Opt_busmode,
96 Opt_listuid, Opt_listgid, Opt_listmode,
97 Opt_err,
100 static match_table_t tokens = {
101 {Opt_devuid, "devuid=%u"},
102 {Opt_devgid, "devgid=%u"},
103 {Opt_devmode, "devmode=%o"},
104 {Opt_busuid, "busuid=%u"},
105 {Opt_busgid, "busgid=%u"},
106 {Opt_busmode, "busmode=%o"},
107 {Opt_listuid, "listuid=%u"},
108 {Opt_listgid, "listgid=%u"},
109 {Opt_listmode, "listmode=%o"},
110 {Opt_err, NULL}
113 static int parse_options(struct super_block *s, char *data)
115 char *p;
116 int option;
118 /* (re)set to defaults. */
119 devuid = 0;
120 busuid = 0;
121 listuid = 0;
122 devgid = 0;
123 busgid = 0;
124 listgid = 0;
125 devmode = USBFS_DEFAULT_DEVMODE;
126 busmode = USBFS_DEFAULT_BUSMODE;
127 listmode = USBFS_DEFAULT_LISTMODE;
129 while ((p = strsep(&data, ",")) != NULL) {
130 substring_t args[MAX_OPT_ARGS];
131 int token;
132 if (!*p)
133 continue;
135 token = match_token(p, tokens, args);
136 switch (token) {
137 case Opt_devuid:
138 if (match_int(&args[0], &option))
139 return -EINVAL;
140 devuid = option;
141 break;
142 case Opt_devgid:
143 if (match_int(&args[0], &option))
144 return -EINVAL;
145 devgid = option;
146 break;
147 case Opt_devmode:
148 if (match_octal(&args[0], &option))
149 return -EINVAL;
150 devmode = option & S_IRWXUGO;
151 break;
152 case Opt_busuid:
153 if (match_int(&args[0], &option))
154 return -EINVAL;
155 busuid = option;
156 break;
157 case Opt_busgid:
158 if (match_int(&args[0], &option))
159 return -EINVAL;
160 busgid = option;
161 break;
162 case Opt_busmode:
163 if (match_octal(&args[0], &option))
164 return -EINVAL;
165 busmode = option & S_IRWXUGO;
166 break;
167 case Opt_listuid:
168 if (match_int(&args[0], &option))
169 return -EINVAL;
170 listuid = option;
171 break;
172 case Opt_listgid:
173 if (match_int(&args[0], &option))
174 return -EINVAL;
175 listgid = option;
176 break;
177 case Opt_listmode:
178 if (match_octal(&args[0], &option))
179 return -EINVAL;
180 listmode = option & S_IRWXUGO;
181 break;
182 default:
183 err("usbfs: unrecognised mount option \"%s\" "
184 "or missing value\n", p);
185 return -EINVAL;
189 return 0;
192 static void update_special(struct dentry *special)
194 special->d_inode->i_uid = listuid;
195 special->d_inode->i_gid = listgid;
196 special->d_inode->i_mode = S_IFREG | listmode;
199 static void update_dev(struct dentry *dev)
201 dev->d_inode->i_uid = devuid;
202 dev->d_inode->i_gid = devgid;
203 dev->d_inode->i_mode = S_IFREG | devmode;
206 static void update_bus(struct dentry *bus)
208 struct dentry *dev = NULL;
210 bus->d_inode->i_uid = busuid;
211 bus->d_inode->i_gid = busgid;
212 bus->d_inode->i_mode = S_IFDIR | busmode;
214 mutex_lock(&bus->d_inode->i_mutex);
216 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
217 if (dev->d_inode)
218 update_dev(dev);
220 mutex_unlock(&bus->d_inode->i_mutex);
223 static void update_sb(struct super_block *sb)
225 struct dentry *root = sb->s_root;
226 struct dentry *bus = NULL;
228 if (!root)
229 return;
231 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
233 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
234 if (bus->d_inode) {
235 switch (S_IFMT & bus->d_inode->i_mode) {
236 case S_IFDIR:
237 update_bus(bus);
238 break;
239 case S_IFREG:
240 update_special(bus);
241 break;
242 default:
243 warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode);
244 break;
249 mutex_unlock(&root->d_inode->i_mutex);
252 static int remount(struct super_block *sb, int *flags, char *data)
254 /* If this is not a real mount,
255 * i.e. it's a simple_pin_fs from create_special_files,
256 * then ignore it.
258 if (ignore_mount)
259 return 0;
261 if (parse_options(sb, data)) {
262 warn("usbfs: mount parameter error:");
263 return -EINVAL;
266 if (usbfs_mount && usbfs_mount->mnt_sb)
267 update_sb(usbfs_mount->mnt_sb);
269 return 0;
272 static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
274 struct inode *inode = new_inode(sb);
276 if (inode) {
277 inode->i_mode = mode;
278 inode->i_uid = current->fsuid;
279 inode->i_gid = current->fsgid;
280 inode->i_blocks = 0;
281 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
282 switch (mode & S_IFMT) {
283 default:
284 init_special_inode(inode, mode, dev);
285 break;
286 case S_IFREG:
287 inode->i_fop = &default_file_operations;
288 break;
289 case S_IFDIR:
290 inode->i_op = &simple_dir_inode_operations;
291 inode->i_fop = &simple_dir_operations;
293 /* directory inodes start off with i_nlink == 2 (for "." entry) */
294 inc_nlink(inode);
295 break;
298 return inode;
301 /* SMP-safe */
302 static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
303 dev_t dev)
305 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
306 int error = -EPERM;
308 if (dentry->d_inode)
309 return -EEXIST;
311 if (inode) {
312 d_instantiate(dentry, inode);
313 dget(dentry);
314 error = 0;
316 return error;
319 static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
321 int res;
323 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
324 res = usbfs_mknod (dir, dentry, mode, 0);
325 if (!res)
326 inc_nlink(dir);
327 return res;
330 static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
332 mode = (mode & S_IALLUGO) | S_IFREG;
333 return usbfs_mknod (dir, dentry, mode, 0);
336 static inline int usbfs_positive (struct dentry *dentry)
338 return dentry->d_inode && !d_unhashed(dentry);
341 static int usbfs_empty (struct dentry *dentry)
343 struct list_head *list;
345 spin_lock(&dcache_lock);
347 list_for_each(list, &dentry->d_subdirs) {
348 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
349 if (usbfs_positive(de)) {
350 spin_unlock(&dcache_lock);
351 return 0;
355 spin_unlock(&dcache_lock);
356 return 1;
359 static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
361 struct inode *inode = dentry->d_inode;
362 mutex_lock(&inode->i_mutex);
363 drop_nlink(dentry->d_inode);
364 dput(dentry);
365 mutex_unlock(&inode->i_mutex);
366 d_delete(dentry);
367 return 0;
370 static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
372 int error = -ENOTEMPTY;
373 struct inode * inode = dentry->d_inode;
375 mutex_lock(&inode->i_mutex);
376 dentry_unhash(dentry);
377 if (usbfs_empty(dentry)) {
378 drop_nlink(dentry->d_inode);
379 drop_nlink(dentry->d_inode);
380 dput(dentry);
381 inode->i_flags |= S_DEAD;
382 drop_nlink(dir);
383 error = 0;
385 mutex_unlock(&inode->i_mutex);
386 if (!error)
387 d_delete(dentry);
388 dput(dentry);
389 return error;
393 /* default file operations */
394 static ssize_t default_read_file (struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
397 return 0;
400 static ssize_t default_write_file (struct file *file, const char __user *buf,
401 size_t count, loff_t *ppos)
403 return count;
406 static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
408 loff_t retval = -EINVAL;
410 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
411 switch(orig) {
412 case 0:
413 if (offset > 0) {
414 file->f_pos = offset;
415 retval = file->f_pos;
417 break;
418 case 1:
419 if ((offset + file->f_pos) > 0) {
420 file->f_pos += offset;
421 retval = file->f_pos;
423 break;
424 default:
425 break;
427 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
428 return retval;
431 static int default_open (struct inode *inode, struct file *file)
433 if (inode->i_private)
434 file->private_data = inode->i_private;
436 return 0;
439 static const struct file_operations default_file_operations = {
440 .read = default_read_file,
441 .write = default_write_file,
442 .open = default_open,
443 .llseek = default_file_lseek,
446 static struct super_operations usbfs_ops = {
447 .statfs = simple_statfs,
448 .drop_inode = generic_delete_inode,
449 .remount_fs = remount,
450 .show_options = usbfs_show_options,
453 static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
455 struct inode *inode;
456 struct dentry *root;
458 sb->s_blocksize = PAGE_CACHE_SIZE;
459 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
460 sb->s_magic = USBDEVICE_SUPER_MAGIC;
461 sb->s_op = &usbfs_ops;
462 sb->s_time_gran = 1;
463 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
465 if (!inode) {
466 dbg("%s: could not get inode!",__FUNCTION__);
467 return -ENOMEM;
470 root = d_alloc_root(inode);
471 if (!root) {
472 dbg("%s: could not get root dentry!",__FUNCTION__);
473 iput(inode);
474 return -ENOMEM;
476 sb->s_root = root;
477 return 0;
481 * fs_create_by_name - create a file, given a name
482 * @name: name of file
483 * @mode: type of file
484 * @parent: dentry of directory to create it in
485 * @dentry: resulting dentry of file
487 * This function handles both regular files and directories.
489 static int fs_create_by_name (const char *name, mode_t mode,
490 struct dentry *parent, struct dentry **dentry)
492 int error = 0;
494 /* If the parent is not specified, we create it in the root.
495 * We need the root dentry to do this, which is in the super
496 * block. A pointer to that is in the struct vfsmount that we
497 * have around.
499 if (!parent ) {
500 if (usbfs_mount && usbfs_mount->mnt_sb) {
501 parent = usbfs_mount->mnt_sb->s_root;
505 if (!parent) {
506 dbg("Ah! can not find a parent!");
507 return -EFAULT;
510 *dentry = NULL;
511 mutex_lock(&parent->d_inode->i_mutex);
512 *dentry = lookup_one_len(name, parent, strlen(name));
513 if (!IS_ERR(dentry)) {
514 if ((mode & S_IFMT) == S_IFDIR)
515 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
516 else
517 error = usbfs_create (parent->d_inode, *dentry, mode);
518 } else
519 error = PTR_ERR(dentry);
520 mutex_unlock(&parent->d_inode->i_mutex);
522 return error;
525 static struct dentry *fs_create_file (const char *name, mode_t mode,
526 struct dentry *parent, void *data,
527 const struct file_operations *fops,
528 uid_t uid, gid_t gid)
530 struct dentry *dentry;
531 int error;
533 dbg("creating file '%s'",name);
535 error = fs_create_by_name (name, mode, parent, &dentry);
536 if (error) {
537 dentry = NULL;
538 } else {
539 if (dentry->d_inode) {
540 if (data)
541 dentry->d_inode->i_private = data;
542 if (fops)
543 dentry->d_inode->i_fop = fops;
544 dentry->d_inode->i_uid = uid;
545 dentry->d_inode->i_gid = gid;
549 return dentry;
552 static void fs_remove_file (struct dentry *dentry)
554 struct dentry *parent = dentry->d_parent;
556 if (!parent || !parent->d_inode)
557 return;
559 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
560 if (usbfs_positive(dentry)) {
561 if (dentry->d_inode) {
562 if (S_ISDIR(dentry->d_inode->i_mode))
563 usbfs_rmdir(parent->d_inode, dentry);
564 else
565 usbfs_unlink(parent->d_inode, dentry);
566 dput(dentry);
569 mutex_unlock(&parent->d_inode->i_mutex);
572 /* --------------------------------------------------------------------- */
574 static int usb_get_sb(struct file_system_type *fs_type,
575 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
577 return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt);
580 static struct file_system_type usb_fs_type = {
581 .owner = THIS_MODULE,
582 .name = "usbfs",
583 .get_sb = usb_get_sb,
584 .kill_sb = kill_litter_super,
587 /* --------------------------------------------------------------------- */
589 static int create_special_files (void)
591 struct dentry *parent;
592 int retval;
594 /* the simple_pin_fs calls will call remount with no options
595 * without this flag that would overwrite the real mount options (if any)
597 ignore_mount = 1;
599 /* create the devices special file */
600 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
601 if (retval) {
602 err ("Unable to get usbfs mount");
603 goto exit;
606 ignore_mount = 0;
608 parent = usbfs_mount->mnt_sb->s_root;
609 devices_usbfs_dentry = fs_create_file ("devices",
610 listmode | S_IFREG, parent,
611 NULL, &usbfs_devices_fops,
612 listuid, listgid);
613 if (devices_usbfs_dentry == NULL) {
614 err ("Unable to create devices usbfs file");
615 retval = -ENODEV;
616 goto error_clean_mounts;
619 goto exit;
621 error_clean_mounts:
622 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
623 exit:
624 return retval;
627 static void remove_special_files (void)
629 if (devices_usbfs_dentry)
630 fs_remove_file (devices_usbfs_dentry);
631 devices_usbfs_dentry = NULL;
632 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
635 void usbfs_update_special (void)
637 struct inode *inode;
639 if (devices_usbfs_dentry) {
640 inode = devices_usbfs_dentry->d_inode;
641 if (inode)
642 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
646 static void usbfs_add_bus(struct usb_bus *bus)
648 struct dentry *parent;
649 char name[8];
650 int retval;
652 /* create the special files if this is the first bus added */
653 if (num_buses == 0) {
654 retval = create_special_files();
655 if (retval)
656 return;
658 ++num_buses;
660 sprintf (name, "%03d", bus->busnum);
662 parent = usbfs_mount->mnt_sb->s_root;
663 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
664 bus, NULL, busuid, busgid);
665 if (bus->usbfs_dentry == NULL) {
666 err ("error creating usbfs bus entry");
667 return;
671 static void usbfs_remove_bus(struct usb_bus *bus)
673 if (bus->usbfs_dentry) {
674 fs_remove_file (bus->usbfs_dentry);
675 bus->usbfs_dentry = NULL;
678 --num_buses;
679 if (num_buses <= 0) {
680 remove_special_files();
681 num_buses = 0;
685 static void usbfs_add_device(struct usb_device *dev)
687 char name[8];
688 int i;
689 int i_size;
691 sprintf (name, "%03d", dev->devnum);
692 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
693 dev->bus->usbfs_dentry, dev,
694 &usbdev_file_operations,
695 devuid, devgid);
696 if (dev->usbfs_dentry == NULL) {
697 err ("error creating usbfs device entry");
698 return;
701 /* Set the size of the device's file to be
702 * equal to the size of the device descriptors. */
703 i_size = sizeof (struct usb_device_descriptor);
704 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
705 struct usb_config_descriptor *config =
706 (struct usb_config_descriptor *)dev->rawdescriptors[i];
707 i_size += le16_to_cpu(config->wTotalLength);
709 if (dev->usbfs_dentry->d_inode)
710 dev->usbfs_dentry->d_inode->i_size = i_size;
713 static void usbfs_remove_device(struct usb_device *dev)
715 struct dev_state *ds;
716 struct siginfo sinfo;
718 if (dev->usbfs_dentry) {
719 fs_remove_file (dev->usbfs_dentry);
720 dev->usbfs_dentry = NULL;
722 while (!list_empty(&dev->filelist)) {
723 ds = list_entry(dev->filelist.next, struct dev_state, list);
724 wake_up_all(&ds->wait);
725 list_del_init(&ds->list);
726 if (ds->discsignr) {
727 sinfo.si_signo = ds->discsignr;
728 sinfo.si_errno = EPIPE;
729 sinfo.si_code = SI_ASYNCIO;
730 sinfo.si_addr = ds->disccontext;
731 kill_pid_info_as_uid(ds->discsignr, &sinfo, ds->disc_pid, ds->disc_uid, ds->disc_euid, ds->secid);
736 static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
738 switch (action) {
739 case USB_DEVICE_ADD:
740 usbfs_add_device(dev);
741 break;
742 case USB_DEVICE_REMOVE:
743 usbfs_remove_device(dev);
744 break;
745 case USB_BUS_ADD:
746 usbfs_add_bus(dev);
747 break;
748 case USB_BUS_REMOVE:
749 usbfs_remove_bus(dev);
752 usbfs_update_special();
753 usbfs_conn_disc_event();
754 return NOTIFY_OK;
757 static struct notifier_block usbfs_nb = {
758 .notifier_call = usbfs_notify,
761 /* --------------------------------------------------------------------- */
763 static struct proc_dir_entry *usbdir = NULL;
765 int __init usbfs_init(void)
767 int retval;
769 retval = register_filesystem(&usb_fs_type);
770 if (retval)
771 return retval;
773 usb_register_notify(&usbfs_nb);
775 /* create mount point for usbfs */
776 usbdir = proc_mkdir("usb", proc_bus);
778 return 0;
781 void usbfs_cleanup(void)
783 usb_unregister_notify(&usbfs_nb);
784 unregister_filesystem(&usb_fs_type);
785 if (usbdir)
786 remove_proc_entry("usb", proc_bus);