Beispiel #1
0
/**
 * usb_serial_generic_write - generic write function for serial USB devices
 * @tty:	Pointer to &struct tty_struct for the device
 * @port:	Pointer to the &usb_serial_port structure for the device
 * @buf:	Pointer to the data to write
 * @count:	Number of bytes to write
 *
 * Returns the number of characters actually written, which may be anything
 * from zero to @count. If an error occurs, it returns the negative errno
 * value.
 */
int usb_serial_generic_write(struct tty_struct *tty,
                             struct usb_serial_port *port, const unsigned char *buf, int count)
{
    struct usb_serial *serial = port->serial;
    int result;

    dbg("%s - port %d", __func__, port->number);

    if (count == 0) {
        dbg("%s - write request of 0 bytes", __func__);
        return 0;
    }

    /* only do something if we have a bulk out endpoint */
    if (!serial->num_bulk_out)
        return 0;

    if (serial->type->max_in_flight_urbs)
        return usb_serial_multi_urb_write(tty, port,
                                          buf, count);

    count = kfifo_put(port->write_fifo, buf, count);
    result = usb_serial_generic_write_start(port);

    if (result >= 0)
        result = count;

    return result;
}
Beispiel #2
0
int usb_serial_generic_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
{
	struct usb_serial *serial = port->serial;
	int result;
	unsigned char *data;

	dbg("%s - port %d", __func__, port->number);

	if (count == 0) {
		dbg("%s - write request of 0 bytes", __func__);
		return 0;
	}

	/* only do something if we have a bulk out endpoint */
	if (serial->num_bulk_out) {
		unsigned long flags;

		if (serial->type->max_in_flight_urbs)
			return usb_serial_multi_urb_write(tty, port,
							  buf, count);

		spin_lock_irqsave(&port->lock, flags);
		if (port->write_urb_busy) {
			spin_unlock_irqrestore(&port->lock, flags);
			dbg("%s - already writing", __func__);
			return 0;
		}
		port->write_urb_busy = 1;
		spin_unlock_irqrestore(&port->lock, flags);

		count = (count > port->bulk_out_size) ?
					port->bulk_out_size : count;

		memcpy(port->write_urb->transfer_buffer, buf, count);
		data = port->write_urb->transfer_buffer;
		usb_serial_debug_data(debug, &port->dev, __func__, count, data);

		/* set up our urb */
		usb_fill_bulk_urb(port->write_urb, serial->dev,
				   usb_sndbulkpipe(serial->dev,
					port->bulk_out_endpointAddress),
				   port->write_urb->transfer_buffer, count,
				   ((serial->type->write_bulk_callback) ?
				     serial->type->write_bulk_callback :
				     usb_serial_generic_write_bulk_callback),
				   port);

		/* send the data out the bulk port */
		port->write_urb_busy = 1;
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
			dev_err(&port->dev,
				"%s - failed submitting write urb, error %d\n",
							__func__, result);
			/* don't have to grab the lock here, as we will
			   retry if != 0 */
			port->write_urb_busy = 0;
		} else
			result = count;

		return result;
	}

	/* no bulk out, so return 0 bytes written */
	return 0;
}