1 /*****************************************************************************/
2 
3 /*
4  *	inode.c  --  Inode/Dentry functions for the USB device file system.
5  *
6  *	Copyright (C) 2000
7  *          Thomas Sailer (sailer@ife.ee.ethz.ch)
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  *  $Id: inode.c,v 1.3 2000/01/11 13:58:25 tom Exp $
24  *
25  *  History:
26  *   0.1  04.01.2000  Created
27  */
28 
29 /*****************************************************************************/
30 
31 #define __NO_VERSION__
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/fs.h>
35 #include <linux/sched.h>
36 #include <linux/smp_lock.h>
37 #include <linux/locks.h>
38 #include <linux/init.h>
39 #include <linux/proc_fs.h>
40 #include <linux/usb.h>
41 #include <linux/usbdevice_fs.h>
42 #include <asm/uaccess.h>
43 
44 static struct inode_operations usbdevfs_bus_inode_operations;
45 static struct file_operations usbdevfs_bus_file_operations;
46 
47 /* --------------------------------------------------------------------- */
48 
49 /*
50  * This list of superblocks is still used,
51  * but since usbdevfs became FS_SINGLE
52  * there is only one super_block.
53  */
54 static LIST_HEAD(superlist);
55 
56 struct special {
57 	const char *name;
58 	struct file_operations *fops;
59 	struct inode *inode;
60 	struct list_head inodes;
61 };
62 
63 static struct special special[] = {
64 	{ "devices", &usbdevfs_devices_fops,  },
65 	{ "drivers", &usbdevfs_drivers_fops,  }
66 };
67 
68 #define NRSPECIAL (sizeof(special)/sizeof(special[0]))
69 
70 /* --------------------------------------------------------------------- */
71 
dnumber(struct dentry * dentry)72 static int dnumber(struct dentry *dentry)
73 {
74 	const char *name;
75 	unsigned int s;
76 
77 	if (dentry->d_name.len != 3)
78 		return -1;
79 	name = dentry->d_name.name;
80 	if (name[0] < '0' || name[0] > '9' ||
81 	    name[1] < '0' || name[1] > '9' ||
82 	    name[2] < '0' || name[2] > '9')
83 		return -1;
84 	s = name[0] - '0';
85 	s = s * 10 + name[1] - '0';
86 	s = s * 10 + name[2] - '0';
87 	return s;
88 }
89 
90 /*
91  * utility functions; should be called with the kernel lock held
92  * to protect against busses/devices appearing/disappearing
93  */
94 
new_dev_inode(struct usb_device * dev,struct super_block * sb)95 static void new_dev_inode(struct usb_device *dev, struct super_block *sb)
96 {
97 	struct inode *inode;
98 	unsigned int devnum = dev->devnum;
99 	unsigned int busnum = dev->bus->busnum;
100 
101 	if (devnum < 1 || devnum > 127 || busnum > 255)
102 		return;
103 	inode = iget(sb, IDEVICE | (busnum << 8) | devnum);
104 	if (!inode) {
105 		printk(KERN_ERR "usbdevfs: cannot create inode for bus %u device %u\n", busnum, devnum);
106 		return;
107 	}
108 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
109 	inode->i_uid = sb->u.usbdevfs_sb.devuid;
110 	inode->i_gid = sb->u.usbdevfs_sb.devgid;
111 	inode->i_mode = sb->u.usbdevfs_sb.devmode | S_IFREG;
112 	inode->i_fop = &usbdevfs_device_file_operations;
113 	inode->i_size = sizeof(struct usb_device_descriptor);
114 	inode->u.usbdev_i.p.dev = dev;
115 	list_add_tail(&inode->u.usbdev_i.slist, &sb->u.usbdevfs_sb.ilist);
116 	list_add_tail(&inode->u.usbdev_i.dlist, &dev->inodes);
117 }
118 
recurse_new_dev_inode(struct usb_device * dev,struct super_block * sb)119 static void recurse_new_dev_inode(struct usb_device *dev, struct super_block *sb)
120 {
121 	unsigned int i;
122 
123 	if (!dev)
124 		return;
125 	new_dev_inode(dev, sb);
126 	for (i = 0; i < dev->maxchild; i++) {
127                 if (!dev->children[i])
128                         continue;
129 		recurse_new_dev_inode(dev->children[i], sb);
130 	}
131 }
132 
new_bus_inode(struct usb_bus * bus,struct super_block * sb)133 static void new_bus_inode(struct usb_bus *bus, struct super_block *sb)
134 {
135 	struct inode *inode;
136 	unsigned int busnum = bus->busnum;
137 
138 	if (busnum > 255)
139 		return;
140 	inode = iget(sb, IBUS | (busnum << 8));
141 	if (!inode) {
142 		printk(KERN_ERR "usbdevfs: cannot create inode for bus %u\n", busnum);
143 		return;
144 	}
145 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
146 	inode->i_uid = sb->u.usbdevfs_sb.busuid;
147 	inode->i_gid = sb->u.usbdevfs_sb.busgid;
148 	inode->i_mode = sb->u.usbdevfs_sb.busmode | S_IFDIR;
149 	inode->i_op = &usbdevfs_bus_inode_operations;
150 	inode->i_fop = &usbdevfs_bus_file_operations;
151 	inode->u.usbdev_i.p.bus = bus;
152 	list_add_tail(&inode->u.usbdev_i.slist, &sb->u.usbdevfs_sb.ilist);
153 	list_add_tail(&inode->u.usbdev_i.dlist, &bus->inodes);
154 }
155 
free_inode(struct inode * inode)156 static void free_inode(struct inode *inode)
157 {
158 	inode->u.usbdev_i.p.bus = NULL;
159 	inode->u.usbdev_i.p.dev = NULL;
160 	inode->i_mode &= ~S_IRWXUGO;
161 	inode->i_uid = inode->i_gid = 0;
162 	inode->i_size = 0;
163 	list_del(&inode->u.usbdev_i.slist);
164 	list_del(&inode->u.usbdev_i.dlist);
165 	iput(inode);
166 }
167 
parse_options(struct super_block * s,char * data)168 static int parse_options(struct super_block *s, char *data)
169 {
170 	uid_t devuid = 0, busuid = 0, listuid = 0;
171 	gid_t devgid = 0, busgid = 0, listgid = 0;
172 	umode_t devmode = S_IWUSR | S_IRUGO, busmode = S_IXUGO | S_IRUGO, listmode = S_IRUGO;
173 	char *curopt = NULL, *value;
174 
175 	/* parse options */
176 	if (data)
177 		curopt = strtok(data, ",");
178 	for (; curopt; curopt = strtok(NULL, ",")) {
179 		if ((value = strchr(curopt, '=')) != NULL)
180 			*value++ = 0;
181 		if (!strcmp(curopt, "devuid")) {
182 			if (!value || !value[0])
183 				return -EINVAL;
184 			devuid = simple_strtoul(value, &value, 0);
185 			if (*value)
186 				return -EINVAL;
187 		}
188 		if (!strcmp(curopt, "devgid")) {
189 			if (!value || !value[0])
190 				return -EINVAL;
191 			devgid = simple_strtoul(value, &value, 0);
192 			if (*value)
193 				return -EINVAL;
194 		}
195 		if (!strcmp(curopt, "devmode")) {
196 			if (!value || !value[0])
197 				return -EINVAL;
198 			devmode = simple_strtoul(value, &value, 0) & S_IRWXUGO;
199 			if (*value)
200 				return -EINVAL;
201 		}
202 		if (!strcmp(curopt, "busuid")) {
203 			if (!value || !value[0])
204 				return -EINVAL;
205 			busuid = simple_strtoul(value, &value, 0);
206 			if (*value)
207 				return -EINVAL;
208 		}
209 		if (!strcmp(curopt, "busgid")) {
210 			if (!value || !value[0])
211 				return -EINVAL;
212 			busgid = simple_strtoul(value, &value, 0);
213 			if (*value)
214 				return -EINVAL;
215 		}
216 		if (!strcmp(curopt, "busmode")) {
217 			if (!value || !value[0])
218 				return -EINVAL;
219 			busmode = simple_strtoul(value, &value, 0) & S_IRWXUGO;
220 			if (*value)
221 				return -EINVAL;
222 		}
223 		if (!strcmp(curopt, "listuid")) {
224 			if (!value || !value[0])
225 				return -EINVAL;
226 			listuid = simple_strtoul(value, &value, 0);
227 			if (*value)
228 				return -EINVAL;
229 		}
230 		if (!strcmp(curopt, "listgid")) {
231 			if (!value || !value[0])
232 				return -EINVAL;
233 			listgid = simple_strtoul(value, &value, 0);
234 			if (*value)
235 				return -EINVAL;
236 		}
237 		if (!strcmp(curopt, "listmode")) {
238 			if (!value || !value[0])
239 				return -EINVAL;
240 			listmode = simple_strtoul(value, &value, 0) & S_IRWXUGO;
241 			if (*value)
242 				return -EINVAL;
243 		}
244 	}
245 
246 	s->u.usbdevfs_sb.devuid = devuid;
247 	s->u.usbdevfs_sb.devgid = devgid;
248 	s->u.usbdevfs_sb.devmode = devmode;
249 	s->u.usbdevfs_sb.busuid = busuid;
250 	s->u.usbdevfs_sb.busgid = busgid;
251 	s->u.usbdevfs_sb.busmode = busmode;
252 	s->u.usbdevfs_sb.listuid = listuid;
253 	s->u.usbdevfs_sb.listgid = listgid;
254 	s->u.usbdevfs_sb.listmode = listmode;
255 
256 	return 0;
257 }
258 
usbdevfs_findbus(int busnr)259 static struct usb_bus *usbdevfs_findbus(int busnr)
260 {
261         struct list_head *list;
262         struct usb_bus *bus;
263 
264 	down (&usb_bus_list_lock);
265         for (list = usb_bus_list.next; list != &usb_bus_list; list = list->next) {
266                 bus = list_entry(list, struct usb_bus, bus_list);
267                 if (bus->busnum == busnr) {
268 			up (&usb_bus_list_lock);
269                         return bus;
270 		}
271         }
272 	up (&usb_bus_list_lock);
273         return NULL;
274 }
275 
276 #if 0
277 static struct usb_device *finddev(struct usb_device *dev, int devnr)
278 {
279         unsigned int i;
280         struct usb_device *d2;
281 
282         if (!dev)
283                 return NULL;
284         if (dev->devnum == devnr)
285                 return dev;
286         for (i = 0; i < dev->maxchild; i++) {
287                 if (!dev->children[i])
288                         continue;
289                 if ((d2 = finddev(dev->children[i], devnr)))
290                         return d2;
291         }
292         return NULL;
293 }
294 
295 static struct usb_device *usbdevfs_finddevice(struct usb_bus *bus, int devnr)
296 {
297         return finddev(bus->root_hub, devnr);
298 }
299 #endif
300 
301 /* --------------------------------------------------------------------- */
302 
usbdevfs_revalidate(struct dentry * dentry,int flags)303 static int usbdevfs_revalidate(struct dentry *dentry, int flags)
304 {
305 	struct inode *inode = dentry->d_inode;
306 
307         if (!inode)
308                 return 0;
309 	if (ITYPE(inode->i_ino) == IBUS && !inode->u.usbdev_i.p.bus)
310 		return 0;
311 	if (ITYPE(inode->i_ino) == IDEVICE && !inode->u.usbdev_i.p.dev)
312 		return 0;
313 	return 1;
314 }
315 
316 static struct dentry_operations usbdevfs_dentry_operations = {
317 	d_revalidate:	usbdevfs_revalidate,
318 };
319 
usbdevfs_root_lookup(struct inode * dir,struct dentry * dentry)320 static struct dentry *usbdevfs_root_lookup(struct inode *dir, struct dentry *dentry)
321 {
322 	int busnr;
323 	unsigned long ino = 0;
324 	unsigned int i;
325 	struct inode *inode;
326 
327 	/* sanity check */
328 	if (dir->i_ino != IROOT)
329 		return ERR_PTR(-EINVAL);
330 	dentry->d_op = &usbdevfs_dentry_operations;
331 	busnr = dnumber(dentry);
332 	if (busnr >= 0 && busnr <= 255)
333 		ino = IBUS | (busnr << 8);
334 	if (!ino) {
335 		for (i = 0; i < NRSPECIAL; i++) {
336 			if (strlen(special[i].name) == dentry->d_name.len &&
337 			    !strncmp(special[i].name, dentry->d_name.name, dentry->d_name.len)) {
338 				ino = ISPECIAL | (i + IROOT + 1);
339 				break;
340 			}
341 		}
342 	}
343 	if (!ino)
344 		return ERR_PTR(-ENOENT);
345 	inode = iget(dir->i_sb, ino);
346 	if (!inode)
347 		return ERR_PTR(-EINVAL);
348 	if (inode && ITYPE(ino) == IBUS && inode->u.usbdev_i.p.bus == NULL) {
349 		iput(inode);
350 		inode = NULL;
351 	}
352 	d_add(dentry, inode);
353 	return NULL;
354 }
355 
usbdevfs_bus_lookup(struct inode * dir,struct dentry * dentry)356 static struct dentry *usbdevfs_bus_lookup(struct inode *dir, struct dentry *dentry)
357 {
358 	struct inode *inode;
359 	int devnr;
360 
361 	/* sanity check */
362 	if (ITYPE(dir->i_ino) != IBUS)
363 		return ERR_PTR(-EINVAL);
364 	dentry->d_op = &usbdevfs_dentry_operations;
365 	devnr = dnumber(dentry);
366 	if (devnr < 1 || devnr > 127)
367 		return ERR_PTR(-ENOENT);
368 	inode = iget(dir->i_sb, IDEVICE | (dir->i_ino & (0xff << 8)) | devnr);
369 	if (!inode)
370 		return ERR_PTR(-EINVAL);
371 	if (inode && inode->u.usbdev_i.p.dev == NULL) {
372 		iput(inode);
373 		inode = NULL;
374 	}
375 	d_add(dentry, inode);
376 	return NULL;
377 }
378 
usbdevfs_root_readdir(struct file * filp,void * dirent,filldir_t filldir)379 static int usbdevfs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
380 {
381 	struct inode *inode = filp->f_dentry->d_inode;
382 	unsigned long ino = inode->i_ino;
383 	struct special *spec;
384 	struct list_head *list;
385 	struct usb_bus *bus;
386 	char numbuf[8];
387 	unsigned int i;
388 
389 	/* sanity check */
390 	if (ino != IROOT)
391 		return -EINVAL;
392 	i = filp->f_pos;
393 	switch (i) {
394 	case 0:
395 		if (filldir(dirent, ".", 1, i, IROOT, DT_DIR) < 0)
396 			return 0;
397 		filp->f_pos++;
398 		i++;
399 		/* fall through */
400 
401 	case 1:
402 		if (filldir(dirent, "..", 2, i, IROOT, DT_DIR) < 0)
403 			return 0;
404 		filp->f_pos++;
405 		i++;
406 		/* fall through */
407 
408 	default:
409 
410 		while (i >= 2 && i < 2+NRSPECIAL) {
411 			spec = &special[filp->f_pos-2];
412 			if (filldir(dirent, spec->name, strlen(spec->name), i, ISPECIAL | (filp->f_pos-2+IROOT), DT_UNKNOWN) < 0)
413 				return 0;
414 			filp->f_pos++;
415 			i++;
416 		}
417 		if (i < 2+NRSPECIAL)
418 			return 0;
419 		i -= 2+NRSPECIAL;
420 		down (&usb_bus_list_lock);
421 		for (list = usb_bus_list.next; list != &usb_bus_list; list = list->next) {
422 			if (i > 0) {
423 				i--;
424 				continue;
425 			}
426 			bus = list_entry(list, struct usb_bus, bus_list);
427 			sprintf(numbuf, "%03d", bus->busnum);
428 			if (filldir(dirent, numbuf, 3, filp->f_pos, IBUS | ((bus->busnum & 0xff) << 8), DT_UNKNOWN) < 0)
429 				break;
430 			filp->f_pos++;
431 		}
432 		up (&usb_bus_list_lock);
433 		return 0;
434 	}
435 }
436 
bus_readdir(struct usb_device * dev,unsigned long ino,int pos,struct file * filp,void * dirent,filldir_t filldir)437 static int bus_readdir(struct usb_device *dev, unsigned long ino, int pos, struct file *filp, void *dirent, filldir_t filldir)
438 {
439 	char numbuf[8];
440 	unsigned int i;
441 
442 	if (!dev)
443 		return pos;
444 	sprintf(numbuf, "%03d", dev->devnum);
445 	if (pos > 0)
446 		pos--;
447 	else {
448 		if (filldir(dirent, numbuf, 3, filp->f_pos, ino | (dev->devnum & 0xff), DT_UNKNOWN) < 0)
449 			return -1;
450 		filp->f_pos++;
451 	}
452 	for (i = 0; i < dev->maxchild; i++) {
453 		if (!dev->children[i])
454 			continue;
455 		pos = bus_readdir(dev->children[i], ino, pos, filp, dirent, filldir);
456 		if (pos < 0)
457 			return -1;
458 	}
459 	return pos;
460 }
461 
usbdevfs_bus_readdir(struct file * filp,void * dirent,filldir_t filldir)462 static int usbdevfs_bus_readdir(struct file *filp, void *dirent, filldir_t filldir)
463 {
464 	struct inode *inode = filp->f_dentry->d_inode;
465 	unsigned long ino = inode->i_ino;
466 	struct usb_bus *bus;
467 
468 	/* sanity check */
469 	if (ITYPE(ino) != IBUS)
470 		return -EINVAL;
471 	switch ((unsigned int)filp->f_pos) {
472 	case 0:
473 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
474 			return 0;
475 		filp->f_pos++;
476 		/* fall through */
477 
478 	case 1:
479 		if (filldir(dirent, "..", 2, filp->f_pos, IROOT, DT_DIR) < 0)
480 			return 0;
481 		filp->f_pos++;
482 		/* fall through */
483 
484 	default:
485 		lock_kernel();
486 		bus = usbdevfs_findbus(IBUSNR(ino));
487 		bus_readdir(bus->root_hub, IDEVICE | ((bus->busnum & 0xff) << 8), filp->f_pos-2, filp, dirent, filldir);
488 		unlock_kernel();
489 		return 0;
490 	}
491 }
492 
493 static struct file_operations usbdevfs_root_file_operations = {
494 	readdir: usbdevfs_root_readdir,
495 };
496 
497 static struct inode_operations usbdevfs_root_inode_operations = {
498 	lookup: usbdevfs_root_lookup,
499 };
500 
501 static struct file_operations usbdevfs_bus_file_operations = {
502 	readdir: usbdevfs_bus_readdir,
503 };
504 
505 static struct inode_operations usbdevfs_bus_inode_operations = {
506 	lookup: usbdevfs_bus_lookup,
507 };
508 
usbdevfs_read_inode(struct inode * inode)509 static void usbdevfs_read_inode(struct inode *inode)
510 {
511 	struct special *spec;
512 
513 	inode->i_ctime = inode->i_mtime = inode->i_atime = CURRENT_TIME;
514 	inode->i_mode = S_IFREG;
515 	inode->i_gid = inode->i_uid = 0;
516 	inode->u.usbdev_i.p.dev = NULL;
517 	inode->u.usbdev_i.p.bus = NULL;
518 	switch (ITYPE(inode->i_ino)) {
519 	case ISPECIAL:
520 		if (inode->i_ino == IROOT) {
521 			inode->i_op = &usbdevfs_root_inode_operations;
522 			inode->i_fop = &usbdevfs_root_file_operations;
523 			inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
524 			return;
525 		}
526 		if (inode->i_ino <= IROOT || inode->i_ino > IROOT+NRSPECIAL)
527 			return;
528 		spec = &special[inode->i_ino-(IROOT+1)];
529 		inode->i_fop = spec->fops;
530 		return;
531 
532 	case IDEVICE:
533 		return;
534 
535 	case IBUS:
536 		return;
537 
538 	default:
539 		return;
540         }
541 }
542 
usbdevfs_put_super(struct super_block * sb)543 static void usbdevfs_put_super(struct super_block *sb)
544 {
545 	list_del(&sb->u.usbdevfs_sb.slist);
546 	INIT_LIST_HEAD(&sb->u.usbdevfs_sb.slist);
547 	while (!list_empty(&sb->u.usbdevfs_sb.ilist))
548 		free_inode(list_entry(sb->u.usbdevfs_sb.ilist.next, struct inode, u.usbdev_i.slist));
549 }
550 
usbdevfs_statfs(struct super_block * sb,struct statfs * buf)551 static int usbdevfs_statfs(struct super_block *sb, struct statfs *buf)
552 {
553         buf->f_type = USBDEVICE_SUPER_MAGIC;
554         buf->f_bsize = PAGE_SIZE/sizeof(long);   /* ??? */
555         buf->f_bfree = 0;
556         buf->f_bavail = 0;
557         buf->f_ffree = 0;
558         buf->f_namelen = NAME_MAX;
559         return 0;
560 }
561 
usbdevfs_remount(struct super_block * s,int * flags,char * data)562 static int usbdevfs_remount(struct super_block *s, int *flags, char *data)
563 {
564 	struct list_head *ilist = s->u.usbdevfs_sb.ilist.next;
565 	struct inode *inode;
566 	int ret;
567 
568 	if ((ret = parse_options(s, data))) {
569 		printk(KERN_WARNING "usbdevfs: remount parameter error\n");
570 		return ret;
571 	}
572 
573 	for (; ilist != &s->u.usbdevfs_sb.ilist; ilist = ilist->next) {
574 		inode = list_entry(ilist, struct inode, u.usbdev_i.slist);
575 
576 		switch (ITYPE(inode->i_ino)) {
577 			case ISPECIAL :
578 				inode->i_uid = s->u.usbdevfs_sb.listuid;
579 				inode->i_gid = s->u.usbdevfs_sb.listgid;
580 				inode->i_mode = s->u.usbdevfs_sb.listmode | S_IFREG;
581 				break;
582 			case IBUS :
583 				inode->i_uid = s->u.usbdevfs_sb.busuid;
584 				inode->i_gid = s->u.usbdevfs_sb.busgid;
585 				inode->i_mode = s->u.usbdevfs_sb.busmode | S_IFDIR;
586 				break;
587 			case IDEVICE :
588 				inode->i_uid = s->u.usbdevfs_sb.devuid;
589 				inode->i_gid = s->u.usbdevfs_sb.devgid;
590 				inode->i_mode = s->u.usbdevfs_sb.devmode | S_IFREG;
591 				break;
592 		}
593 	}
594 
595 	return 0;
596 }
597 
598 static struct super_operations usbdevfs_sops = {
599 	read_inode:	usbdevfs_read_inode,
600 	put_super:	usbdevfs_put_super,
601 	statfs:		usbdevfs_statfs,
602 	remount_fs:	usbdevfs_remount,
603 };
604 
usbdevfs_read_super(struct super_block * s,void * data,int silent)605 struct super_block *usbdevfs_read_super(struct super_block *s, void *data, int silent)
606 {
607         struct inode *root_inode, *inode;
608 	struct list_head *blist;
609 	struct usb_bus *bus;
610 	unsigned int i;
611 
612 	if (parse_options(s, data)) {
613 		printk(KERN_WARNING "usbdevfs: mount parameter error\n");
614 		return NULL;
615 	}
616 
617 	/* fill superblock */
618         s->s_blocksize = 1024;
619         s->s_blocksize_bits = 10;
620         s->s_magic = USBDEVICE_SUPER_MAGIC;
621         s->s_op = &usbdevfs_sops;
622 	INIT_LIST_HEAD(&s->u.usbdevfs_sb.slist);
623 	INIT_LIST_HEAD(&s->u.usbdevfs_sb.ilist);
624 	root_inode = iget(s, IROOT);
625         if (!root_inode)
626                 goto out_no_root;
627         s->s_root = d_alloc_root(root_inode);
628         if (!s->s_root)
629                 goto out_no_root;
630 	lock_kernel();
631 	list_add_tail(&s->u.usbdevfs_sb.slist, &superlist);
632 	for (i = 0; i < NRSPECIAL; i++) {
633 		if (!(inode = iget(s, IROOT+1+i)))
634 			continue;
635 		inode->i_uid = s->u.usbdevfs_sb.listuid;
636 		inode->i_gid = s->u.usbdevfs_sb.listgid;
637 		inode->i_mode = s->u.usbdevfs_sb.listmode | S_IFREG;
638 		special[i].inode = inode;
639 		list_add_tail(&inode->u.usbdev_i.slist, &s->u.usbdevfs_sb.ilist);
640 		list_add_tail(&inode->u.usbdev_i.dlist, &special[i].inodes);
641 	}
642 	down (&usb_bus_list_lock);
643 	for (blist = usb_bus_list.next; blist != &usb_bus_list; blist = blist->next) {
644 		bus = list_entry(blist, struct usb_bus, bus_list);
645 		new_bus_inode(bus, s);
646 		recurse_new_dev_inode(bus->root_hub, s);
647 	}
648 	up (&usb_bus_list_lock);
649 	unlock_kernel();
650         return s;
651 
652  out_no_root:
653         printk("usbdevfs_read_super: get root inode failed\n");
654         iput(root_inode);
655         return NULL;
656 }
657 
658 /*
659  * The usbdevfs name is now deprecated (as of 2.4.19).
660  * It will be removed when the 2.7.x development cycle is started.
661  * You have been warned :)
662  */
663 static DECLARE_FSTYPE(usbdevice_fs_type, "usbdevfs", usbdevfs_read_super, FS_SINGLE);
664 static DECLARE_FSTYPE(usbfs_type, "usbfs", usbdevfs_read_super, FS_SINGLE);
665 
666 /* --------------------------------------------------------------------- */
667 
update_special_inodes(void)668 static void update_special_inodes (void)
669 {
670 	int i;
671 	for (i = 0; i < NRSPECIAL; i++) {
672 		struct inode *inode = special[i].inode;
673 		if (inode)
674 			inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
675 	}
676 }
677 
678 
usbdevfs_add_bus(struct usb_bus * bus)679 void usbdevfs_add_bus(struct usb_bus *bus)
680 {
681 	struct list_head *slist;
682 
683 	lock_kernel();
684 	for (slist = superlist.next; slist != &superlist; slist = slist->next)
685 		new_bus_inode(bus, list_entry(slist, struct super_block, u.usbdevfs_sb.slist));
686 	update_special_inodes();
687 	unlock_kernel();
688 	usbdevfs_conn_disc_event();
689 }
690 
usbdevfs_remove_bus(struct usb_bus * bus)691 void usbdevfs_remove_bus(struct usb_bus *bus)
692 {
693 	lock_kernel();
694 	while (!list_empty(&bus->inodes))
695 		free_inode(list_entry(bus->inodes.next, struct inode, u.usbdev_i.dlist));
696 	update_special_inodes();
697 	unlock_kernel();
698 	usbdevfs_conn_disc_event();
699 }
700 
usbdevfs_add_device(struct usb_device * dev)701 void usbdevfs_add_device(struct usb_device *dev)
702 {
703 	struct list_head *slist;
704 
705 	lock_kernel();
706 	for (slist = superlist.next; slist != &superlist; slist = slist->next)
707 		new_dev_inode(dev, list_entry(slist, struct super_block, u.usbdevfs_sb.slist));
708 	update_special_inodes();
709 	unlock_kernel();
710 	usbdevfs_conn_disc_event();
711 }
712 
usbdevfs_remove_device(struct usb_device * dev)713 void usbdevfs_remove_device(struct usb_device *dev)
714 {
715 	struct dev_state *ds;
716 	struct siginfo sinfo;
717 
718 	lock_kernel();
719 	while (!list_empty(&dev->inodes))
720 		free_inode(list_entry(dev->inodes.next, struct inode, u.usbdev_i.dlist));
721 	while (!list_empty(&dev->filelist)) {
722 		ds = list_entry(dev->filelist.next, struct dev_state, list);
723 		list_del(&ds->list);
724 		INIT_LIST_HEAD(&ds->list);
725 		down_write(&ds->devsem);
726 		ds->dev = NULL;
727 		up_write(&ds->devsem);
728 		if (ds->discsignr) {
729 			sinfo.si_signo = SIGPIPE;
730 			sinfo.si_errno = EPIPE;
731 			sinfo.si_code = SI_ASYNCIO;
732 			sinfo.si_addr = ds->disccontext;
733 			send_sig_info(ds->discsignr, &sinfo, ds->disctask);
734 		}
735 	}
736 
737 	update_special_inodes();
738 	unlock_kernel();
739 	usbdevfs_conn_disc_event();
740 }
741 
742 /* --------------------------------------------------------------------- */
743 
744 #ifdef CONFIG_PROC_FS
745 static struct proc_dir_entry *usbdir = NULL;
746 #endif
747 
usbdevfs_init(void)748 int __init usbdevfs_init(void)
749 {
750 	int ret;
751 
752 	for (ret = 0; ret < NRSPECIAL; ret++) {
753 		INIT_LIST_HEAD(&special[ret].inodes);
754 	}
755 	if ((ret = usb_register(&usbdevfs_driver)))
756 		return ret;
757 	if ((ret = register_filesystem(&usbdevice_fs_type))) {
758 		usb_deregister(&usbdevfs_driver);
759 		return ret;
760 	}
761 	if ((ret = register_filesystem(&usbfs_type))) {
762 		usb_deregister(&usbdevfs_driver);
763 		unregister_filesystem(&usbdevice_fs_type);
764 		return ret;
765 	}
766 #ifdef CONFIG_PROC_FS
767 	/* create mount point for usbdevfs */
768 	usbdir = proc_mkdir("usb", proc_bus);
769 #endif
770 	return ret;
771 }
772 
usbdevfs_cleanup(void)773 void __exit usbdevfs_cleanup(void)
774 {
775 	usb_deregister(&usbdevfs_driver);
776 	unregister_filesystem(&usbdevice_fs_type);
777 	unregister_filesystem(&usbfs_type);
778 #ifdef CONFIG_PROC_FS
779         if (usbdir)
780                 remove_proc_entry("usb", proc_bus);
781 #endif
782 }
783 
784 #if 0
785 module_init(usbdevfs_init);
786 module_exit(usbdevfs_cleanup);
787 #endif
788