18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Infinity Unlimited USB Phoenix driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  And tested with help of WB Electronics
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/errno.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/tty.h>
178c2ecf20Sopenharmony_ci#include <linux/tty_driver.h>
188c2ecf20Sopenharmony_ci#include <linux/tty_flip.h>
198c2ecf20Sopenharmony_ci#include <linux/serial.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
228c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
238c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
248c2ecf20Sopenharmony_ci#include <linux/usb.h>
258c2ecf20Sopenharmony_ci#include <linux/usb/serial.h>
268c2ecf20Sopenharmony_ci#include "iuu_phoenix.h"
278c2ecf20Sopenharmony_ci#include <linux/random.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table[] = {
328c2ecf20Sopenharmony_ci	{USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
338c2ecf20Sopenharmony_ci	{}			/* Terminating entry */
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* turbo parameter */
388c2ecf20Sopenharmony_cistatic int boost = 100;
398c2ecf20Sopenharmony_cistatic int clockmode = 1;
408c2ecf20Sopenharmony_cistatic int cdmode = 1;
418c2ecf20Sopenharmony_cistatic int iuu_cardin;
428c2ecf20Sopenharmony_cistatic int iuu_cardout;
438c2ecf20Sopenharmony_cistatic bool xmas;
448c2ecf20Sopenharmony_cistatic int vcc_default = 5;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int iuu_create_sysfs_attrs(struct usb_serial_port *port);
478c2ecf20Sopenharmony_cistatic int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
488c2ecf20Sopenharmony_cistatic void read_rxcmd_callback(struct urb *urb);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistruct iuu_private {
518c2ecf20Sopenharmony_ci	spinlock_t lock;	/* store irq state */
528c2ecf20Sopenharmony_ci	u8 line_status;
538c2ecf20Sopenharmony_ci	int tiostatus;		/* store IUART SIGNAL for tiocmget call */
548c2ecf20Sopenharmony_ci	u8 reset;		/* if 1 reset is needed */
558c2ecf20Sopenharmony_ci	int poll;		/* number of poll */
568c2ecf20Sopenharmony_ci	u8 *writebuf;		/* buffer for writing to device */
578c2ecf20Sopenharmony_ci	int writelen;		/* num of byte to write to device */
588c2ecf20Sopenharmony_ci	u8 *buf;		/* used for initialize speed */
598c2ecf20Sopenharmony_ci	u8 len;
608c2ecf20Sopenharmony_ci	int vcc;		/* vcc (either 3 or 5 V) */
618c2ecf20Sopenharmony_ci	u32 boost;
628c2ecf20Sopenharmony_ci	u32 clk;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int iuu_port_probe(struct usb_serial_port *port)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct iuu_private *priv;
688c2ecf20Sopenharmony_ci	int ret;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
718c2ecf20Sopenharmony_ci	if (!priv)
728c2ecf20Sopenharmony_ci		return -ENOMEM;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	priv->buf = kzalloc(256, GFP_KERNEL);
758c2ecf20Sopenharmony_ci	if (!priv->buf) {
768c2ecf20Sopenharmony_ci		kfree(priv);
778c2ecf20Sopenharmony_ci		return -ENOMEM;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	priv->writebuf = kzalloc(256, GFP_KERNEL);
818c2ecf20Sopenharmony_ci	if (!priv->writebuf) {
828c2ecf20Sopenharmony_ci		kfree(priv->buf);
838c2ecf20Sopenharmony_ci		kfree(priv);
848c2ecf20Sopenharmony_ci		return -ENOMEM;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	priv->vcc = vcc_default;
888c2ecf20Sopenharmony_ci	spin_lock_init(&priv->lock);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	usb_set_serial_port_data(port, priv);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ret = iuu_create_sysfs_attrs(port);
938c2ecf20Sopenharmony_ci	if (ret) {
948c2ecf20Sopenharmony_ci		kfree(priv->writebuf);
958c2ecf20Sopenharmony_ci		kfree(priv->buf);
968c2ecf20Sopenharmony_ci		kfree(priv);
978c2ecf20Sopenharmony_ci		return ret;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return 0;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int iuu_port_remove(struct usb_serial_port *port)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	iuu_remove_sysfs_attrs(port);
1088c2ecf20Sopenharmony_ci	kfree(priv->writebuf);
1098c2ecf20Sopenharmony_ci	kfree(priv->buf);
1108c2ecf20Sopenharmony_ci	kfree(priv);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return 0;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int iuu_tiocmset(struct tty_struct *tty,
1168c2ecf20Sopenharmony_ci			unsigned int set, unsigned int clear)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
1198c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
1208c2ecf20Sopenharmony_ci	unsigned long flags;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* FIXME: locking on tiomstatus */
1238c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
1248c2ecf20Sopenharmony_ci		__func__, set, clear);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
1298c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
1308c2ecf20Sopenharmony_ci		priv->reset = 1;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci	if (set & TIOCM_RTS)
1338c2ecf20Sopenharmony_ci		priv->tiostatus = TIOCM_RTS;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* This is used to provide a carrier detect mechanism
1408c2ecf20Sopenharmony_ci * When a card is present, the response is 0x00
1418c2ecf20Sopenharmony_ci * When no card , the reader respond with TIOCM_CD
1428c2ecf20Sopenharmony_ci * This is known as CD autodetect mechanism
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_cistatic int iuu_tiocmget(struct tty_struct *tty)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
1478c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
1488c2ecf20Sopenharmony_ci	unsigned long flags;
1498c2ecf20Sopenharmony_ci	int rc;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
1528c2ecf20Sopenharmony_ci	rc = priv->tiostatus;
1538c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return rc;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic void iuu_rxcmd(struct urb *urb)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
1618c2ecf20Sopenharmony_ci	int status = urb->status;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (status) {
1648c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
1658c2ecf20Sopenharmony_ci		/* error stop all */
1668c2ecf20Sopenharmony_ci		return;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1718c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1728c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
1738c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
1748c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 1,
1758c2ecf20Sopenharmony_ci			  read_rxcmd_callback, port);
1768c2ecf20Sopenharmony_ci	usb_submit_urb(port->write_urb, GFP_ATOMIC);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int iuu_reset(struct usb_serial_port *port, u8 wt)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
1828c2ecf20Sopenharmony_ci	int result;
1838c2ecf20Sopenharmony_ci	char *buf_ptr = port->write_urb->transfer_buffer;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Prepare the reset sequence */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	*buf_ptr++ = IUU_RST_SET;
1888c2ecf20Sopenharmony_ci	*buf_ptr++ = IUU_DELAY_MS;
1898c2ecf20Sopenharmony_ci	*buf_ptr++ = wt;
1908c2ecf20Sopenharmony_ci	*buf_ptr = IUU_RST_CLEAR;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* send the sequence */
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb,
1958c2ecf20Sopenharmony_ci			  port->serial->dev,
1968c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
1978c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
1988c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
1998c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2008c2ecf20Sopenharmony_ci	priv->reset = 0;
2018c2ecf20Sopenharmony_ci	return result;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Status Function
2058c2ecf20Sopenharmony_ci * Return value is
2068c2ecf20Sopenharmony_ci * 0x00 = no card
2078c2ecf20Sopenharmony_ci * 0x01 = smartcard
2088c2ecf20Sopenharmony_ci * 0x02 = sim card
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_cistatic void iuu_update_status_callback(struct urb *urb)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
2138c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
2148c2ecf20Sopenharmony_ci	u8 *st;
2158c2ecf20Sopenharmony_ci	int status = urb->status;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (status) {
2188c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
2198c2ecf20Sopenharmony_ci		/* error stop all */
2208c2ecf20Sopenharmony_ci		return;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	st = urb->transfer_buffer;
2248c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - enter\n", __func__);
2258c2ecf20Sopenharmony_ci	if (urb->actual_length == 1) {
2268c2ecf20Sopenharmony_ci		switch (st[0]) {
2278c2ecf20Sopenharmony_ci		case 0x1:
2288c2ecf20Sopenharmony_ci			priv->tiostatus = iuu_cardout;
2298c2ecf20Sopenharmony_ci			break;
2308c2ecf20Sopenharmony_ci		case 0x0:
2318c2ecf20Sopenharmony_ci			priv->tiostatus = iuu_cardin;
2328c2ecf20Sopenharmony_ci			break;
2338c2ecf20Sopenharmony_ci		default:
2348c2ecf20Sopenharmony_ci			priv->tiostatus = iuu_cardin;
2358c2ecf20Sopenharmony_ci		}
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci	iuu_rxcmd(urb);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic void iuu_status_callback(struct urb *urb)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
2438c2ecf20Sopenharmony_ci	int status = urb->status;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
2468c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
2478c2ecf20Sopenharmony_ci			  usb_rcvbulkpipe(port->serial->dev,
2488c2ecf20Sopenharmony_ci					  port->bulk_in_endpointAddress),
2498c2ecf20Sopenharmony_ci			  port->read_urb->transfer_buffer, 256,
2508c2ecf20Sopenharmony_ci			  iuu_update_status_callback, port);
2518c2ecf20Sopenharmony_ci	usb_submit_urb(port->read_urb, GFP_ATOMIC);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int iuu_status(struct usb_serial_port *port)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	int result;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
2598c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2608c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
2618c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
2628c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 1,
2638c2ecf20Sopenharmony_ci			  iuu_status_callback, port);
2648c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2658c2ecf20Sopenharmony_ci	return result;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int status;
2728c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
2738c2ecf20Sopenharmony_ci	int actual = 0;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* send the data out the bulk port */
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	status =
2788c2ecf20Sopenharmony_ci	    usb_bulk_msg(serial->dev,
2798c2ecf20Sopenharmony_ci			 usb_sndbulkpipe(serial->dev,
2808c2ecf20Sopenharmony_ci					 port->bulk_out_endpointAddress), buf,
2818c2ecf20Sopenharmony_ci			 count, &actual, 1000);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
2848c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
2858c2ecf20Sopenharmony_ci	else
2868c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - write OK !\n", __func__);
2878c2ecf20Sopenharmony_ci	return status;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	int status;
2938c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
2948c2ecf20Sopenharmony_ci	int actual = 0;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* send the data out the bulk port */
2978c2ecf20Sopenharmony_ci	status =
2988c2ecf20Sopenharmony_ci	    usb_bulk_msg(serial->dev,
2998c2ecf20Sopenharmony_ci			 usb_rcvbulkpipe(serial->dev,
3008c2ecf20Sopenharmony_ci					 port->bulk_in_endpointAddress), buf,
3018c2ecf20Sopenharmony_ci			 count, &actual, 1000);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
3048c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
3058c2ecf20Sopenharmony_ci	else
3068c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - read OK !\n", __func__);
3078c2ecf20Sopenharmony_ci	return status;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int iuu_led(struct usb_serial_port *port, unsigned int R,
3118c2ecf20Sopenharmony_ci		   unsigned int G, unsigned int B, u8 f)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	int status;
3148c2ecf20Sopenharmony_ci	u8 *buf;
3158c2ecf20Sopenharmony_ci	buf = kmalloc(8, GFP_KERNEL);
3168c2ecf20Sopenharmony_ci	if (!buf)
3178c2ecf20Sopenharmony_ci		return -ENOMEM;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	buf[0] = IUU_SET_LED;
3208c2ecf20Sopenharmony_ci	buf[1] = R & 0xFF;
3218c2ecf20Sopenharmony_ci	buf[2] = (R >> 8) & 0xFF;
3228c2ecf20Sopenharmony_ci	buf[3] = G & 0xFF;
3238c2ecf20Sopenharmony_ci	buf[4] = (G >> 8) & 0xFF;
3248c2ecf20Sopenharmony_ci	buf[5] = B & 0xFF;
3258c2ecf20Sopenharmony_ci	buf[6] = (B >> 8) & 0xFF;
3268c2ecf20Sopenharmony_ci	buf[7] = f;
3278c2ecf20Sopenharmony_ci	status = bulk_immediate(port, buf, 8);
3288c2ecf20Sopenharmony_ci	kfree(buf);
3298c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
3308c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
3318c2ecf20Sopenharmony_ci	else
3328c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - led OK !\n", __func__);
3338c2ecf20Sopenharmony_ci	return IUU_OPERATION_OK;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
3378c2ecf20Sopenharmony_ci				 u8 b2, u8 freq)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	*buf++ = IUU_SET_LED;
3408c2ecf20Sopenharmony_ci	*buf++ = r1;
3418c2ecf20Sopenharmony_ci	*buf++ = r2;
3428c2ecf20Sopenharmony_ci	*buf++ = g1;
3438c2ecf20Sopenharmony_ci	*buf++ = g2;
3448c2ecf20Sopenharmony_ci	*buf++ = b1;
3458c2ecf20Sopenharmony_ci	*buf++ = b2;
3468c2ecf20Sopenharmony_ci	*buf = freq;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic void iuu_led_activity_on(struct urb *urb)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
3528c2ecf20Sopenharmony_ci	char *buf_ptr = port->write_urb->transfer_buffer;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (xmas) {
3558c2ecf20Sopenharmony_ci		buf_ptr[0] = IUU_SET_LED;
3568c2ecf20Sopenharmony_ci		get_random_bytes(buf_ptr + 1, 6);
3578c2ecf20Sopenharmony_ci		buf_ptr[7] = 1;
3588c2ecf20Sopenharmony_ci	} else {
3598c2ecf20Sopenharmony_ci		iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
3638c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
3648c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
3658c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 8 ,
3668c2ecf20Sopenharmony_ci			  iuu_rxcmd, port);
3678c2ecf20Sopenharmony_ci	usb_submit_urb(port->write_urb, GFP_ATOMIC);
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic void iuu_led_activity_off(struct urb *urb)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
3738c2ecf20Sopenharmony_ci	char *buf_ptr = port->write_urb->transfer_buffer;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (xmas) {
3768c2ecf20Sopenharmony_ci		iuu_rxcmd(urb);
3778c2ecf20Sopenharmony_ci		return;
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
3838c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
3848c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
3858c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 8 ,
3868c2ecf20Sopenharmony_ci			  iuu_rxcmd, port);
3878c2ecf20Sopenharmony_ci	usb_submit_urb(port->write_urb, GFP_ATOMIC);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int iuu_clk(struct usb_serial_port *port, int dwFrq)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	int status;
3958c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
3968c2ecf20Sopenharmony_ci	int Count = 0;
3978c2ecf20Sopenharmony_ci	u8 FrqGenAdr = 0x69;
3988c2ecf20Sopenharmony_ci	u8 DIV = 0;		/* 8bit */
3998c2ecf20Sopenharmony_ci	u8 XDRV = 0;		/* 8bit */
4008c2ecf20Sopenharmony_ci	u8 PUMP = 0;		/* 3bit */
4018c2ecf20Sopenharmony_ci	u8 PBmsb = 0;		/* 2bit */
4028c2ecf20Sopenharmony_ci	u8 PBlsb = 0;		/* 8bit */
4038c2ecf20Sopenharmony_ci	u8 PO = 0;		/* 1bit */
4048c2ecf20Sopenharmony_ci	u8 Q = 0;		/* 7bit */
4058c2ecf20Sopenharmony_ci	/* 24bit = 3bytes */
4068c2ecf20Sopenharmony_ci	unsigned int P = 0;
4078c2ecf20Sopenharmony_ci	unsigned int P2 = 0;
4088c2ecf20Sopenharmony_ci	int frq = (int)dwFrq;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (frq == 0) {
4118c2ecf20Sopenharmony_ci		priv->buf[Count++] = IUU_UART_WRITE_I2C;
4128c2ecf20Sopenharmony_ci		priv->buf[Count++] = FrqGenAdr << 1;
4138c2ecf20Sopenharmony_ci		priv->buf[Count++] = 0x09;
4148c2ecf20Sopenharmony_ci		priv->buf[Count++] = 0x00;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		status = bulk_immediate(port, (u8 *) priv->buf, Count);
4178c2ecf20Sopenharmony_ci		if (status != 0) {
4188c2ecf20Sopenharmony_ci			dev_dbg(&port->dev, "%s - write error\n", __func__);
4198c2ecf20Sopenharmony_ci			return status;
4208c2ecf20Sopenharmony_ci		}
4218c2ecf20Sopenharmony_ci	} else if (frq == 3579000) {
4228c2ecf20Sopenharmony_ci		DIV = 100;
4238c2ecf20Sopenharmony_ci		P = 1193;
4248c2ecf20Sopenharmony_ci		Q = 40;
4258c2ecf20Sopenharmony_ci		XDRV = 0;
4268c2ecf20Sopenharmony_ci	} else if (frq == 3680000) {
4278c2ecf20Sopenharmony_ci		DIV = 105;
4288c2ecf20Sopenharmony_ci		P = 161;
4298c2ecf20Sopenharmony_ci		Q = 5;
4308c2ecf20Sopenharmony_ci		XDRV = 0;
4318c2ecf20Sopenharmony_ci	} else if (frq == 6000000) {
4328c2ecf20Sopenharmony_ci		DIV = 66;
4338c2ecf20Sopenharmony_ci		P = 66;
4348c2ecf20Sopenharmony_ci		Q = 2;
4358c2ecf20Sopenharmony_ci		XDRV = 0x28;
4368c2ecf20Sopenharmony_ci	} else {
4378c2ecf20Sopenharmony_ci		unsigned int result = 0;
4388c2ecf20Sopenharmony_ci		unsigned int tmp = 0;
4398c2ecf20Sopenharmony_ci		unsigned int check;
4408c2ecf20Sopenharmony_ci		unsigned int check2;
4418c2ecf20Sopenharmony_ci		char found = 0x00;
4428c2ecf20Sopenharmony_ci		unsigned int lQ = 2;
4438c2ecf20Sopenharmony_ci		unsigned int lP = 2055;
4448c2ecf20Sopenharmony_ci		unsigned int lDiv = 4;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		for (lQ = 2; lQ <= 47 && !found; lQ++)
4478c2ecf20Sopenharmony_ci			for (lP = 2055; lP >= 8 && !found; lP--)
4488c2ecf20Sopenharmony_ci				for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
4498c2ecf20Sopenharmony_ci					tmp = (12000000 / lDiv) * (lP / lQ);
4508c2ecf20Sopenharmony_ci					if (abs((int)(tmp - frq)) <
4518c2ecf20Sopenharmony_ci					    abs((int)(frq - result))) {
4528c2ecf20Sopenharmony_ci						check2 = (12000000 / lQ);
4538c2ecf20Sopenharmony_ci						if (check2 < 250000)
4548c2ecf20Sopenharmony_ci							continue;
4558c2ecf20Sopenharmony_ci						check = (12000000 / lQ) * lP;
4568c2ecf20Sopenharmony_ci						if (check > 400000000)
4578c2ecf20Sopenharmony_ci							continue;
4588c2ecf20Sopenharmony_ci						if (check < 100000000)
4598c2ecf20Sopenharmony_ci							continue;
4608c2ecf20Sopenharmony_ci						if (lDiv < 4 || lDiv > 127)
4618c2ecf20Sopenharmony_ci							continue;
4628c2ecf20Sopenharmony_ci						result = tmp;
4638c2ecf20Sopenharmony_ci						P = lP;
4648c2ecf20Sopenharmony_ci						DIV = lDiv;
4658c2ecf20Sopenharmony_ci						Q = lQ;
4668c2ecf20Sopenharmony_ci						if (result == frq)
4678c2ecf20Sopenharmony_ci							found = 0x01;
4688c2ecf20Sopenharmony_ci					}
4698c2ecf20Sopenharmony_ci				}
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci	P2 = ((P - PO) / 2) - 4;
4728c2ecf20Sopenharmony_ci	PUMP = 0x04;
4738c2ecf20Sopenharmony_ci	PBmsb = (P2 >> 8 & 0x03);
4748c2ecf20Sopenharmony_ci	PBlsb = P2 & 0xFF;
4758c2ecf20Sopenharmony_ci	PO = (P >> 10) & 0x01;
4768c2ecf20Sopenharmony_ci	Q = Q - 2;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
4798c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
4808c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x09;
4818c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x20;	/* Adr = 0x09 */
4828c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
4838c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
4848c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x0C;
4858c2ecf20Sopenharmony_ci	priv->buf[Count++] = DIV;	/* Adr = 0x0C */
4868c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
4878c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
4888c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x12;
4898c2ecf20Sopenharmony_ci	priv->buf[Count++] = XDRV;	/* Adr = 0x12 */
4908c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
4918c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
4928c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x13;
4938c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x6B;	/* Adr = 0x13 */
4948c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
4958c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
4968c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x40;
4978c2ecf20Sopenharmony_ci	priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
4988c2ecf20Sopenharmony_ci			     (PBmsb & 0x03);	/* Adr = 0x40 */
4998c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5008c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5018c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x41;
5028c2ecf20Sopenharmony_ci	priv->buf[Count++] = PBlsb;	/* Adr = 0x41 */
5038c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5048c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5058c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x42;
5068c2ecf20Sopenharmony_ci	priv->buf[Count++] = Q | (((PO & 0x01) << 7));	/* Adr = 0x42 */
5078c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5088c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5098c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x44;
5108c2ecf20Sopenharmony_ci	priv->buf[Count++] = (char)0xFF;	/* Adr = 0x44 */
5118c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5128c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5138c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x45;
5148c2ecf20Sopenharmony_ci	priv->buf[Count++] = (char)0xFE;	/* Adr = 0x45 */
5158c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5168c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5178c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x46;
5188c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x7F;	/* Adr = 0x46 */
5198c2ecf20Sopenharmony_ci	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
5208c2ecf20Sopenharmony_ci	priv->buf[Count++] = FrqGenAdr << 1;
5218c2ecf20Sopenharmony_ci	priv->buf[Count++] = 0x47;
5228c2ecf20Sopenharmony_ci	priv->buf[Count++] = (char)0x84;	/* Adr = 0x47 */
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	status = bulk_immediate(port, (u8 *) priv->buf, Count);
5258c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
5268c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - write error\n", __func__);
5278c2ecf20Sopenharmony_ci	return status;
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_cistatic int iuu_uart_flush(struct usb_serial_port *port)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	struct device *dev = &port->dev;
5338c2ecf20Sopenharmony_ci	int i;
5348c2ecf20Sopenharmony_ci	int status;
5358c2ecf20Sopenharmony_ci	u8 *rxcmd;
5368c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
5398c2ecf20Sopenharmony_ci		return -EIO;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	rxcmd = kmalloc(1, GFP_KERNEL);
5428c2ecf20Sopenharmony_ci	if (!rxcmd)
5438c2ecf20Sopenharmony_ci		return -ENOMEM;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	rxcmd[0] = IUU_UART_RX;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
5488c2ecf20Sopenharmony_ci		status = bulk_immediate(port, rxcmd, 1);
5498c2ecf20Sopenharmony_ci		if (status != IUU_OPERATION_OK) {
5508c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
5518c2ecf20Sopenharmony_ci			goto out_free;
5528c2ecf20Sopenharmony_ci		}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		status = read_immediate(port, &priv->len, 1);
5558c2ecf20Sopenharmony_ci		if (status != IUU_OPERATION_OK) {
5568c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
5578c2ecf20Sopenharmony_ci			goto out_free;
5588c2ecf20Sopenharmony_ci		}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci		if (priv->len > 0) {
5618c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
5628c2ecf20Sopenharmony_ci			status = read_immediate(port, priv->buf, priv->len);
5638c2ecf20Sopenharmony_ci			if (status != IUU_OPERATION_OK) {
5648c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
5658c2ecf20Sopenharmony_ci				goto out_free;
5668c2ecf20Sopenharmony_ci			}
5678c2ecf20Sopenharmony_ci		}
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
5708c2ecf20Sopenharmony_ci	iuu_led(port, 0, 0xF000, 0, 0xFF);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ciout_free:
5738c2ecf20Sopenharmony_ci	kfree(rxcmd);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	return status;
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic void read_buf_callback(struct urb *urb)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
5818c2ecf20Sopenharmony_ci	unsigned char *data = urb->transfer_buffer;
5828c2ecf20Sopenharmony_ci	int status = urb->status;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	if (status) {
5858c2ecf20Sopenharmony_ci		if (status == -EPROTO) {
5868c2ecf20Sopenharmony_ci			/* reschedule needed */
5878c2ecf20Sopenharmony_ci		}
5888c2ecf20Sopenharmony_ci		return;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	if (urb->actual_length) {
5948c2ecf20Sopenharmony_ci		tty_insert_flip_string(&port->port, data, urb->actual_length);
5958c2ecf20Sopenharmony_ci		tty_flip_buffer_push(&port->port);
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci	iuu_led_activity_on(urb);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic int iuu_bulk_write(struct usb_serial_port *port)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
6038c2ecf20Sopenharmony_ci	unsigned long flags;
6048c2ecf20Sopenharmony_ci	int result;
6058c2ecf20Sopenharmony_ci	int buf_len;
6068c2ecf20Sopenharmony_ci	char *buf_ptr = port->write_urb->transfer_buffer;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
6098c2ecf20Sopenharmony_ci	*buf_ptr++ = IUU_UART_ESC;
6108c2ecf20Sopenharmony_ci	*buf_ptr++ = IUU_UART_TX;
6118c2ecf20Sopenharmony_ci	*buf_ptr++ = priv->writelen;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	memcpy(buf_ptr, priv->writebuf, priv->writelen);
6148c2ecf20Sopenharmony_ci	buf_len = priv->writelen;
6158c2ecf20Sopenharmony_ci	priv->writelen = 0;
6168c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
6178c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
6188c2ecf20Sopenharmony_ci		buf_len, buf_len, buf_ptr);
6198c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
6208c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
6218c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
6228c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, buf_len + 3,
6238c2ecf20Sopenharmony_ci			  iuu_rxcmd, port);
6248c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
6258c2ecf20Sopenharmony_ci	usb_serial_port_softint(port);
6268c2ecf20Sopenharmony_ci	return result;
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cistatic int iuu_read_buf(struct usb_serial_port *port, int len)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	int result;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
6348c2ecf20Sopenharmony_ci			  usb_rcvbulkpipe(port->serial->dev,
6358c2ecf20Sopenharmony_ci					  port->bulk_in_endpointAddress),
6368c2ecf20Sopenharmony_ci			  port->read_urb->transfer_buffer, len,
6378c2ecf20Sopenharmony_ci			  read_buf_callback, port);
6388c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
6398c2ecf20Sopenharmony_ci	return result;
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic void iuu_uart_read_callback(struct urb *urb)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
6458c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
6468c2ecf20Sopenharmony_ci	unsigned long flags;
6478c2ecf20Sopenharmony_ci	int status = urb->status;
6488c2ecf20Sopenharmony_ci	int error = 0;
6498c2ecf20Sopenharmony_ci	int len = 0;
6508c2ecf20Sopenharmony_ci	unsigned char *data = urb->transfer_buffer;
6518c2ecf20Sopenharmony_ci	priv->poll++;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if (status) {
6548c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
6558c2ecf20Sopenharmony_ci		/* error stop all */
6568c2ecf20Sopenharmony_ci		return;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	if (urb->actual_length == 1)
6608c2ecf20Sopenharmony_ci		len = (int) data[0];
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	if (urb->actual_length > 1) {
6638c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
6648c2ecf20Sopenharmony_ci		    urb->actual_length);
6658c2ecf20Sopenharmony_ci		error = 1;
6668c2ecf20Sopenharmony_ci		return;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci	/* if len > 0 call readbuf */
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	if (len > 0 && error == 0) {
6718c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
6728c2ecf20Sopenharmony_ci			__func__, len);
6738c2ecf20Sopenharmony_ci		status = iuu_read_buf(port, len);
6748c2ecf20Sopenharmony_ci		return;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci	/* need to update status  ? */
6778c2ecf20Sopenharmony_ci	if (priv->poll > 99) {
6788c2ecf20Sopenharmony_ci		status = iuu_status(port);
6798c2ecf20Sopenharmony_ci		priv->poll = 0;
6808c2ecf20Sopenharmony_ci		return;
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/* reset waiting ? */
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (priv->reset == 1) {
6868c2ecf20Sopenharmony_ci		status = iuu_reset(port, 0xC);
6878c2ecf20Sopenharmony_ci		return;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci	/* Writebuf is waiting */
6908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
6918c2ecf20Sopenharmony_ci	if (priv->writelen > 0) {
6928c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
6938c2ecf20Sopenharmony_ci		status = iuu_bulk_write(port);
6948c2ecf20Sopenharmony_ci		return;
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
6978c2ecf20Sopenharmony_ci	/* if nothing to write call again rxcmd */
6988c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
6998c2ecf20Sopenharmony_ci	iuu_led_activity_off(urb);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
7038c2ecf20Sopenharmony_ci			  const u8 *buf, int count)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
7068c2ecf20Sopenharmony_ci	unsigned long flags;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	count = min(count, 256 - priv->writelen);
7118c2ecf20Sopenharmony_ci	if (count == 0)
7128c2ecf20Sopenharmony_ci		goto out;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	/* fill the buffer */
7158c2ecf20Sopenharmony_ci	memcpy(priv->writebuf + priv->writelen, buf, count);
7168c2ecf20Sopenharmony_ci	priv->writelen += count;
7178c2ecf20Sopenharmony_ciout:
7188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return count;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic void read_rxcmd_callback(struct urb *urb)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
7268c2ecf20Sopenharmony_ci	int result;
7278c2ecf20Sopenharmony_ci	int status = urb->status;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (status) {
7308c2ecf20Sopenharmony_ci		/* error stop all */
7318c2ecf20Sopenharmony_ci		return;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
7358c2ecf20Sopenharmony_ci			  usb_rcvbulkpipe(port->serial->dev,
7368c2ecf20Sopenharmony_ci					  port->bulk_in_endpointAddress),
7378c2ecf20Sopenharmony_ci			  port->read_urb->transfer_buffer, 256,
7388c2ecf20Sopenharmony_ci			  iuu_uart_read_callback, port);
7398c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
7408c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
7418c2ecf20Sopenharmony_ci}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cistatic int iuu_uart_on(struct usb_serial_port *port)
7448c2ecf20Sopenharmony_ci{
7458c2ecf20Sopenharmony_ci	int status;
7468c2ecf20Sopenharmony_ci	u8 *buf;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	buf = kmalloc(4, GFP_KERNEL);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (!buf)
7518c2ecf20Sopenharmony_ci		return -ENOMEM;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	buf[0] = IUU_UART_ENABLE;
7548c2ecf20Sopenharmony_ci	buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
7558c2ecf20Sopenharmony_ci	buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
7568c2ecf20Sopenharmony_ci	buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	status = bulk_immediate(port, buf, 4);
7598c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK) {
7608c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
7618c2ecf20Sopenharmony_ci		goto uart_enable_failed;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci	/*  iuu_reset() the card after iuu_uart_on() */
7648c2ecf20Sopenharmony_ci	status = iuu_uart_flush(port);
7658c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
7668c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
7678c2ecf20Sopenharmony_ciuart_enable_failed:
7688c2ecf20Sopenharmony_ci	kfree(buf);
7698c2ecf20Sopenharmony_ci	return status;
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci/*  Disables the IUU UART (a.k.a. the Phoenix voiderface) */
7738c2ecf20Sopenharmony_cistatic int iuu_uart_off(struct usb_serial_port *port)
7748c2ecf20Sopenharmony_ci{
7758c2ecf20Sopenharmony_ci	int status;
7768c2ecf20Sopenharmony_ci	u8 *buf;
7778c2ecf20Sopenharmony_ci	buf = kmalloc(1, GFP_KERNEL);
7788c2ecf20Sopenharmony_ci	if (!buf)
7798c2ecf20Sopenharmony_ci		return -ENOMEM;
7808c2ecf20Sopenharmony_ci	buf[0] = IUU_UART_DISABLE;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	status = bulk_immediate(port, buf, 1);
7838c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
7848c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	kfree(buf);
7878c2ecf20Sopenharmony_ci	return status;
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
7918c2ecf20Sopenharmony_ci			 u32 *actual, u8 parity)
7928c2ecf20Sopenharmony_ci{
7938c2ecf20Sopenharmony_ci	int status;
7948c2ecf20Sopenharmony_ci	u32 baud;
7958c2ecf20Sopenharmony_ci	u8 *dataout;
7968c2ecf20Sopenharmony_ci	u8 DataCount = 0;
7978c2ecf20Sopenharmony_ci	u8 T1Frekvens = 0;
7988c2ecf20Sopenharmony_ci	u8 T1reload = 0;
7998c2ecf20Sopenharmony_ci	unsigned int T1FrekvensHZ = 0;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
8028c2ecf20Sopenharmony_ci	dataout = kmalloc(5, GFP_KERNEL);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	if (!dataout)
8058c2ecf20Sopenharmony_ci		return -ENOMEM;
8068c2ecf20Sopenharmony_ci	/*baud = (((priv->clk / 35) * baud_base) / 100000); */
8078c2ecf20Sopenharmony_ci	baud = baud_base;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (baud < 1200 || baud > 230400) {
8108c2ecf20Sopenharmony_ci		kfree(dataout);
8118c2ecf20Sopenharmony_ci		return IUU_INVALID_PARAMETER;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci	if (baud > 977) {
8148c2ecf20Sopenharmony_ci		T1Frekvens = 3;
8158c2ecf20Sopenharmony_ci		T1FrekvensHZ = 500000;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	if (baud > 3906) {
8198c2ecf20Sopenharmony_ci		T1Frekvens = 2;
8208c2ecf20Sopenharmony_ci		T1FrekvensHZ = 2000000;
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	if (baud > 11718) {
8248c2ecf20Sopenharmony_ci		T1Frekvens = 1;
8258c2ecf20Sopenharmony_ci		T1FrekvensHZ = 6000000;
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	if (baud > 46875) {
8298c2ecf20Sopenharmony_ci		T1Frekvens = 0;
8308c2ecf20Sopenharmony_ci		T1FrekvensHZ = 24000000;
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	/*  magic number here:  ENTER_FIRMWARE_UPDATE; */
8368c2ecf20Sopenharmony_ci	dataout[DataCount++] = IUU_UART_ESC;
8378c2ecf20Sopenharmony_ci	/*  magic number here:  CHANGE_BAUD; */
8388c2ecf20Sopenharmony_ci	dataout[DataCount++] = IUU_UART_CHANGE;
8398c2ecf20Sopenharmony_ci	dataout[DataCount++] = T1Frekvens;
8408c2ecf20Sopenharmony_ci	dataout[DataCount++] = T1reload;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	*actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	switch (parity & 0x0F) {
8458c2ecf20Sopenharmony_ci	case IUU_PARITY_NONE:
8468c2ecf20Sopenharmony_ci		dataout[DataCount++] = 0x00;
8478c2ecf20Sopenharmony_ci		break;
8488c2ecf20Sopenharmony_ci	case IUU_PARITY_EVEN:
8498c2ecf20Sopenharmony_ci		dataout[DataCount++] = 0x01;
8508c2ecf20Sopenharmony_ci		break;
8518c2ecf20Sopenharmony_ci	case IUU_PARITY_ODD:
8528c2ecf20Sopenharmony_ci		dataout[DataCount++] = 0x02;
8538c2ecf20Sopenharmony_ci		break;
8548c2ecf20Sopenharmony_ci	case IUU_PARITY_MARK:
8558c2ecf20Sopenharmony_ci		dataout[DataCount++] = 0x03;
8568c2ecf20Sopenharmony_ci		break;
8578c2ecf20Sopenharmony_ci	case IUU_PARITY_SPACE:
8588c2ecf20Sopenharmony_ci		dataout[DataCount++] = 0x04;
8598c2ecf20Sopenharmony_ci		break;
8608c2ecf20Sopenharmony_ci	default:
8618c2ecf20Sopenharmony_ci		kfree(dataout);
8628c2ecf20Sopenharmony_ci		return IUU_INVALID_PARAMETER;
8638c2ecf20Sopenharmony_ci		break;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	switch (parity & 0xF0) {
8678c2ecf20Sopenharmony_ci	case IUU_ONE_STOP_BIT:
8688c2ecf20Sopenharmony_ci		dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
8698c2ecf20Sopenharmony_ci		break;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	case IUU_TWO_STOP_BITS:
8728c2ecf20Sopenharmony_ci		dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
8738c2ecf20Sopenharmony_ci		break;
8748c2ecf20Sopenharmony_ci	default:
8758c2ecf20Sopenharmony_ci		kfree(dataout);
8768c2ecf20Sopenharmony_ci		return IUU_INVALID_PARAMETER;
8778c2ecf20Sopenharmony_ci		break;
8788c2ecf20Sopenharmony_ci	}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	status = bulk_immediate(port, dataout, DataCount);
8818c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
8828c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
8838c2ecf20Sopenharmony_ci	kfree(dataout);
8848c2ecf20Sopenharmony_ci	return status;
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic void iuu_set_termios(struct tty_struct *tty,
8888c2ecf20Sopenharmony_ci		struct usb_serial_port *port, struct ktermios *old_termios)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	const u32 supported_mask = CMSPAR|PARENB|PARODD;
8918c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
8928c2ecf20Sopenharmony_ci	unsigned int cflag = tty->termios.c_cflag;
8938c2ecf20Sopenharmony_ci	int status;
8948c2ecf20Sopenharmony_ci	u32 actual;
8958c2ecf20Sopenharmony_ci	u32 parity;
8968c2ecf20Sopenharmony_ci	int csize = CS7;
8978c2ecf20Sopenharmony_ci	int baud;
8988c2ecf20Sopenharmony_ci	u32 newval = cflag & supported_mask;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	/* Just use the ospeed. ispeed should be the same. */
9018c2ecf20Sopenharmony_ci	baud = tty->termios.c_ospeed;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	/* compute the parity parameter */
9068c2ecf20Sopenharmony_ci	parity = 0;
9078c2ecf20Sopenharmony_ci	if (cflag & CMSPAR) {	/* Using mark space */
9088c2ecf20Sopenharmony_ci		if (cflag & PARODD)
9098c2ecf20Sopenharmony_ci			parity |= IUU_PARITY_SPACE;
9108c2ecf20Sopenharmony_ci		else
9118c2ecf20Sopenharmony_ci			parity |= IUU_PARITY_MARK;
9128c2ecf20Sopenharmony_ci	} else if (!(cflag & PARENB)) {
9138c2ecf20Sopenharmony_ci		parity |= IUU_PARITY_NONE;
9148c2ecf20Sopenharmony_ci		csize = CS8;
9158c2ecf20Sopenharmony_ci	} else if (cflag & PARODD)
9168c2ecf20Sopenharmony_ci		parity |= IUU_PARITY_ODD;
9178c2ecf20Sopenharmony_ci	else
9188c2ecf20Sopenharmony_ci		parity |= IUU_PARITY_EVEN;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	/* set it */
9238c2ecf20Sopenharmony_ci	status = iuu_uart_baud(port,
9248c2ecf20Sopenharmony_ci			baud * priv->boost / 100,
9258c2ecf20Sopenharmony_ci			&actual, parity);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	/* set the termios value to the real one, so the user now what has
9288c2ecf20Sopenharmony_ci	 * changed. We support few fields so its easies to copy the old hw
9298c2ecf20Sopenharmony_ci	 * settings back over and then adjust them
9308c2ecf20Sopenharmony_ci	 */
9318c2ecf20Sopenharmony_ci	if (old_termios)
9328c2ecf20Sopenharmony_ci		tty_termios_copy_hw(&tty->termios, old_termios);
9338c2ecf20Sopenharmony_ci	if (status != 0)	/* Set failed - return old bits */
9348c2ecf20Sopenharmony_ci		return;
9358c2ecf20Sopenharmony_ci	/* Re-encode speed, parity and csize */
9368c2ecf20Sopenharmony_ci	tty_encode_baud_rate(tty, baud, baud);
9378c2ecf20Sopenharmony_ci	tty->termios.c_cflag &= ~(supported_mask|CSIZE);
9388c2ecf20Sopenharmony_ci	tty->termios.c_cflag |= newval | csize;
9398c2ecf20Sopenharmony_ci}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_cistatic void iuu_close(struct usb_serial_port *port)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	/* iuu_led (port,255,0,0,0); */
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	iuu_uart_off(port);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	usb_kill_urb(port->write_urb);
9488c2ecf20Sopenharmony_ci	usb_kill_urb(port->read_urb);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	iuu_led(port, 0, 0, 0xF000, 0xFF);
9518c2ecf20Sopenharmony_ci}
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_cistatic void iuu_init_termios(struct tty_struct *tty)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	tty->termios.c_cflag = B9600 | CS8 | CSTOPB | CREAD | PARENB | CLOCAL;
9568c2ecf20Sopenharmony_ci	tty->termios.c_ispeed = 9600;
9578c2ecf20Sopenharmony_ci	tty->termios.c_ospeed = 9600;
9588c2ecf20Sopenharmony_ci	tty->termios.c_lflag = 0;
9598c2ecf20Sopenharmony_ci	tty->termios.c_oflag = 0;
9608c2ecf20Sopenharmony_ci	tty->termios.c_iflag = 0;
9618c2ecf20Sopenharmony_ci}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_cistatic int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
9668c2ecf20Sopenharmony_ci	struct device *dev = &port->dev;
9678c2ecf20Sopenharmony_ci	int result;
9688c2ecf20Sopenharmony_ci	int baud;
9698c2ecf20Sopenharmony_ci	u32 actual;
9708c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	baud = tty->termios.c_ospeed;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s - baud %d\n", __func__, baud);
9758c2ecf20Sopenharmony_ci	usb_clear_halt(serial->dev, port->write_urb->pipe);
9768c2ecf20Sopenharmony_ci	usb_clear_halt(serial->dev, port->read_urb->pipe);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	priv->poll = 0;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci#define SOUP(a, b, c, d)  do { \
9818c2ecf20Sopenharmony_ci	result = usb_control_msg(port->serial->dev,	\
9828c2ecf20Sopenharmony_ci				usb_sndctrlpipe(port->serial->dev, 0),	\
9838c2ecf20Sopenharmony_ci				b, a, c, d, NULL, 0, 1000); \
9848c2ecf20Sopenharmony_ci	dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x  %d\n", a, b, c, d, result); } while (0)
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	/*  This is not UART related but IUU USB driver related or something */
9878c2ecf20Sopenharmony_ci	/*  like that. Basically no IUU will accept any commands from the USB */
9888c2ecf20Sopenharmony_ci	/*  host unless it has received the following message */
9898c2ecf20Sopenharmony_ci	/* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	SOUP(0x03, 0x02, 0x02, 0x0);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
9948c2ecf20Sopenharmony_ci	iuu_uart_on(port);
9958c2ecf20Sopenharmony_ci	if (boost < 100)
9968c2ecf20Sopenharmony_ci		boost = 100;
9978c2ecf20Sopenharmony_ci	priv->boost = boost;
9988c2ecf20Sopenharmony_ci	switch (clockmode) {
9998c2ecf20Sopenharmony_ci	case 2:		/*  3.680 Mhz */
10008c2ecf20Sopenharmony_ci		priv->clk = IUU_CLK_3680000;
10018c2ecf20Sopenharmony_ci		iuu_clk(port, IUU_CLK_3680000 * boost / 100);
10028c2ecf20Sopenharmony_ci		result =
10038c2ecf20Sopenharmony_ci		    iuu_uart_baud(port, baud * boost / 100, &actual,
10048c2ecf20Sopenharmony_ci				  IUU_PARITY_EVEN);
10058c2ecf20Sopenharmony_ci		break;
10068c2ecf20Sopenharmony_ci	case 3:		/*  6.00 Mhz */
10078c2ecf20Sopenharmony_ci		iuu_clk(port, IUU_CLK_6000000 * boost / 100);
10088c2ecf20Sopenharmony_ci		priv->clk = IUU_CLK_6000000;
10098c2ecf20Sopenharmony_ci		/* Ratio of 6000000 to 3500000 for baud 9600 */
10108c2ecf20Sopenharmony_ci		result =
10118c2ecf20Sopenharmony_ci		    iuu_uart_baud(port, 16457 * boost / 100, &actual,
10128c2ecf20Sopenharmony_ci				  IUU_PARITY_EVEN);
10138c2ecf20Sopenharmony_ci		break;
10148c2ecf20Sopenharmony_ci	default:		/*  3.579 Mhz */
10158c2ecf20Sopenharmony_ci		iuu_clk(port, IUU_CLK_3579000 * boost / 100);
10168c2ecf20Sopenharmony_ci		priv->clk = IUU_CLK_3579000;
10178c2ecf20Sopenharmony_ci		result =
10188c2ecf20Sopenharmony_ci		    iuu_uart_baud(port, baud * boost / 100, &actual,
10198c2ecf20Sopenharmony_ci				  IUU_PARITY_EVEN);
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/* set the cardin cardout signals */
10238c2ecf20Sopenharmony_ci	switch (cdmode) {
10248c2ecf20Sopenharmony_ci	case 0:
10258c2ecf20Sopenharmony_ci		iuu_cardin = 0;
10268c2ecf20Sopenharmony_ci		iuu_cardout = 0;
10278c2ecf20Sopenharmony_ci		break;
10288c2ecf20Sopenharmony_ci	case 1:
10298c2ecf20Sopenharmony_ci		iuu_cardin = TIOCM_CD;
10308c2ecf20Sopenharmony_ci		iuu_cardout =  0;
10318c2ecf20Sopenharmony_ci		break;
10328c2ecf20Sopenharmony_ci	case 2:
10338c2ecf20Sopenharmony_ci		iuu_cardin = 0;
10348c2ecf20Sopenharmony_ci		iuu_cardout = TIOCM_CD;
10358c2ecf20Sopenharmony_ci		break;
10368c2ecf20Sopenharmony_ci	case 3:
10378c2ecf20Sopenharmony_ci		iuu_cardin = TIOCM_DSR;
10388c2ecf20Sopenharmony_ci		iuu_cardout = 0;
10398c2ecf20Sopenharmony_ci		break;
10408c2ecf20Sopenharmony_ci	case 4:
10418c2ecf20Sopenharmony_ci		iuu_cardin = 0;
10428c2ecf20Sopenharmony_ci		iuu_cardout = TIOCM_DSR;
10438c2ecf20Sopenharmony_ci		break;
10448c2ecf20Sopenharmony_ci	case 5:
10458c2ecf20Sopenharmony_ci		iuu_cardin = TIOCM_CTS;
10468c2ecf20Sopenharmony_ci		iuu_cardout = 0;
10478c2ecf20Sopenharmony_ci		break;
10488c2ecf20Sopenharmony_ci	case 6:
10498c2ecf20Sopenharmony_ci		iuu_cardin = 0;
10508c2ecf20Sopenharmony_ci		iuu_cardout = TIOCM_CTS;
10518c2ecf20Sopenharmony_ci		break;
10528c2ecf20Sopenharmony_ci	case 7:
10538c2ecf20Sopenharmony_ci		iuu_cardin = TIOCM_RNG;
10548c2ecf20Sopenharmony_ci		iuu_cardout = 0;
10558c2ecf20Sopenharmony_ci		break;
10568c2ecf20Sopenharmony_ci	case 8:
10578c2ecf20Sopenharmony_ci		iuu_cardin = 0;
10588c2ecf20Sopenharmony_ci		iuu_cardout = TIOCM_RNG;
10598c2ecf20Sopenharmony_ci	}
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	iuu_uart_flush(port);
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s - initialization done\n", __func__);
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
10668c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
10678c2ecf20Sopenharmony_ci			  usb_sndbulkpipe(port->serial->dev,
10688c2ecf20Sopenharmony_ci					  port->bulk_out_endpointAddress),
10698c2ecf20Sopenharmony_ci			  port->write_urb->transfer_buffer, 1,
10708c2ecf20Sopenharmony_ci			  read_rxcmd_callback, port);
10718c2ecf20Sopenharmony_ci	result = usb_submit_urb(port->write_urb, GFP_KERNEL);
10728c2ecf20Sopenharmony_ci	if (result) {
10738c2ecf20Sopenharmony_ci		dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
10748c2ecf20Sopenharmony_ci		iuu_close(port);
10758c2ecf20Sopenharmony_ci	} else {
10768c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - rxcmd OK\n", __func__);
10778c2ecf20Sopenharmony_ci	}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	return result;
10808c2ecf20Sopenharmony_ci}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci/* how to change VCC */
10838c2ecf20Sopenharmony_cistatic int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	int status;
10868c2ecf20Sopenharmony_ci	u8 *buf;
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	buf = kmalloc(5, GFP_KERNEL);
10898c2ecf20Sopenharmony_ci	if (!buf)
10908c2ecf20Sopenharmony_ci		return -ENOMEM;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	buf[0] = IUU_SET_VCC;
10938c2ecf20Sopenharmony_ci	buf[1] = vcc & 0xFF;
10948c2ecf20Sopenharmony_ci	buf[2] = (vcc >> 8) & 0xFF;
10958c2ecf20Sopenharmony_ci	buf[3] = (vcc >> 16) & 0xFF;
10968c2ecf20Sopenharmony_ci	buf[4] = (vcc >> 24) & 0xFF;
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	status = bulk_immediate(port, buf, 5);
10998c2ecf20Sopenharmony_ci	kfree(buf);
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	if (status != IUU_OPERATION_OK)
11028c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
11038c2ecf20Sopenharmony_ci	else
11048c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	return status;
11078c2ecf20Sopenharmony_ci}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci/*
11108c2ecf20Sopenharmony_ci * Sysfs Attributes
11118c2ecf20Sopenharmony_ci */
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_cistatic ssize_t vcc_mode_show(struct device *dev,
11148c2ecf20Sopenharmony_ci	struct device_attribute *attr, char *buf)
11158c2ecf20Sopenharmony_ci{
11168c2ecf20Sopenharmony_ci	struct usb_serial_port *port = to_usb_serial_port(dev);
11178c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", priv->vcc);
11208c2ecf20Sopenharmony_ci}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_cistatic ssize_t vcc_mode_store(struct device *dev,
11238c2ecf20Sopenharmony_ci	struct device_attribute *attr, const char *buf, size_t count)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci	struct usb_serial_port *port = to_usb_serial_port(dev);
11268c2ecf20Sopenharmony_ci	struct iuu_private *priv = usb_get_serial_port_data(port);
11278c2ecf20Sopenharmony_ci	unsigned long v;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (kstrtoul(buf, 10, &v)) {
11308c2ecf20Sopenharmony_ci		dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
11318c2ecf20Sopenharmony_ci				__func__, buf);
11328c2ecf20Sopenharmony_ci		goto fail_store_vcc_mode;
11338c2ecf20Sopenharmony_ci	}
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	if ((v != 3) && (v != 5)) {
11388c2ecf20Sopenharmony_ci		dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
11398c2ecf20Sopenharmony_ci	} else {
11408c2ecf20Sopenharmony_ci		iuu_vcc_set(port, v);
11418c2ecf20Sopenharmony_ci		priv->vcc = v;
11428c2ecf20Sopenharmony_ci	}
11438c2ecf20Sopenharmony_cifail_store_vcc_mode:
11448c2ecf20Sopenharmony_ci	return count;
11458c2ecf20Sopenharmony_ci}
11468c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vcc_mode);
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic int iuu_create_sysfs_attrs(struct usb_serial_port *port)
11498c2ecf20Sopenharmony_ci{
11508c2ecf20Sopenharmony_ci	return device_create_file(&port->dev, &dev_attr_vcc_mode);
11518c2ecf20Sopenharmony_ci}
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_cistatic int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
11548c2ecf20Sopenharmony_ci{
11558c2ecf20Sopenharmony_ci	device_remove_file(&port->dev, &dev_attr_vcc_mode);
11568c2ecf20Sopenharmony_ci	return 0;
11578c2ecf20Sopenharmony_ci}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci/*
11608c2ecf20Sopenharmony_ci * End Sysfs Attributes
11618c2ecf20Sopenharmony_ci */
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_cistatic struct usb_serial_driver iuu_device = {
11648c2ecf20Sopenharmony_ci	.driver = {
11658c2ecf20Sopenharmony_ci		   .owner = THIS_MODULE,
11668c2ecf20Sopenharmony_ci		   .name = "iuu_phoenix",
11678c2ecf20Sopenharmony_ci		   },
11688c2ecf20Sopenharmony_ci	.id_table = id_table,
11698c2ecf20Sopenharmony_ci	.num_ports = 1,
11708c2ecf20Sopenharmony_ci	.num_bulk_in = 1,
11718c2ecf20Sopenharmony_ci	.num_bulk_out = 1,
11728c2ecf20Sopenharmony_ci	.bulk_in_size = 512,
11738c2ecf20Sopenharmony_ci	.bulk_out_size = 512,
11748c2ecf20Sopenharmony_ci	.open = iuu_open,
11758c2ecf20Sopenharmony_ci	.close = iuu_close,
11768c2ecf20Sopenharmony_ci	.write = iuu_uart_write,
11778c2ecf20Sopenharmony_ci	.read_bulk_callback = iuu_uart_read_callback,
11788c2ecf20Sopenharmony_ci	.tiocmget = iuu_tiocmget,
11798c2ecf20Sopenharmony_ci	.tiocmset = iuu_tiocmset,
11808c2ecf20Sopenharmony_ci	.set_termios = iuu_set_termios,
11818c2ecf20Sopenharmony_ci	.init_termios = iuu_init_termios,
11828c2ecf20Sopenharmony_ci	.port_probe = iuu_port_probe,
11838c2ecf20Sopenharmony_ci	.port_remove = iuu_port_remove,
11848c2ecf20Sopenharmony_ci};
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
11878c2ecf20Sopenharmony_ci	&iuu_device, NULL
11888c2ecf20Sopenharmony_ci};
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
11958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_cimodule_param(xmas, bool, S_IRUGO | S_IWUSR);
11988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_cimodule_param(boost, int, S_IRUGO | S_IWUSR);
12018c2ecf20Sopenharmony_ciMODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cimodule_param(clockmode, int, S_IRUGO | S_IWUSR);
12048c2ecf20Sopenharmony_ciMODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
12058c2ecf20Sopenharmony_ci		"3=6 Mhz)");
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_cimodule_param(cdmode, int, S_IRUGO | S_IWUSR);
12088c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
12098c2ecf20Sopenharmony_ci		 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_cimodule_param(vcc_default, int, S_IRUGO | S_IWUSR);
12128c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
12138c2ecf20Sopenharmony_ci		"for 5V). Default to 5.");
1214