static void ipaq_close(struct usb_serial_port *port, struct file *filp)
{
	struct ipaq_private	*priv = usb_get_serial_port_data(port);

	dbg("%s - port %d", __FUNCTION__, port->number);
			 
	/*
	 * shut down bulk read and write
	 */
	usb_unlink_urb(port->write_urb);
	usb_unlink_urb(port->read_urb);
	ipaq_destroy_lists(port);
	kfree(priv);
	usb_set_serial_port_data(port, NULL);

	/* Uncomment the following line if you want to see some statistics in your syslog */
	/* info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out); */
}
static int ipaq_open(struct usb_serial_port *port, struct file *filp)
{
	struct usb_serial	*serial = port->serial;
	struct ipaq_private	*priv;
	struct ipaq_packet	*pkt;
	int			i, result = 0;
	int			retries = KP_RETRIES;

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

	bytes_in = 0;
	bytes_out = 0;
	priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
	if (priv == NULL) {
		err("%s - Out of memory", __FUNCTION__);
		return -ENOMEM;
	}
	usb_set_serial_port_data(port, priv);
	priv->active = 0;
	priv->queue_len = 0;
	INIT_LIST_HEAD(&priv->queue);
	INIT_LIST_HEAD(&priv->freelist);

	for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
		pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
		if (pkt == NULL) {
			goto enomem;
		}
		pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
		if (pkt->data == NULL) {
			kfree(pkt);
			goto enomem;
		}
		pkt->len = 0;
		pkt->written = 0;
		INIT_LIST_HEAD(&pkt->list);
		list_add(&pkt->list, &priv->freelist);
		priv->free_len += PACKET_SIZE;
	}

	/*
	 * Force low latency on. This will immediately push data to the line
	 * discipline instead of queueing.
	 */

	port->tty->low_latency = 1;
	port->tty->raw = 1;
	port->tty->real_raw = 1;

	/*
	 * Lose the small buffers usbserial provides. Make larger ones.
	 */

	kfree(port->bulk_in_buffer);
	kfree(port->bulk_out_buffer);
	port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
	if (port->bulk_in_buffer == NULL) {
		goto enomem;
	}
	port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
	if (port->bulk_out_buffer == NULL) {
		kfree(port->bulk_in_buffer);
		goto enomem;
	}
	port->read_urb->transfer_buffer = port->bulk_in_buffer;
	port->write_urb->transfer_buffer = port->bulk_out_buffer;
	port->read_urb->transfer_buffer_length = URBDATA_SIZE;
	port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
	
	/* Start reading from the device */
	usb_fill_bulk_urb(port->read_urb, serial->dev, 
		      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
		      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
		      ipaq_read_bulk_callback, port);
	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
	if (result) {
		err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
		goto error;
	}

	/*
	 * Send out control message observed in win98 sniffs. Not sure what
	 * it does, but from empirical observations, it seems that the device
	 * will start the chat sequence once one of these messages gets
	 * through. Since this has a reasonably high failure rate, we retry
	 * several times.
	 */

	while (retries--) {
		result = usb_control_msg(serial->dev,
				usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
				0x1, 0, NULL, 0, HZ / 10 + 1);
		if (result == 0) {
			return 0;
		}
	}
	err("%s - failed doing control urb, error %d", __FUNCTION__, result);
	goto error;

enomem:
	result = -ENOMEM;
	err("%s - Out of memory", __FUNCTION__);
error:
	ipaq_destroy_lists(port);
	kfree(priv);
	return result;
}
示例#3
0
文件: ipaq.c 项目: artm1248/linux
static int ipaq_open(struct tty_struct *tty,
                     struct usb_serial_port *port, struct file *filp)
{
    struct usb_serial	*serial = port->serial;
    struct ipaq_private	*priv;
    struct ipaq_packet	*pkt;
    int			i, result = 0;
    int			retries = connect_retries;

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

    bytes_in = 0;
    bytes_out = 0;
    priv = kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
    if (priv == NULL) {
        dev_err(&port->dev, "%s - Out of memory\n", __func__);
        return -ENOMEM;
    }
    usb_set_serial_port_data(port, priv);
    priv->active = 0;
    priv->queue_len = 0;
    priv->free_len = 0;
    INIT_LIST_HEAD(&priv->queue);
    INIT_LIST_HEAD(&priv->freelist);

    for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
        pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
        if (pkt == NULL)
            goto enomem;

        pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
        if (pkt->data == NULL) {
            kfree(pkt);
            goto enomem;
        }
        pkt->len = 0;
        pkt->written = 0;
        INIT_LIST_HEAD(&pkt->list);
        list_add(&pkt->list, &priv->freelist);
        priv->free_len += PACKET_SIZE;
    }

    if (tty) {
        /* FIXME: These two are bogus */
        tty->raw = 1;
        tty->real_raw = 1;
    }
    /*
     * Lose the small buffers usbserial provides. Make larger ones.
     */

    kfree(port->bulk_in_buffer);
    kfree(port->bulk_out_buffer);
    /* make sure the generic serial code knows */
    port->bulk_out_buffer = NULL;

    port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
    if (port->bulk_in_buffer == NULL)
        goto enomem;

    port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
    if (port->bulk_out_buffer == NULL) {
        /* the buffer is useless, free it */
        kfree(port->bulk_in_buffer);
        port->bulk_in_buffer = NULL;
        goto enomem;
    }
    port->read_urb->transfer_buffer = port->bulk_in_buffer;
    port->write_urb->transfer_buffer = port->bulk_out_buffer;
    port->read_urb->transfer_buffer_length = URBDATA_SIZE;
    port->bulk_out_size = port->write_urb->transfer_buffer_length
                          = URBDATA_SIZE;

    msleep(1000*initial_wait);

    /*
     * Send out control message observed in win98 sniffs. Not sure what
     * it does, but from empirical observations, it seems that the device
     * will start the chat sequence once one of these messages gets
     * through. Since this has a reasonably high failure rate, we retry
     * several times.
     */

    while (retries--) {
        result = usb_control_msg(serial->dev,
                                 usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
                                 0x1, 0, NULL, 0, 100);
        if (!result)
            break;

        msleep(1000);
    }

    if (!retries && result) {
        dev_err(&port->dev, "%s - failed doing control urb, error %d\n",			__func__, result);
        goto error;
    }

    /* Start reading from the device */
    usb_fill_bulk_urb(port->read_urb, serial->dev,
                      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
                      port->read_urb->transfer_buffer,
                      port->read_urb->transfer_buffer_length,
                      ipaq_read_bulk_callback, port);

    result = usb_submit_urb(port->read_urb, GFP_KERNEL);
    if (result) {
        dev_err(&port->dev,
                "%s - failed submitting read urb, error %d\n",
                __func__, result);
        goto error;
    }

    return 0;

enomem:
    result = -ENOMEM;
    dev_err(&port->dev, "%s - Out of memory\n", __func__);
error:
    ipaq_destroy_lists(port);
    kfree(priv);
    return result;
}