inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*****************************************************************************/
  2. /*
  3. * inode.c -- Inode/Dentry functions for the USB device file system.
  4. *
  5. * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * History:
  23. * 0.1 04.01.2000 Created
  24. * 0.2 10.12.2001 converted to use the vfs layer better
  25. */
  26. /*****************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/fs.h>
  29. #include <linux/mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/init.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/usb.h>
  34. #include <linux/namei.h>
  35. #include <linux/usbdevice_fs.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/parser.h>
  38. #include <linux/notifier.h>
  39. #include <asm/byteorder.h>
  40. #include "usb.h"
  41. #include "hcd.h"
  42. static struct super_operations usbfs_ops;
  43. static const struct file_operations default_file_operations;
  44. static struct vfsmount *usbfs_mount;
  45. static int usbfs_mount_count; /* = 0 */
  46. static int ignore_mount = 0;
  47. static struct dentry *devices_usbfs_dentry;
  48. static int num_buses; /* = 0 */
  49. static uid_t devuid; /* = 0 */
  50. static uid_t busuid; /* = 0 */
  51. static uid_t listuid; /* = 0 */
  52. static gid_t devgid; /* = 0 */
  53. static gid_t busgid; /* = 0 */
  54. static gid_t listgid; /* = 0 */
  55. static umode_t devmode = S_IWUSR | S_IRUGO;
  56. static umode_t busmode = S_IXUGO | S_IRUGO;
  57. static umode_t listmode = S_IRUGO;
  58. enum {
  59. Opt_devuid, Opt_devgid, Opt_devmode,
  60. Opt_busuid, Opt_busgid, Opt_busmode,
  61. Opt_listuid, Opt_listgid, Opt_listmode,
  62. Opt_err,
  63. };
  64. static match_table_t tokens = {
  65. {Opt_devuid, "devuid=%u"},
  66. {Opt_devgid, "devgid=%u"},
  67. {Opt_devmode, "devmode=%o"},
  68. {Opt_busuid, "busuid=%u"},
  69. {Opt_busgid, "busgid=%u"},
  70. {Opt_busmode, "busmode=%o"},
  71. {Opt_listuid, "listuid=%u"},
  72. {Opt_listgid, "listgid=%u"},
  73. {Opt_listmode, "listmode=%o"},
  74. {Opt_err, NULL}
  75. };
  76. static int parse_options(struct super_block *s, char *data)
  77. {
  78. char *p;
  79. int option;
  80. /* (re)set to defaults. */
  81. devuid = 0;
  82. busuid = 0;
  83. listuid = 0;
  84. devgid = 0;
  85. busgid = 0;
  86. listgid = 0;
  87. devmode = S_IWUSR | S_IRUGO;
  88. busmode = S_IXUGO | S_IRUGO;
  89. listmode = S_IRUGO;
  90. while ((p = strsep(&data, ",")) != NULL) {
  91. substring_t args[MAX_OPT_ARGS];
  92. int token;
  93. if (!*p)
  94. continue;
  95. token = match_token(p, tokens, args);
  96. switch (token) {
  97. case Opt_devuid:
  98. if (match_int(&args[0], &option))
  99. return -EINVAL;
  100. devuid = option;
  101. break;
  102. case Opt_devgid:
  103. if (match_int(&args[0], &option))
  104. return -EINVAL;
  105. devgid = option;
  106. break;
  107. case Opt_devmode:
  108. if (match_octal(&args[0], &option))
  109. return -EINVAL;
  110. devmode = option & S_IRWXUGO;
  111. break;
  112. case Opt_busuid:
  113. if (match_int(&args[0], &option))
  114. return -EINVAL;
  115. busuid = option;
  116. break;
  117. case Opt_busgid:
  118. if (match_int(&args[0], &option))
  119. return -EINVAL;
  120. busgid = option;
  121. break;
  122. case Opt_busmode:
  123. if (match_octal(&args[0], &option))
  124. return -EINVAL;
  125. busmode = option & S_IRWXUGO;
  126. break;
  127. case Opt_listuid:
  128. if (match_int(&args[0], &option))
  129. return -EINVAL;
  130. listuid = option;
  131. break;
  132. case Opt_listgid:
  133. if (match_int(&args[0], &option))
  134. return -EINVAL;
  135. listgid = option;
  136. break;
  137. case Opt_listmode:
  138. if (match_octal(&args[0], &option))
  139. return -EINVAL;
  140. listmode = option & S_IRWXUGO;
  141. break;
  142. default:
  143. err("usbfs: unrecognised mount option \"%s\" "
  144. "or missing value\n", p);
  145. return -EINVAL;
  146. }
  147. }
  148. return 0;
  149. }
  150. static void update_special(struct dentry *special)
  151. {
  152. special->d_inode->i_uid = listuid;
  153. special->d_inode->i_gid = listgid;
  154. special->d_inode->i_mode = S_IFREG | listmode;
  155. }
  156. static void update_dev(struct dentry *dev)
  157. {
  158. dev->d_inode->i_uid = devuid;
  159. dev->d_inode->i_gid = devgid;
  160. dev->d_inode->i_mode = S_IFREG | devmode;
  161. }
  162. static void update_bus(struct dentry *bus)
  163. {
  164. struct dentry *dev = NULL;
  165. bus->d_inode->i_uid = busuid;
  166. bus->d_inode->i_gid = busgid;
  167. bus->d_inode->i_mode = S_IFDIR | busmode;
  168. mutex_lock(&bus->d_inode->i_mutex);
  169. list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
  170. if (dev->d_inode)
  171. update_dev(dev);
  172. mutex_unlock(&bus->d_inode->i_mutex);
  173. }
  174. static void update_sb(struct super_block *sb)
  175. {
  176. struct dentry *root = sb->s_root;
  177. struct dentry *bus = NULL;
  178. if (!root)
  179. return;
  180. mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
  181. list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
  182. if (bus->d_inode) {
  183. switch (S_IFMT & bus->d_inode->i_mode) {
  184. case S_IFDIR:
  185. update_bus(bus);
  186. break;
  187. case S_IFREG:
  188. update_special(bus);
  189. break;
  190. default:
  191. warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode);
  192. break;
  193. }
  194. }
  195. }
  196. mutex_unlock(&root->d_inode->i_mutex);
  197. }
  198. static int remount(struct super_block *sb, int *flags, char *data)
  199. {
  200. /* If this is not a real mount,
  201. * i.e. it's a simple_pin_fs from create_special_files,
  202. * then ignore it.
  203. */
  204. if (ignore_mount)
  205. return 0;
  206. if (parse_options(sb, data)) {
  207. warn("usbfs: mount parameter error:");
  208. return -EINVAL;
  209. }
  210. if (usbfs_mount && usbfs_mount->mnt_sb)
  211. update_sb(usbfs_mount->mnt_sb);
  212. return 0;
  213. }
  214. static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
  215. {
  216. struct inode *inode = new_inode(sb);
  217. if (inode) {
  218. inode->i_mode = mode;
  219. inode->i_uid = current->fsuid;
  220. inode->i_gid = current->fsgid;
  221. inode->i_blocks = 0;
  222. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  223. switch (mode & S_IFMT) {
  224. default:
  225. init_special_inode(inode, mode, dev);
  226. break;
  227. case S_IFREG:
  228. inode->i_fop = &default_file_operations;
  229. break;
  230. case S_IFDIR:
  231. inode->i_op = &simple_dir_inode_operations;
  232. inode->i_fop = &simple_dir_operations;
  233. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  234. inode->i_nlink++;
  235. break;
  236. }
  237. }
  238. return inode;
  239. }
  240. /* SMP-safe */
  241. static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
  242. dev_t dev)
  243. {
  244. struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
  245. int error = -EPERM;
  246. if (dentry->d_inode)
  247. return -EEXIST;
  248. if (inode) {
  249. d_instantiate(dentry, inode);
  250. dget(dentry);
  251. error = 0;
  252. }
  253. return error;
  254. }
  255. static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
  256. {
  257. int res;
  258. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  259. res = usbfs_mknod (dir, dentry, mode, 0);
  260. if (!res)
  261. dir->i_nlink++;
  262. return res;
  263. }
  264. static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
  265. {
  266. mode = (mode & S_IALLUGO) | S_IFREG;
  267. return usbfs_mknod (dir, dentry, mode, 0);
  268. }
  269. static inline int usbfs_positive (struct dentry *dentry)
  270. {
  271. return dentry->d_inode && !d_unhashed(dentry);
  272. }
  273. static int usbfs_empty (struct dentry *dentry)
  274. {
  275. struct list_head *list;
  276. spin_lock(&dcache_lock);
  277. list_for_each(list, &dentry->d_subdirs) {
  278. struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
  279. if (usbfs_positive(de)) {
  280. spin_unlock(&dcache_lock);
  281. return 0;
  282. }
  283. }
  284. spin_unlock(&dcache_lock);
  285. return 1;
  286. }
  287. static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
  288. {
  289. struct inode *inode = dentry->d_inode;
  290. mutex_lock(&inode->i_mutex);
  291. dentry->d_inode->i_nlink--;
  292. dput(dentry);
  293. mutex_unlock(&inode->i_mutex);
  294. d_delete(dentry);
  295. return 0;
  296. }
  297. static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
  298. {
  299. int error = -ENOTEMPTY;
  300. struct inode * inode = dentry->d_inode;
  301. mutex_lock(&inode->i_mutex);
  302. dentry_unhash(dentry);
  303. if (usbfs_empty(dentry)) {
  304. dentry->d_inode->i_nlink -= 2;
  305. dput(dentry);
  306. inode->i_flags |= S_DEAD;
  307. dir->i_nlink--;
  308. error = 0;
  309. }
  310. mutex_unlock(&inode->i_mutex);
  311. if (!error)
  312. d_delete(dentry);
  313. dput(dentry);
  314. return error;
  315. }
  316. /* default file operations */
  317. static ssize_t default_read_file (struct file *file, char __user *buf,
  318. size_t count, loff_t *ppos)
  319. {
  320. return 0;
  321. }
  322. static ssize_t default_write_file (struct file *file, const char __user *buf,
  323. size_t count, loff_t *ppos)
  324. {
  325. return count;
  326. }
  327. static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
  328. {
  329. loff_t retval = -EINVAL;
  330. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  331. switch(orig) {
  332. case 0:
  333. if (offset > 0) {
  334. file->f_pos = offset;
  335. retval = file->f_pos;
  336. }
  337. break;
  338. case 1:
  339. if ((offset + file->f_pos) > 0) {
  340. file->f_pos += offset;
  341. retval = file->f_pos;
  342. }
  343. break;
  344. default:
  345. break;
  346. }
  347. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  348. return retval;
  349. }
  350. static int default_open (struct inode *inode, struct file *file)
  351. {
  352. if (inode->i_private)
  353. file->private_data = inode->i_private;
  354. return 0;
  355. }
  356. static const struct file_operations default_file_operations = {
  357. .read = default_read_file,
  358. .write = default_write_file,
  359. .open = default_open,
  360. .llseek = default_file_lseek,
  361. };
  362. static struct super_operations usbfs_ops = {
  363. .statfs = simple_statfs,
  364. .drop_inode = generic_delete_inode,
  365. .remount_fs = remount,
  366. };
  367. static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
  368. {
  369. struct inode *inode;
  370. struct dentry *root;
  371. sb->s_blocksize = PAGE_CACHE_SIZE;
  372. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  373. sb->s_magic = USBDEVICE_SUPER_MAGIC;
  374. sb->s_op = &usbfs_ops;
  375. sb->s_time_gran = 1;
  376. inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
  377. if (!inode) {
  378. dbg("%s: could not get inode!",__FUNCTION__);
  379. return -ENOMEM;
  380. }
  381. root = d_alloc_root(inode);
  382. if (!root) {
  383. dbg("%s: could not get root dentry!",__FUNCTION__);
  384. iput(inode);
  385. return -ENOMEM;
  386. }
  387. sb->s_root = root;
  388. return 0;
  389. }
  390. /*
  391. * fs_create_by_name - create a file, given a name
  392. * @name: name of file
  393. * @mode: type of file
  394. * @parent: dentry of directory to create it in
  395. * @dentry: resulting dentry of file
  396. *
  397. * This function handles both regular files and directories.
  398. */
  399. static int fs_create_by_name (const char *name, mode_t mode,
  400. struct dentry *parent, struct dentry **dentry)
  401. {
  402. int error = 0;
  403. /* If the parent is not specified, we create it in the root.
  404. * We need the root dentry to do this, which is in the super
  405. * block. A pointer to that is in the struct vfsmount that we
  406. * have around.
  407. */
  408. if (!parent ) {
  409. if (usbfs_mount && usbfs_mount->mnt_sb) {
  410. parent = usbfs_mount->mnt_sb->s_root;
  411. }
  412. }
  413. if (!parent) {
  414. dbg("Ah! can not find a parent!");
  415. return -EFAULT;
  416. }
  417. *dentry = NULL;
  418. mutex_lock(&parent->d_inode->i_mutex);
  419. *dentry = lookup_one_len(name, parent, strlen(name));
  420. if (!IS_ERR(dentry)) {
  421. if ((mode & S_IFMT) == S_IFDIR)
  422. error = usbfs_mkdir (parent->d_inode, *dentry, mode);
  423. else
  424. error = usbfs_create (parent->d_inode, *dentry, mode);
  425. } else
  426. error = PTR_ERR(dentry);
  427. mutex_unlock(&parent->d_inode->i_mutex);
  428. return error;
  429. }
  430. static struct dentry *fs_create_file (const char *name, mode_t mode,
  431. struct dentry *parent, void *data,
  432. const struct file_operations *fops,
  433. uid_t uid, gid_t gid)
  434. {
  435. struct dentry *dentry;
  436. int error;
  437. dbg("creating file '%s'",name);
  438. error = fs_create_by_name (name, mode, parent, &dentry);
  439. if (error) {
  440. dentry = NULL;
  441. } else {
  442. if (dentry->d_inode) {
  443. if (data)
  444. dentry->d_inode->i_private = data;
  445. if (fops)
  446. dentry->d_inode->i_fop = fops;
  447. dentry->d_inode->i_uid = uid;
  448. dentry->d_inode->i_gid = gid;
  449. }
  450. }
  451. return dentry;
  452. }
  453. static void fs_remove_file (struct dentry *dentry)
  454. {
  455. struct dentry *parent = dentry->d_parent;
  456. if (!parent || !parent->d_inode)
  457. return;
  458. mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
  459. if (usbfs_positive(dentry)) {
  460. if (dentry->d_inode) {
  461. if (S_ISDIR(dentry->d_inode->i_mode))
  462. usbfs_rmdir(parent->d_inode, dentry);
  463. else
  464. usbfs_unlink(parent->d_inode, dentry);
  465. dput(dentry);
  466. }
  467. }
  468. mutex_unlock(&parent->d_inode->i_mutex);
  469. }
  470. /* --------------------------------------------------------------------- */
  471. static int usb_get_sb(struct file_system_type *fs_type,
  472. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  473. {
  474. return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt);
  475. }
  476. static struct file_system_type usb_fs_type = {
  477. .owner = THIS_MODULE,
  478. .name = "usbfs",
  479. .get_sb = usb_get_sb,
  480. .kill_sb = kill_litter_super,
  481. };
  482. /* --------------------------------------------------------------------- */
  483. static int create_special_files (void)
  484. {
  485. struct dentry *parent;
  486. int retval;
  487. /* the simple_pin_fs calls will call remount with no options
  488. * without this flag that would overwrite the real mount options (if any)
  489. */
  490. ignore_mount = 1;
  491. /* create the devices special file */
  492. retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
  493. if (retval) {
  494. err ("Unable to get usbfs mount");
  495. goto exit;
  496. }
  497. ignore_mount = 0;
  498. parent = usbfs_mount->mnt_sb->s_root;
  499. devices_usbfs_dentry = fs_create_file ("devices",
  500. listmode | S_IFREG, parent,
  501. NULL, &usbfs_devices_fops,
  502. listuid, listgid);
  503. if (devices_usbfs_dentry == NULL) {
  504. err ("Unable to create devices usbfs file");
  505. retval = -ENODEV;
  506. goto error_clean_mounts;
  507. }
  508. goto exit;
  509. error_clean_mounts:
  510. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  511. exit:
  512. return retval;
  513. }
  514. static void remove_special_files (void)
  515. {
  516. if (devices_usbfs_dentry)
  517. fs_remove_file (devices_usbfs_dentry);
  518. devices_usbfs_dentry = NULL;
  519. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  520. }
  521. void usbfs_update_special (void)
  522. {
  523. struct inode *inode;
  524. if (devices_usbfs_dentry) {
  525. inode = devices_usbfs_dentry->d_inode;
  526. if (inode)
  527. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  528. }
  529. }
  530. static void usbfs_add_bus(struct usb_bus *bus)
  531. {
  532. struct dentry *parent;
  533. char name[8];
  534. int retval;
  535. /* create the special files if this is the first bus added */
  536. if (num_buses == 0) {
  537. retval = create_special_files();
  538. if (retval)
  539. return;
  540. }
  541. ++num_buses;
  542. sprintf (name, "%03d", bus->busnum);
  543. parent = usbfs_mount->mnt_sb->s_root;
  544. bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
  545. bus, NULL, busuid, busgid);
  546. if (bus->usbfs_dentry == NULL) {
  547. err ("error creating usbfs bus entry");
  548. return;
  549. }
  550. }
  551. static void usbfs_remove_bus(struct usb_bus *bus)
  552. {
  553. if (bus->usbfs_dentry) {
  554. fs_remove_file (bus->usbfs_dentry);
  555. bus->usbfs_dentry = NULL;
  556. }
  557. --num_buses;
  558. if (num_buses <= 0) {
  559. remove_special_files();
  560. num_buses = 0;
  561. }
  562. }
  563. static void usbfs_add_device(struct usb_device *dev)
  564. {
  565. char name[8];
  566. int i;
  567. int i_size;
  568. sprintf (name, "%03d", dev->devnum);
  569. dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
  570. dev->bus->usbfs_dentry, dev,
  571. &usbfs_device_file_operations,
  572. devuid, devgid);
  573. if (dev->usbfs_dentry == NULL) {
  574. err ("error creating usbfs device entry");
  575. return;
  576. }
  577. /* Set the size of the device's file to be
  578. * equal to the size of the device descriptors. */
  579. i_size = sizeof (struct usb_device_descriptor);
  580. for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
  581. struct usb_config_descriptor *config =
  582. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  583. i_size += le16_to_cpu(config->wTotalLength);
  584. }
  585. if (dev->usbfs_dentry->d_inode)
  586. dev->usbfs_dentry->d_inode->i_size = i_size;
  587. }
  588. static void usbfs_remove_device(struct usb_device *dev)
  589. {
  590. struct dev_state *ds;
  591. struct siginfo sinfo;
  592. if (dev->usbfs_dentry) {
  593. fs_remove_file (dev->usbfs_dentry);
  594. dev->usbfs_dentry = NULL;
  595. }
  596. while (!list_empty(&dev->filelist)) {
  597. ds = list_entry(dev->filelist.next, struct dev_state, list);
  598. wake_up_all(&ds->wait);
  599. list_del_init(&ds->list);
  600. if (ds->discsignr) {
  601. sinfo.si_signo = ds->discsignr;
  602. sinfo.si_errno = EPIPE;
  603. sinfo.si_code = SI_ASYNCIO;
  604. sinfo.si_addr = ds->disccontext;
  605. kill_proc_info_as_uid(ds->discsignr, &sinfo, ds->disc_pid, ds->disc_uid, ds->disc_euid, ds->secid);
  606. }
  607. }
  608. }
  609. static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
  610. {
  611. switch (action) {
  612. case USB_DEVICE_ADD:
  613. usbfs_add_device(dev);
  614. break;
  615. case USB_DEVICE_REMOVE:
  616. usbfs_remove_device(dev);
  617. break;
  618. case USB_BUS_ADD:
  619. usbfs_add_bus(dev);
  620. break;
  621. case USB_BUS_REMOVE:
  622. usbfs_remove_bus(dev);
  623. }
  624. usbfs_update_special();
  625. usbfs_conn_disc_event();
  626. return NOTIFY_OK;
  627. }
  628. static struct notifier_block usbfs_nb = {
  629. .notifier_call = usbfs_notify,
  630. };
  631. /* --------------------------------------------------------------------- */
  632. static struct proc_dir_entry *usbdir = NULL;
  633. int __init usbfs_init(void)
  634. {
  635. int retval;
  636. retval = register_filesystem(&usb_fs_type);
  637. if (retval)
  638. return retval;
  639. usb_register_notify(&usbfs_nb);
  640. /* create mount point for usbfs */
  641. usbdir = proc_mkdir("usb", proc_bus);
  642. return 0;
  643. }
  644. void usbfs_cleanup(void)
  645. {
  646. usb_unregister_notify(&usbfs_nb);
  647. unregister_filesystem(&usb_fs_type);
  648. if (usbdir)
  649. remove_proc_entry("usb", proc_bus);
  650. }