11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/usb/file.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright Linus Torvalds 1999 51da177e4SLinus Torvalds * (C) Copyright Johannes Erdfelt 1999-2001 61da177e4SLinus Torvalds * (C) Copyright Andreas Gal 1999 71da177e4SLinus Torvalds * (C) Copyright Gregory P. Smith 1999 81da177e4SLinus Torvalds * (C) Copyright Deti Fliegl 1999 (new USB architecture) 91da177e4SLinus Torvalds * (C) Copyright Randy Dunlap 2000 101da177e4SLinus Torvalds * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id, 111da177e4SLinus Torvalds more docs, etc) 121da177e4SLinus Torvalds * (C) Copyright Yggdrasil Computing, Inc. 2000 131da177e4SLinus Torvalds * (usb_device_id matching changes by Adam J. Richter) 141da177e4SLinus Torvalds * (C) Copyright Greg Kroah-Hartman 2002-2003 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/spinlock.h> 201da177e4SLinus Torvalds #include <linux/errno.h> 211da177e4SLinus Torvalds #include <linux/usb.h> 221da177e4SLinus Torvalds 236d5e8254SGreg KH #include "usb.h" 246d5e8254SGreg KH 251da177e4SLinus Torvalds #define MAX_USB_MINORS 256 2699ac48f5SArjan van de Ven static const struct file_operations *usb_minors[MAX_USB_MINORS]; 271da177e4SLinus Torvalds static DEFINE_SPINLOCK(minor_lock); 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds static int usb_open(struct inode * inode, struct file * file) 301da177e4SLinus Torvalds { 311da177e4SLinus Torvalds int minor = iminor(inode); 3299ac48f5SArjan van de Ven const struct file_operations *c; 331da177e4SLinus Torvalds int err = -ENODEV; 3499ac48f5SArjan van de Ven const struct file_operations *old_fops, *new_fops = NULL; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds spin_lock (&minor_lock); 371da177e4SLinus Torvalds c = usb_minors[minor]; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds if (!c || !(new_fops = fops_get(c))) { 401da177e4SLinus Torvalds spin_unlock(&minor_lock); 411da177e4SLinus Torvalds return err; 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds spin_unlock(&minor_lock); 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds old_fops = file->f_op; 461da177e4SLinus Torvalds file->f_op = new_fops; 471da177e4SLinus Torvalds /* Curiouser and curiouser... NULL ->open() as "no device" ? */ 481da177e4SLinus Torvalds if (file->f_op->open) 491da177e4SLinus Torvalds err = file->f_op->open(inode,file); 501da177e4SLinus Torvalds if (err) { 511da177e4SLinus Torvalds fops_put(file->f_op); 521da177e4SLinus Torvalds file->f_op = fops_get(old_fops); 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds fops_put(old_fops); 551da177e4SLinus Torvalds return err; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static struct file_operations usb_fops = { 591da177e4SLinus Torvalds .owner = THIS_MODULE, 601da177e4SLinus Torvalds .open = usb_open, 611da177e4SLinus Torvalds }; 621da177e4SLinus Torvalds 6343104f1dSGreg Kroah-Hartman static struct usb_class { 6443104f1dSGreg Kroah-Hartman struct kref kref; 6543104f1dSGreg Kroah-Hartman struct class *class; 6643104f1dSGreg Kroah-Hartman } *usb_class; 6743104f1dSGreg Kroah-Hartman 6843104f1dSGreg Kroah-Hartman static int init_usb_class(void) 6943104f1dSGreg Kroah-Hartman { 7043104f1dSGreg Kroah-Hartman int result = 0; 7143104f1dSGreg Kroah-Hartman 7243104f1dSGreg Kroah-Hartman if (usb_class != NULL) { 7343104f1dSGreg Kroah-Hartman kref_get(&usb_class->kref); 7443104f1dSGreg Kroah-Hartman goto exit; 7543104f1dSGreg Kroah-Hartman } 7643104f1dSGreg Kroah-Hartman 7743104f1dSGreg Kroah-Hartman usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL); 7843104f1dSGreg Kroah-Hartman if (!usb_class) { 7943104f1dSGreg Kroah-Hartman result = -ENOMEM; 8043104f1dSGreg Kroah-Hartman goto exit; 8143104f1dSGreg Kroah-Hartman } 8243104f1dSGreg Kroah-Hartman 8343104f1dSGreg Kroah-Hartman kref_init(&usb_class->kref); 8443104f1dSGreg Kroah-Hartman usb_class->class = class_create(THIS_MODULE, "usb"); 8543104f1dSGreg Kroah-Hartman if (IS_ERR(usb_class->class)) { 8643104f1dSGreg Kroah-Hartman result = IS_ERR(usb_class->class); 8743104f1dSGreg Kroah-Hartman err("class_create failed for usb devices"); 8843104f1dSGreg Kroah-Hartman kfree(usb_class); 8943104f1dSGreg Kroah-Hartman usb_class = NULL; 9043104f1dSGreg Kroah-Hartman } 9143104f1dSGreg Kroah-Hartman 9243104f1dSGreg Kroah-Hartman exit: 9343104f1dSGreg Kroah-Hartman return result; 9443104f1dSGreg Kroah-Hartman } 9543104f1dSGreg Kroah-Hartman 9643104f1dSGreg Kroah-Hartman static void release_usb_class(struct kref *kref) 9743104f1dSGreg Kroah-Hartman { 9843104f1dSGreg Kroah-Hartman /* Ok, we cheat as we know we only have one usb_class */ 9943104f1dSGreg Kroah-Hartman class_destroy(usb_class->class); 10043104f1dSGreg Kroah-Hartman kfree(usb_class); 10143104f1dSGreg Kroah-Hartman usb_class = NULL; 10243104f1dSGreg Kroah-Hartman } 10343104f1dSGreg Kroah-Hartman 10443104f1dSGreg Kroah-Hartman static void destroy_usb_class(void) 10543104f1dSGreg Kroah-Hartman { 10643104f1dSGreg Kroah-Hartman if (usb_class) 10743104f1dSGreg Kroah-Hartman kref_put(&usb_class->kref, release_usb_class); 10843104f1dSGreg Kroah-Hartman } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds int usb_major_init(void) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds int error; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds error = register_chrdev(USB_MAJOR, "usb", &usb_fops); 11543104f1dSGreg Kroah-Hartman if (error) 1161da177e4SLinus Torvalds err("unable to get major %d for usb devices", USB_MAJOR); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds return error; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds void usb_major_cleanup(void) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds unregister_chrdev(USB_MAJOR, "usb"); 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /** 1271da177e4SLinus Torvalds * usb_register_dev - register a USB device, and ask for a minor number 1281da177e4SLinus Torvalds * @intf: pointer to the usb_interface that is being registered 1291da177e4SLinus Torvalds * @class_driver: pointer to the usb_class_driver for this device 1301da177e4SLinus Torvalds * 1311da177e4SLinus Torvalds * This should be called by all USB drivers that use the USB major number. 1321da177e4SLinus Torvalds * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be 1331da177e4SLinus Torvalds * dynamically allocated out of the list of available ones. If it is not 1341da177e4SLinus Torvalds * enabled, the minor number will be based on the next available free minor, 1351da177e4SLinus Torvalds * starting at the class_driver->minor_base. 1361da177e4SLinus Torvalds * 137d6e5bcf4SGreg Kroah-Hartman * This function also creates a usb class device in the sysfs tree. 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds * usb_deregister_dev() must be called when the driver is done with 1401da177e4SLinus Torvalds * the minor numbers given out by this function. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * Returns -EINVAL if something bad happens with trying to register a 1431da177e4SLinus Torvalds * device, and 0 on success. 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds int usb_register_dev(struct usb_interface *intf, 1461da177e4SLinus Torvalds struct usb_class_driver *class_driver) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds int retval = -EINVAL; 1491da177e4SLinus Torvalds int minor_base = class_driver->minor_base; 1501da177e4SLinus Torvalds int minor = 0; 1511da177e4SLinus Torvalds char name[BUS_ID_SIZE]; 1521da177e4SLinus Torvalds char *temp; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * We don't care what the device tries to start at, we want to start 1571da177e4SLinus Torvalds * at zero to pack the devices into the smallest available space with 1581da177e4SLinus Torvalds * no holes in the minor range. 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds minor_base = 0; 1611da177e4SLinus Torvalds #endif 1621da177e4SLinus Torvalds intf->minor = -1; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds dbg ("looking for a minor, starting at %d", minor_base); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds if (class_driver->fops == NULL) 1671da177e4SLinus Torvalds goto exit; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds spin_lock (&minor_lock); 1701da177e4SLinus Torvalds for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) { 1711da177e4SLinus Torvalds if (usb_minors[minor]) 1721da177e4SLinus Torvalds continue; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds usb_minors[minor] = class_driver->fops; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds retval = 0; 1771da177e4SLinus Torvalds break; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds spin_unlock (&minor_lock); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds if (retval) 1821da177e4SLinus Torvalds goto exit; 1831da177e4SLinus Torvalds 18443104f1dSGreg Kroah-Hartman retval = init_usb_class(); 18543104f1dSGreg Kroah-Hartman if (retval) 18643104f1dSGreg Kroah-Hartman goto exit; 18743104f1dSGreg Kroah-Hartman 1881da177e4SLinus Torvalds intf->minor = minor; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* create a usb class device for this usb interface */ 191d6e5bcf4SGreg Kroah-Hartman snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base); 1921da177e4SLinus Torvalds temp = strrchr(name, '/'); 1931da177e4SLinus Torvalds if (temp && (temp[1] != 0x00)) 1941da177e4SLinus Torvalds ++temp; 1951da177e4SLinus Torvalds else 1961da177e4SLinus Torvalds temp = name; 197*27fb4f0aSGreg Kroah-Hartman intf->class_dev = class_device_create(usb_class->class, NULL, 198*27fb4f0aSGreg Kroah-Hartman MKDEV(USB_MAJOR, minor), 199*27fb4f0aSGreg Kroah-Hartman &intf->dev, "%s", temp); 200*27fb4f0aSGreg Kroah-Hartman if (IS_ERR(intf->class_dev)) { 2011da177e4SLinus Torvalds spin_lock (&minor_lock); 2021da177e4SLinus Torvalds usb_minors[intf->minor] = NULL; 2031da177e4SLinus Torvalds spin_unlock (&minor_lock); 204*27fb4f0aSGreg Kroah-Hartman retval = PTR_ERR(intf->class_dev); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds exit: 2071da177e4SLinus Torvalds return retval; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds EXPORT_SYMBOL(usb_register_dev); 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /** 2121da177e4SLinus Torvalds * usb_deregister_dev - deregister a USB device's dynamic minor. 2131da177e4SLinus Torvalds * @intf: pointer to the usb_interface that is being deregistered 2141da177e4SLinus Torvalds * @class_driver: pointer to the usb_class_driver for this device 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * Used in conjunction with usb_register_dev(). This function is called 2171da177e4SLinus Torvalds * when the USB driver is finished with the minor numbers gotten from a 2181da177e4SLinus Torvalds * call to usb_register_dev() (usually when the device is disconnected 2191da177e4SLinus Torvalds * from the system.) 2201da177e4SLinus Torvalds * 221d6e5bcf4SGreg Kroah-Hartman * This function also removes the usb class device from the sysfs tree. 2221da177e4SLinus Torvalds * 2231da177e4SLinus Torvalds * This should be called by all drivers that use the USB major number. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds void usb_deregister_dev(struct usb_interface *intf, 2261da177e4SLinus Torvalds struct usb_class_driver *class_driver) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds int minor_base = class_driver->minor_base; 2291da177e4SLinus Torvalds char name[BUS_ID_SIZE]; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS 2321da177e4SLinus Torvalds minor_base = 0; 2331da177e4SLinus Torvalds #endif 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds if (intf->minor == -1) 2361da177e4SLinus Torvalds return; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds dbg ("removing %d minor", intf->minor); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds spin_lock (&minor_lock); 2411da177e4SLinus Torvalds usb_minors[intf->minor] = NULL; 2421da177e4SLinus Torvalds spin_unlock (&minor_lock); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base); 245*27fb4f0aSGreg Kroah-Hartman class_device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor)); 246*27fb4f0aSGreg Kroah-Hartman intf->class_dev = NULL; 2471da177e4SLinus Torvalds intf->minor = -1; 24843104f1dSGreg Kroah-Hartman destroy_usb_class(); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds EXPORT_SYMBOL(usb_deregister_dev); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds 253