inode.c 17 KB

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