1、linux/usb/serial.h#include pl2303.h * Version Information#define DRIVER_AUTHOR Greg Kroah-Hartman, greg, #define DRIVER_DESC USB Serial Driver corestatic void port_free(struct usb_serial_port *port);/* Driver structure we register with the USB core */static struct usb_driver usb_serial_driver = .nam
2、e = usbserial, .probe = usb_serial_probe, .disconnect = usb_serial_disconnect, .suspend = usb_serial_suspend, .resume = usb_serial_resume, .no_dynamic_id = 1,;/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead the MODULE_DEVICE_TABLE declarations in each serial driver cause the hotplug prog
3、ram to pull in whatever module is necessary via modprobe, and modprobe will load usbserial because the serial drivers depend on it.*/static int debug;/* initially all NULL */static struct usb_serial *serial_tableSERIAL_TTY_MINORS;static DEFINE_MUTEX(table_lock);static LIST_HEAD(usb_serial_driver_lis
4、t);struct usb_serial *usb_serial_get_by_index(unsigned index) struct usb_serial *serial; mutex_lock(&table_lock); serial = serial_tableindex; if (serial) kref_get(&serial-kref); mutex_unlock(& return serial;static struct usb_serial *get_free_serial(struct usb_serial *serial, int num_ports, unsigned
5、int *minor) unsigned int i, j; int good_spot; dbg(%s %d, _func_, num_ports); *minor = 0; for (i = 0; i SERIAL_TTY_MINORS; +i) if (serial_tablei) continue; good_spot = 1; for (j = 1; j = SERIAL_TTY_MINORS) | (serial_tablei+j) good_spot = 0; i += j; break; if (good_spot = 0) *minor = i; j = 0; dbg(%s
6、- minor base = %d, _func_, *minor); for (i = *minor; (i portj+-number = i; mutex_unlock(& return serial; return NULL;static void return_serial(struct usb_serial *serial) int i;%s, _func_); serial-num_ports; +i) serial_tableserial-minor + i = NULL;static void destroy_serial(struct kref *kref) struct
7、usb_serial_port *port; serial = to_usb_serial(kref);%s - %s, _func_, serial-type-description); /* return the minor range that this device had */ if (serial-minor != SERIAL_TTY_NO_MINOR) return_serial(serial);release(serial); port = serial-porti; if (port) put_device(&port-dev); /* If this is a fake
8、port, we have to clean it up here, as it will * not get cleaned up in port_release() as it was never registered with * the driver core */num_ports i if (port) port_free(port); usb_put_dev(serial- /* free up any memory that we allocated */ kfree(serial);void usb_serial_put(struct usb_serial *serial)
9、kref_put(&kref, destroy_serial);/* * Driver tty interface functions */static int serial_open (struct tty_struct *tty, struct file *filp) unsigned int portNumber; int retval = 0; /* get the serial object associated with this tty pointer */ serial = usb_serial_get_by_index(tty-index); if (!serial) tty
10、-driver_data = NULL; return -ENODEV;disc_mutex); portNumber = tty-index - serial-minor; port = serial-portportNumber;port | serial-disconnected) retval = -ENODEV; else get_device(& /* * Note: Our locking order requirement does not allow port-mutex * to be acquired while serial-disc_mutex is held. if
11、 (retval) goto bailout_serial_put; if (mutex_lock_interruptible(&mutex) retval = -ERESTARTSYS; goto bailout_port_put; +port-port.count; /* set up our port structure making the tty driver * remember our port object, and us it */ tty-driver_data = port; tty_port_tty_set(&port, tty); if (port-port.coun
12、t = 1) /* lock this module before we call it * this may fail, which means we must bail out, * safe because we are called with BKL held */ if (!try_module_get(serial-driver.owner) retval = -ENODEV; goto bailout_mutex_unlock; mutex_lock(& if (serial- else retval = usb_autopm_get_interface(serial-inter
13、face); if (retval) goto bailout_module_put; /* only call the device specific open if this * is the first time the port is opened */ retval = serial-open(tty, port, filp); goto bailout_interface_put;mutex); return 0;bailout_interface_put: usb_autopm_put_interface(serial-bailout_module_put: module_put
14、(serial-driver.owner);bailout_mutex_unlock: port-port.count = 0;port, NULL);bailout_port_put: put_device(&bailout_serial_put: usb_serial_put(serial); return retval;static void serial_close(struct tty_struct *tty, struct file *filp) struct usb_serial_port *port = tty-driver_data; struct module *owner
15、; int count;port) return;%s - port %d, _func_, port-number); serial = port-serial; owner = serial-driver.owner;port.count = 0) port.count = 1) /* only call the device specific close if this * port is being closed by the last owner. Ensure we do * this before we drop the port count. The call is prote
16、cted * by the port mutex */ serial-close(tty, port, filp);port.count = (port-console ? 2 : 1) struct tty_struct *tty = tty_port_tty_get(&port); if (tty) /* We must do this before we drop the port count to zero. */ if (tty-driver_data) tty- tty_port_tty_set(& tty_kref_put(tty); -port- count = port- /
17、* Mustnt dereference port any more */ if (count = 0) usb_autopm_put_interface(serial-t dereference serial any more */ if (count = 0) module_put(owner);static int serial_write(struct tty_struct *tty, const unsigned char *buf, int count) int retval = -ENODEV;dev-state = USB_STATE_NOTATTACHED) goto exi
18、t;%s - port %d, %d byte(s)number, count); /* count is managed under the mutex lock for the tty so cannot drop to zero until after the last close completes */ WARN_ON(!port.count); /* pass on to the driver specific version of this function */ retval = port-write(tty, port, buf, count);exit:static int
19、 serial_write_room(struct tty_struct *tty) return port-write_room(tty);static int serial_chars_in_buffer(struct tty_struct *tty)%s = port %d /* if the device was unplugged then any remaining characters fell out of the connector ;) */ return 0;chars_in_buffer(tty);static void serial_throttle(struct tty_struct *tty)th
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1