Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * USB Serial Converter Generic functions
0004  *
0005  * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
0006  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/errno.h>
0012 #include <linux/slab.h>
0013 #include <linux/sysrq.h>
0014 #include <linux/tty.h>
0015 #include <linux/tty_flip.h>
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/usb.h>
0019 #include <linux/usb/serial.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/kfifo.h>
0022 #include <linux/serial.h>
0023 
0024 #ifdef CONFIG_USB_SERIAL_GENERIC
0025 
0026 static __u16 vendor  = 0x05f9;
0027 static __u16 product = 0xffff;
0028 
0029 module_param(vendor, ushort, 0);
0030 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
0031 
0032 module_param(product, ushort, 0);
0033 MODULE_PARM_DESC(product, "User specified USB idProduct");
0034 
0035 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
0036 
0037 static int usb_serial_generic_probe(struct usb_serial *serial,
0038                     const struct usb_device_id *id)
0039 {
0040     struct device *dev = &serial->interface->dev;
0041 
0042     dev_info(dev, "The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n");
0043     dev_info(dev, "Tell linux-usb@vger.kernel.org to add your device to a proper driver.\n");
0044 
0045     return 0;
0046 }
0047 
0048 static int usb_serial_generic_calc_num_ports(struct usb_serial *serial,
0049                     struct usb_serial_endpoints *epds)
0050 {
0051     struct device *dev = &serial->interface->dev;
0052     int num_ports;
0053 
0054     num_ports = max(epds->num_bulk_in, epds->num_bulk_out);
0055 
0056     if (num_ports == 0) {
0057         dev_err(dev, "device has no bulk endpoints\n");
0058         return -ENODEV;
0059     }
0060 
0061     return num_ports;
0062 }
0063 
0064 static struct usb_serial_driver usb_serial_generic_device = {
0065     .driver = {
0066         .owner =    THIS_MODULE,
0067         .name =     "generic",
0068     },
0069     .id_table =     generic_device_ids,
0070     .probe =        usb_serial_generic_probe,
0071     .calc_num_ports =   usb_serial_generic_calc_num_ports,
0072     .throttle =     usb_serial_generic_throttle,
0073     .unthrottle =       usb_serial_generic_unthrottle,
0074     .resume =       usb_serial_generic_resume,
0075 };
0076 
0077 static struct usb_serial_driver * const serial_drivers[] = {
0078     &usb_serial_generic_device, NULL
0079 };
0080 
0081 #endif
0082 
0083 int usb_serial_generic_register(void)
0084 {
0085     int retval = 0;
0086 
0087 #ifdef CONFIG_USB_SERIAL_GENERIC
0088     generic_device_ids[0].idVendor = vendor;
0089     generic_device_ids[0].idProduct = product;
0090     generic_device_ids[0].match_flags =
0091         USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
0092 
0093     retval = usb_serial_register_drivers(serial_drivers,
0094             "usbserial_generic", generic_device_ids);
0095 #endif
0096     return retval;
0097 }
0098 
0099 void usb_serial_generic_deregister(void)
0100 {
0101 #ifdef CONFIG_USB_SERIAL_GENERIC
0102     usb_serial_deregister_drivers(serial_drivers);
0103 #endif
0104 }
0105 
0106 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
0107 {
0108     int result = 0;
0109 
0110     clear_bit(USB_SERIAL_THROTTLED, &port->flags);
0111 
0112     if (port->bulk_in_size)
0113         result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
0114 
0115     return result;
0116 }
0117 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
0118 
0119 void usb_serial_generic_close(struct usb_serial_port *port)
0120 {
0121     unsigned long flags;
0122     int i;
0123 
0124     if (port->bulk_out_size) {
0125         for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
0126             usb_kill_urb(port->write_urbs[i]);
0127 
0128         spin_lock_irqsave(&port->lock, flags);
0129         kfifo_reset_out(&port->write_fifo);
0130         spin_unlock_irqrestore(&port->lock, flags);
0131     }
0132     if (port->bulk_in_size) {
0133         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
0134             usb_kill_urb(port->read_urbs[i]);
0135     }
0136 }
0137 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
0138 
0139 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
0140                         void *dest, size_t size)
0141 {
0142     return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
0143 }
0144 
0145 /**
0146  * usb_serial_generic_write_start - start writing buffered data
0147  * @port: usb-serial port
0148  * @mem_flags: flags to use for memory allocations
0149  *
0150  * Serialised using USB_SERIAL_WRITE_BUSY flag.
0151  *
0152  * Return: Zero on success or if busy, otherwise a negative errno value.
0153  */
0154 int usb_serial_generic_write_start(struct usb_serial_port *port,
0155                             gfp_t mem_flags)
0156 {
0157     struct urb *urb;
0158     int count, result;
0159     unsigned long flags;
0160     int i;
0161 
0162     if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
0163         return 0;
0164 retry:
0165     spin_lock_irqsave(&port->lock, flags);
0166     if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
0167         clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
0168         spin_unlock_irqrestore(&port->lock, flags);
0169         return 0;
0170     }
0171     i = (int)find_first_bit(&port->write_urbs_free,
0172                         ARRAY_SIZE(port->write_urbs));
0173     spin_unlock_irqrestore(&port->lock, flags);
0174 
0175     urb = port->write_urbs[i];
0176     count = port->serial->type->prepare_write_buffer(port,
0177                         urb->transfer_buffer,
0178                         port->bulk_out_size);
0179     urb->transfer_buffer_length = count;
0180     usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
0181     spin_lock_irqsave(&port->lock, flags);
0182     port->tx_bytes += count;
0183     spin_unlock_irqrestore(&port->lock, flags);
0184 
0185     clear_bit(i, &port->write_urbs_free);
0186     result = usb_submit_urb(urb, mem_flags);
0187     if (result) {
0188         dev_err_console(port, "%s - error submitting urb: %d\n",
0189                         __func__, result);
0190         set_bit(i, &port->write_urbs_free);
0191         spin_lock_irqsave(&port->lock, flags);
0192         port->tx_bytes -= count;
0193         spin_unlock_irqrestore(&port->lock, flags);
0194 
0195         clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
0196         return result;
0197     }
0198 
0199     goto retry; /* try sending off another urb */
0200 }
0201 EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
0202 
0203 /**
0204  * usb_serial_generic_write - generic write function
0205  * @tty: tty for the port
0206  * @port: usb-serial port
0207  * @buf: data to write
0208  * @count: number of bytes to write
0209  *
0210  * Return: The number of characters buffered, which may be anything from
0211  * zero to @count, or a negative errno value.
0212  */
0213 int usb_serial_generic_write(struct tty_struct *tty,
0214     struct usb_serial_port *port, const unsigned char *buf, int count)
0215 {
0216     int result;
0217 
0218     if (!port->bulk_out_size)
0219         return -ENODEV;
0220 
0221     if (!count)
0222         return 0;
0223 
0224     count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
0225     result = usb_serial_generic_write_start(port, GFP_ATOMIC);
0226     if (result)
0227         return result;
0228 
0229     return count;
0230 }
0231 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
0232 
0233 unsigned int usb_serial_generic_write_room(struct tty_struct *tty)
0234 {
0235     struct usb_serial_port *port = tty->driver_data;
0236     unsigned long flags;
0237     unsigned int room;
0238 
0239     if (!port->bulk_out_size)
0240         return 0;
0241 
0242     spin_lock_irqsave(&port->lock, flags);
0243     room = kfifo_avail(&port->write_fifo);
0244     spin_unlock_irqrestore(&port->lock, flags);
0245 
0246     dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
0247     return room;
0248 }
0249 
0250 unsigned int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
0251 {
0252     struct usb_serial_port *port = tty->driver_data;
0253     unsigned long flags;
0254     unsigned int chars;
0255 
0256     if (!port->bulk_out_size)
0257         return 0;
0258 
0259     spin_lock_irqsave(&port->lock, flags);
0260     chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
0261     spin_unlock_irqrestore(&port->lock, flags);
0262 
0263     dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
0264     return chars;
0265 }
0266 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
0267 
0268 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
0269 {
0270     struct usb_serial_port *port = tty->driver_data;
0271     unsigned int bps;
0272     unsigned long period;
0273     unsigned long expire;
0274 
0275     bps = tty_get_baud_rate(tty);
0276     if (!bps)
0277         bps = 9600; /* B0 */
0278     /*
0279      * Use a poll-period of roughly the time it takes to send one
0280      * character or at least one jiffy.
0281      */
0282     period = max_t(unsigned long, (10 * HZ / bps), 1);
0283     if (timeout)
0284         period = min_t(unsigned long, period, timeout);
0285 
0286     dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n",
0287                     __func__, jiffies_to_msecs(timeout),
0288                     jiffies_to_msecs(period));
0289     expire = jiffies + timeout;
0290     while (!port->serial->type->tx_empty(port)) {
0291         schedule_timeout_interruptible(period);
0292         if (signal_pending(current))
0293             break;
0294         if (timeout && time_after(jiffies, expire))
0295             break;
0296     }
0297 }
0298 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
0299 
0300 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
0301                         int index, gfp_t mem_flags)
0302 {
0303     int res;
0304 
0305     if (!test_and_clear_bit(index, &port->read_urbs_free))
0306         return 0;
0307 
0308     dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
0309 
0310     res = usb_submit_urb(port->read_urbs[index], mem_flags);
0311     if (res) {
0312         if (res != -EPERM && res != -ENODEV) {
0313             dev_err(&port->dev,
0314                     "%s - usb_submit_urb failed: %d\n",
0315                     __func__, res);
0316         }
0317         set_bit(index, &port->read_urbs_free);
0318         return res;
0319     }
0320 
0321     return 0;
0322 }
0323 
0324 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
0325                     gfp_t mem_flags)
0326 {
0327     int res;
0328     int i;
0329 
0330     for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
0331         res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
0332         if (res)
0333             goto err;
0334     }
0335 
0336     return 0;
0337 err:
0338     for (; i >= 0; --i)
0339         usb_kill_urb(port->read_urbs[i]);
0340 
0341     return res;
0342 }
0343 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
0344 
0345 void usb_serial_generic_process_read_urb(struct urb *urb)
0346 {
0347     struct usb_serial_port *port = urb->context;
0348     char *ch = urb->transfer_buffer;
0349     int i;
0350 
0351     if (!urb->actual_length)
0352         return;
0353     /*
0354      * The per character mucking around with sysrq path it too slow for
0355      * stuff like 3G modems, so shortcircuit it in the 99.9999999% of
0356      * cases where the USB serial is not a console anyway.
0357      */
0358     if (port->sysrq) {
0359         for (i = 0; i < urb->actual_length; i++, ch++) {
0360             if (!usb_serial_handle_sysrq_char(port, *ch))
0361                 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
0362         }
0363     } else {
0364         tty_insert_flip_string(&port->port, ch, urb->actual_length);
0365     }
0366     tty_flip_buffer_push(&port->port);
0367 }
0368 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
0369 
0370 void usb_serial_generic_read_bulk_callback(struct urb *urb)
0371 {
0372     struct usb_serial_port *port = urb->context;
0373     unsigned char *data = urb->transfer_buffer;
0374     bool stopped = false;
0375     int status = urb->status;
0376     int i;
0377 
0378     for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
0379         if (urb == port->read_urbs[i])
0380             break;
0381     }
0382 
0383     dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
0384                             urb->actual_length);
0385     switch (status) {
0386     case 0:
0387         usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
0388                             data);
0389         port->serial->type->process_read_urb(urb);
0390         break;
0391     case -ENOENT:
0392     case -ECONNRESET:
0393     case -ESHUTDOWN:
0394         dev_dbg(&port->dev, "%s - urb stopped: %d\n",
0395                             __func__, status);
0396         stopped = true;
0397         break;
0398     case -EPIPE:
0399         dev_err(&port->dev, "%s - urb stopped: %d\n",
0400                             __func__, status);
0401         stopped = true;
0402         break;
0403     default:
0404         dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
0405                             __func__, status);
0406         break;
0407     }
0408 
0409     /*
0410      * Make sure URB processing is done before marking as free to avoid
0411      * racing with unthrottle() on another CPU. Matches the barriers
0412      * implied by the test_and_clear_bit() in
0413      * usb_serial_generic_submit_read_urb().
0414      */
0415     smp_mb__before_atomic();
0416     set_bit(i, &port->read_urbs_free);
0417     /*
0418      * Make sure URB is marked as free before checking the throttled flag
0419      * to avoid racing with unthrottle() on another CPU. Matches the
0420      * smp_mb__after_atomic() in unthrottle().
0421      */
0422     smp_mb__after_atomic();
0423 
0424     if (stopped)
0425         return;
0426 
0427     if (test_bit(USB_SERIAL_THROTTLED, &port->flags))
0428         return;
0429 
0430     usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
0431 }
0432 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
0433 
0434 void usb_serial_generic_write_bulk_callback(struct urb *urb)
0435 {
0436     unsigned long flags;
0437     struct usb_serial_port *port = urb->context;
0438     int status = urb->status;
0439     int i;
0440 
0441     for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
0442         if (port->write_urbs[i] == urb)
0443             break;
0444     }
0445     spin_lock_irqsave(&port->lock, flags);
0446     port->tx_bytes -= urb->transfer_buffer_length;
0447     set_bit(i, &port->write_urbs_free);
0448     spin_unlock_irqrestore(&port->lock, flags);
0449 
0450     switch (status) {
0451     case 0:
0452         break;
0453     case -ENOENT:
0454     case -ECONNRESET:
0455     case -ESHUTDOWN:
0456         dev_dbg(&port->dev, "%s - urb stopped: %d\n",
0457                             __func__, status);
0458         return;
0459     case -EPIPE:
0460         dev_err_console(port, "%s - urb stopped: %d\n",
0461                             __func__, status);
0462         return;
0463     default:
0464         dev_err_console(port, "%s - nonzero urb status: %d\n",
0465                             __func__, status);
0466         break;
0467     }
0468 
0469     usb_serial_generic_write_start(port, GFP_ATOMIC);
0470     usb_serial_port_softint(port);
0471 }
0472 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
0473 
0474 void usb_serial_generic_throttle(struct tty_struct *tty)
0475 {
0476     struct usb_serial_port *port = tty->driver_data;
0477 
0478     set_bit(USB_SERIAL_THROTTLED, &port->flags);
0479 }
0480 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
0481 
0482 void usb_serial_generic_unthrottle(struct tty_struct *tty)
0483 {
0484     struct usb_serial_port *port = tty->driver_data;
0485 
0486     clear_bit(USB_SERIAL_THROTTLED, &port->flags);
0487 
0488     /*
0489      * Matches the smp_mb__after_atomic() in
0490      * usb_serial_generic_read_bulk_callback().
0491      */
0492     smp_mb__after_atomic();
0493 
0494     usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
0495 }
0496 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
0497 
0498 static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
0499                 unsigned long arg, struct async_icount *cprev)
0500 {
0501     struct usb_serial_port *port = tty->driver_data;
0502     struct async_icount cnow;
0503     unsigned long flags;
0504     bool ret;
0505 
0506     /*
0507      * Use tty-port initialised flag to detect all hangups including the
0508      * one generated at USB-device disconnect.
0509      */
0510     if (!tty_port_initialized(&port->port))
0511         return true;
0512 
0513     spin_lock_irqsave(&port->lock, flags);
0514     cnow = port->icount;                /* atomic copy*/
0515     spin_unlock_irqrestore(&port->lock, flags);
0516 
0517     ret =   ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
0518         ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
0519         ((arg & TIOCM_CD)  && (cnow.dcd != cprev->dcd)) ||
0520         ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
0521 
0522     *cprev = cnow;
0523 
0524     return ret;
0525 }
0526 
0527 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
0528 {
0529     struct usb_serial_port *port = tty->driver_data;
0530     struct async_icount cnow;
0531     unsigned long flags;
0532     int ret;
0533 
0534     spin_lock_irqsave(&port->lock, flags);
0535     cnow = port->icount;                /* atomic copy */
0536     spin_unlock_irqrestore(&port->lock, flags);
0537 
0538     ret = wait_event_interruptible(port->port.delta_msr_wait,
0539             usb_serial_generic_msr_changed(tty, arg, &cnow));
0540     if (!ret && !tty_port_initialized(&port->port))
0541         ret = -EIO;
0542 
0543     return ret;
0544 }
0545 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
0546 
0547 int usb_serial_generic_get_icount(struct tty_struct *tty,
0548                     struct serial_icounter_struct *icount)
0549 {
0550     struct usb_serial_port *port = tty->driver_data;
0551     struct async_icount cnow;
0552     unsigned long flags;
0553 
0554     spin_lock_irqsave(&port->lock, flags);
0555     cnow = port->icount;                /* atomic copy */
0556     spin_unlock_irqrestore(&port->lock, flags);
0557 
0558     icount->cts = cnow.cts;
0559     icount->dsr = cnow.dsr;
0560     icount->rng = cnow.rng;
0561     icount->dcd = cnow.dcd;
0562     icount->tx = cnow.tx;
0563     icount->rx = cnow.rx;
0564     icount->frame = cnow.frame;
0565     icount->parity = cnow.parity;
0566     icount->overrun = cnow.overrun;
0567     icount->brk = cnow.brk;
0568     icount->buf_overrun = cnow.buf_overrun;
0569 
0570     return 0;
0571 }
0572 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
0573 
0574 #if defined(CONFIG_USB_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
0575 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
0576 {
0577     if (port->sysrq) {
0578         if (ch && time_before(jiffies, port->sysrq)) {
0579             handle_sysrq(ch);
0580             port->sysrq = 0;
0581             return 1;
0582         }
0583         port->sysrq = 0;
0584     }
0585     return 0;
0586 }
0587 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
0588 
0589 int usb_serial_handle_break(struct usb_serial_port *port)
0590 {
0591     if (!port->port.console)
0592         return 0;
0593 
0594     if (!port->sysrq) {
0595         port->sysrq = jiffies + HZ*5;
0596         return 1;
0597     }
0598     port->sysrq = 0;
0599     return 0;
0600 }
0601 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
0602 #endif
0603 
0604 /**
0605  * usb_serial_handle_dcd_change - handle a change of carrier detect state
0606  * @port: usb-serial port
0607  * @tty: tty for the port
0608  * @status: new carrier detect status, nonzero if active
0609  */
0610 void usb_serial_handle_dcd_change(struct usb_serial_port *port,
0611                 struct tty_struct *tty, unsigned int status)
0612 {
0613     dev_dbg(&port->dev, "%s - status %d\n", __func__, status);
0614 
0615     if (tty) {
0616         struct tty_ldisc *ld = tty_ldisc_ref(tty);
0617 
0618         if (ld) {
0619             if (ld->ops->dcd_change)
0620                 ld->ops->dcd_change(tty, status);
0621             tty_ldisc_deref(ld);
0622         }
0623     }
0624 
0625     if (status)
0626         wake_up_interruptible(&port->port.open_wait);
0627     else if (tty && !C_CLOCAL(tty))
0628         tty_hangup(tty);
0629 }
0630 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
0631 
0632 int usb_serial_generic_resume(struct usb_serial *serial)
0633 {
0634     struct usb_serial_port *port;
0635     int i, c = 0, r;
0636 
0637     for (i = 0; i < serial->num_ports; i++) {
0638         port = serial->port[i];
0639         if (!tty_port_initialized(&port->port))
0640             continue;
0641 
0642         if (port->bulk_in_size) {
0643             r = usb_serial_generic_submit_read_urbs(port,
0644                                 GFP_NOIO);
0645             if (r < 0)
0646                 c++;
0647         }
0648 
0649         if (port->bulk_out_size) {
0650             r = usb_serial_generic_write_start(port, GFP_NOIO);
0651             if (r < 0)
0652                 c++;
0653         }
0654     }
0655 
0656     return c ? -EIO : 0;
0657 }
0658 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);