^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Serial Converter Generic functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sysrq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifdef CONFIG_USB_SERIAL_GENERIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static __u16 vendor = 0x05f9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static __u16 product = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) module_param(vendor, ushort, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_PARM_DESC(vendor, "User specified USB idVendor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) module_param(product, ushort, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_PARM_DESC(product, "User specified USB idProduct");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int usb_serial_generic_probe(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct device *dev = &serial->interface->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) dev_info(dev, "The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) dev_info(dev, "Tell linux-usb@vger.kernel.org to add your device to a proper driver.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int usb_serial_generic_calc_num_ports(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct usb_serial_endpoints *epds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct device *dev = &serial->interface->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) num_ports = max(epds->num_bulk_in, epds->num_bulk_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (num_ports == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) dev_err(dev, "device has no bulk endpoints\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static struct usb_serial_driver usb_serial_generic_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .name = "generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .id_table = generic_device_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .probe = usb_serial_generic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .calc_num_ports = usb_serial_generic_calc_num_ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .throttle = usb_serial_generic_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .unthrottle = usb_serial_generic_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .resume = usb_serial_generic_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) &usb_serial_generic_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int usb_serial_generic_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #ifdef CONFIG_USB_SERIAL_GENERIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) generic_device_ids[0].idVendor = vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) generic_device_ids[0].idProduct = product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) generic_device_ids[0].match_flags =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) retval = usb_serial_register_drivers(serial_drivers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) "usbserial_generic", generic_device_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void usb_serial_generic_deregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #ifdef CONFIG_USB_SERIAL_GENERIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) usb_serial_deregister_drivers(serial_drivers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) clear_bit(USB_SERIAL_THROTTLED, &port->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (port->bulk_in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL_GPL(usb_serial_generic_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void usb_serial_generic_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (port->bulk_out_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) usb_kill_urb(port->write_urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) kfifo_reset_out(&port->write_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (port->bulk_in_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) usb_kill_urb(port->read_urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL_GPL(usb_serial_generic_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void *dest, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * usb_serial_generic_write_start - start writing buffered data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @port: usb-serial port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @mem_flags: flags to use for memory allocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Serialised using USB_SERIAL_WRITE_BUSY flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Return: Zero on success or if busy, otherwise a negative errno value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int usb_serial_generic_write_start(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int count, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) i = (int)find_first_bit(&port->write_urbs_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ARRAY_SIZE(port->write_urbs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) urb = port->write_urbs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) count = port->serial->type->prepare_write_buffer(port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) port->bulk_out_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) urb->transfer_buffer_length = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) port->tx_bytes += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) clear_bit(i, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) result = usb_submit_urb(urb, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dev_err_console(port, "%s - error submitting urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) set_bit(i, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) port->tx_bytes -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto retry; /* try sending off another urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * usb_serial_generic_write - generic write function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @tty: tty for the port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * @port: usb-serial port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * @buf: data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * @count: number of bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Return: The number of characters buffered, which may be anything from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * zero to @count, or a negative errno value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int usb_serial_generic_write(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct usb_serial_port *port, const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!port->bulk_out_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) result = usb_serial_generic_write_start(port, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) EXPORT_SYMBOL_GPL(usb_serial_generic_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int usb_serial_generic_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int room;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!port->bulk_out_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) room = kfifo_avail(&port->write_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return room;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int chars;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!port->bulk_out_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return chars;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) unsigned int bps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) unsigned long period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) bps = tty_get_baud_rate(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!bps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) bps = 9600; /* B0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * Use a poll-period of roughly the time it takes to send one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * character or at least one jiffy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) period = max_t(unsigned long, (10 * HZ / bps), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) period = min_t(unsigned long, period, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) __func__, jiffies_to_msecs(timeout),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) jiffies_to_msecs(period));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) expire = jiffies + timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) while (!port->serial->type->tx_empty(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) schedule_timeout_interruptible(period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (timeout && time_after(jiffies, expire))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int index, gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!test_and_clear_bit(index, &port->read_urbs_free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) res = usb_submit_urb(port->read_urbs[index], mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (res != -EPERM && res != -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) "%s - usb_submit_urb failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) __func__, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) set_bit(index, &port->read_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) for (; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) usb_kill_urb(port->read_urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) void usb_serial_generic_process_read_urb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) char *ch = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (!urb->actual_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * The per character mucking around with sysrq path it too slow for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * stuff like 3G modems, so shortcircuit it in the 99.9999999% of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * cases where the USB serial is not a console anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (port->sysrq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) for (i = 0; i < urb->actual_length; i++, ch++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!usb_serial_handle_sysrq_char(port, *ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) tty_insert_flip_string(&port->port, ch, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) void usb_serial_generic_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) bool stopped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (urb == port->read_urbs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) port->serial->type->process_read_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dev_dbg(&port->dev, "%s - urb stopped: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) stopped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dev_err(&port->dev, "%s - urb stopped: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) stopped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * Make sure URB processing is done before marking as free to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * racing with unthrottle() on another CPU. Matches the barriers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * implied by the test_and_clear_bit() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * usb_serial_generic_submit_read_urb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) smp_mb__before_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) set_bit(i, &port->read_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * Make sure URB is marked as free before checking the throttled flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * to avoid racing with unthrottle() on another CPU. Matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * smp_mb__after_atomic() in unthrottle().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) smp_mb__after_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (test_bit(USB_SERIAL_THROTTLED, &port->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) void usb_serial_generic_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (port->write_urbs[i] == urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) port->tx_bytes -= urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) set_bit(i, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dev_dbg(&port->dev, "%s - urb stopped: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev_err_console(port, "%s - urb stopped: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dev_err_console(port, "%s - nonzero urb status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) usb_serial_generic_write_start(port, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) usb_serial_port_softint(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) void usb_serial_generic_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) set_bit(USB_SERIAL_THROTTLED, &port->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) void usb_serial_generic_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) clear_bit(USB_SERIAL_THROTTLED, &port->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * Matches the smp_mb__after_atomic() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * usb_serial_generic_read_bulk_callback().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) smp_mb__after_atomic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) unsigned long arg, struct async_icount *cprev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct async_icount cnow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Use tty-port initialised flag to detect all hangups including the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * one generated at USB-device disconnect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (!tty_port_initialized(&port->port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) cnow = port->icount; /* atomic copy*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) *cprev = cnow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) struct async_icount cnow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) cnow = port->icount; /* atomic copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) ret = wait_event_interruptible(port->port.delta_msr_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) usb_serial_generic_msr_changed(tty, arg, &cnow));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (!ret && !tty_port_initialized(&port->port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int usb_serial_generic_get_icount(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct serial_icounter_struct *icount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct async_icount cnow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) spin_lock_irqsave(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) cnow = port->icount; /* atomic copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) spin_unlock_irqrestore(&port->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) icount->cts = cnow.cts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) icount->dsr = cnow.dsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) icount->rng = cnow.rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) icount->dcd = cnow.dcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) icount->tx = cnow.tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) icount->rx = cnow.rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) icount->frame = cnow.frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) icount->parity = cnow.parity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) icount->overrun = cnow.overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) icount->brk = cnow.brk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) icount->buf_overrun = cnow.buf_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) #if defined(CONFIG_USB_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (port->sysrq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (ch && time_before(jiffies, port->sysrq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) handle_sysrq(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) port->sysrq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) port->sysrq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) int usb_serial_handle_break(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (!port->port.console)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (!port->sysrq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) port->sysrq = jiffies + HZ*5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) port->sysrq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) EXPORT_SYMBOL_GPL(usb_serial_handle_break);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * usb_serial_handle_dcd_change - handle a change of carrier detect state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * @port: usb-serial port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * @tty: tty for the port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * @status: new carrier detect status, nonzero if active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) void usb_serial_handle_dcd_change(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct tty_struct *tty, unsigned int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) dev_dbg(&port->dev, "%s - status %d\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct tty_ldisc *ld = tty_ldisc_ref(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (ld) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (ld->ops->dcd_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ld->ops->dcd_change(tty, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) tty_ldisc_deref(ld);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) wake_up_interruptible(&port->port.open_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) else if (tty && !C_CLOCAL(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) tty_hangup(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) int usb_serial_generic_resume(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) int i, c = 0, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) for (i = 0; i < serial->num_ports; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) port = serial->port[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (!tty_port_initialized(&port->port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (port->bulk_in_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) r = usb_serial_generic_submit_read_urbs(port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (port->bulk_out_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) r = usb_serial_generic_write_start(port, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return c ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) EXPORT_SYMBOL_GPL(usb_serial_generic_resume);