blob: 2fb71303ec3ab95b82c6bea40d9604ad4bdc586e [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Alain Degreffe60a8fc02007-10-26 13:51:49 +02002/*
3 * Infinity Unlimited USB Phoenix driver
4 *
James Courtier-Dutton89b54392010-05-21 11:53:25 +01005 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
6
Alain Degreffe60a8fc02007-10-26 13:51:49 +02007 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
8 *
9 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
10 *
Alain Degreffe60a8fc02007-10-26 13:51:49 +020011 * And tested with help of WB Electronics
Alain Degreffe60a8fc02007-10-26 13:51:49 +020012 */
13#include <linux/kernel.h>
14#include <linux/errno.h>
Alain Degreffe60a8fc02007-10-26 13:51:49 +020015#include <linux/slab.h>
16#include <linux/tty.h>
17#include <linux/tty_driver.h>
18#include <linux/tty_flip.h>
19#include <linux/serial.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/spinlock.h>
23#include <linux/uaccess.h>
24#include <linux/usb.h>
25#include <linux/usb/serial.h>
26#include "iuu_phoenix.h"
27#include <linux/random.h>
28
Alain Degreffe60a8fc02007-10-26 13:51:49 +020029#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
30
Németh Márton7d40d7e2010-01-10 15:34:24 +010031static const struct usb_device_id id_table[] = {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020032 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
33 {} /* Terminating entry */
34};
35MODULE_DEVICE_TABLE(usb, id_table);
36
Alain Degreffe60a8fc02007-10-26 13:51:49 +020037/* turbo parameter */
38static int boost = 100;
39static int clockmode = 1;
40static int cdmode = 1;
41static int iuu_cardin;
42static int iuu_cardout;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020044static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020045
Johan Hovold0978c942012-10-18 10:52:17 +020046static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
47static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020048static void read_rxcmd_callback(struct urb *urb);
49
50struct iuu_private {
51 spinlock_t lock; /* store irq state */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020052 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020053 int tiostatus; /* store IUART SIGNAL for tiocmget call */
54 u8 reset; /* if 1 reset is needed */
55 int poll; /* number of poll */
56 u8 *writebuf; /* buffer for writing to device */
57 int writelen; /* num of byte to write to device */
58 u8 *buf; /* used for initialize speed */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020059 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020060 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010061 u32 baud;
62 u32 boost;
63 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020064};
65
Johan Hovold53636552012-10-17 13:34:59 +020066static int iuu_port_probe(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +020067{
68 struct iuu_private *priv;
Johan Hovold0978c942012-10-18 10:52:17 +020069 int ret;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070070
Alain Degreffe60a8fc02007-10-26 13:51:49 +020071 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020072 if (!priv)
73 return -ENOMEM;
Johan Hovold53636552012-10-17 13:34:59 +020074
75 priv->buf = kzalloc(256, GFP_KERNEL);
76 if (!priv->buf) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020077 kfree(priv);
78 return -ENOMEM;
79 }
Johan Hovold53636552012-10-17 13:34:59 +020080
81 priv->writebuf = kzalloc(256, GFP_KERNEL);
82 if (!priv->writebuf) {
83 kfree(priv->buf);
84 kfree(priv);
85 return -ENOMEM;
86 }
87
Olivier Bornete55c6d02009-08-18 21:05:57 +020088 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020089 spin_lock_init(&priv->lock);
Johan Hovold53636552012-10-17 13:34:59 +020090
91 usb_set_serial_port_data(port, priv);
92
Johan Hovold0978c942012-10-18 10:52:17 +020093 ret = iuu_create_sysfs_attrs(port);
94 if (ret) {
95 kfree(priv->writebuf);
96 kfree(priv->buf);
97 kfree(priv);
98 return ret;
99 }
100
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200101 return 0;
102}
103
Johan Hovold53636552012-10-17 13:34:59 +0200104static int iuu_port_remove(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200105{
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200106 struct iuu_private *priv = usb_get_serial_port_data(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200107
Johan Hovold0978c942012-10-18 10:52:17 +0200108 iuu_remove_sysfs_attrs(port);
Johan Hovold53636552012-10-17 13:34:59 +0200109 kfree(priv->writebuf);
110 kfree(priv->buf);
111 kfree(priv);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200112
Johan Hovold53636552012-10-17 13:34:59 +0200113 return 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200114}
115
Alan Cox20b9d172011-02-14 16:26:50 +0000116static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200117 unsigned int set, unsigned int clear)
118{
Alan Cox95da3102008-07-22 11:09:07 +0100119 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200120 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000121 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200122
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000123 /* FIXME: locking on tiomstatus */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700124 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
125 __func__, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000126
127 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200128
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100129 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700130 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200131 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200132 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100133 if (set & TIOCM_RTS)
134 priv->tiostatus = TIOCM_RTS;
135
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000136 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200137 return 0;
138}
139
140/* This is used to provide a carrier detect mechanism
141 * When a card is present, the response is 0x00
142 * When no card , the reader respond with TIOCM_CD
143 * This is known as CD autodetect mechanism
144 */
Alan Cox60b33c12011-02-14 16:26:14 +0000145static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200146{
Alan Cox95da3102008-07-22 11:09:07 +0100147 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200148 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000149 unsigned long flags;
150 int rc;
151
152 spin_lock_irqsave(&priv->lock, flags);
153 rc = priv->tiostatus;
154 spin_unlock_irqrestore(&priv->lock, flags);
155
156 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200157}
158
159static void iuu_rxcmd(struct urb *urb)
160{
Ming Leicdc97792008-02-24 18:41:47 +0800161 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200162 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800163 int status = urb->status;
164
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800165 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700166 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200167 /* error stop all */
168 return;
169 }
170
171
172 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
173 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
174 usb_sndbulkpipe(port->serial->dev,
175 port->bulk_out_endpointAddress),
176 port->write_urb->transfer_buffer, 1,
177 read_rxcmd_callback, port);
178 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
179}
180
181static int iuu_reset(struct usb_serial_port *port, u8 wt)
182{
183 struct iuu_private *priv = usb_get_serial_port_data(port);
184 int result;
185 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200186
187 /* Prepare the reset sequence */
188
189 *buf_ptr++ = IUU_RST_SET;
190 *buf_ptr++ = IUU_DELAY_MS;
191 *buf_ptr++ = wt;
192 *buf_ptr = IUU_RST_CLEAR;
193
194 /* send the sequence */
195
196 usb_fill_bulk_urb(port->write_urb,
197 port->serial->dev,
198 usb_sndbulkpipe(port->serial->dev,
199 port->bulk_out_endpointAddress),
200 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
201 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
202 priv->reset = 0;
203 return result;
204}
205
206/* Status Function
207 * Return value is
208 * 0x00 = no card
209 * 0x01 = smartcard
210 * 0x02 = sim card
211 */
212static void iuu_update_status_callback(struct urb *urb)
213{
Ming Leicdc97792008-02-24 18:41:47 +0800214 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200215 struct iuu_private *priv = usb_get_serial_port_data(port);
216 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800217 int status = urb->status;
218
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800219 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700220 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200221 /* error stop all */
222 return;
223 }
224
225 st = urb->transfer_buffer;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700226 dev_dbg(&port->dev, "%s - enter\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200227 if (urb->actual_length == 1) {
228 switch (st[0]) {
229 case 0x1:
230 priv->tiostatus = iuu_cardout;
231 break;
232 case 0x0:
233 priv->tiostatus = iuu_cardin;
234 break;
235 default:
236 priv->tiostatus = iuu_cardin;
237 }
238 }
239 iuu_rxcmd(urb);
240}
241
242static void iuu_status_callback(struct urb *urb)
243{
Ming Leicdc97792008-02-24 18:41:47 +0800244 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200245 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800246 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200247
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700248 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200249 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
250 usb_rcvbulkpipe(port->serial->dev,
251 port->bulk_in_endpointAddress),
252 port->read_urb->transfer_buffer, 256,
253 iuu_update_status_callback, port);
254 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
255}
256
257static int iuu_status(struct usb_serial_port *port)
258{
259 int result;
260
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200261 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
262 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
263 usb_sndbulkpipe(port->serial->dev,
264 port->bulk_out_endpointAddress),
265 port->write_urb->transfer_buffer, 1,
266 iuu_status_callback, port);
267 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
268 return result;
269
270}
271
272static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
273{
274 int status;
275 struct usb_serial *serial = port->serial;
276 int actual = 0;
277
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200278 /* send the data out the bulk port */
279
280 status =
281 usb_bulk_msg(serial->dev,
282 usb_sndbulkpipe(serial->dev,
283 port->bulk_out_endpointAddress), buf,
Johan Hovold6c13ff62013-05-27 14:44:42 +0200284 count, &actual, 1000);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200285
Alan Cox9e8e2d22008-07-22 11:12:59 +0100286 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700287 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100288 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700289 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200290 return status;
291}
292
293static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
294{
295 int status;
296 struct usb_serial *serial = port->serial;
297 int actual = 0;
298
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200299 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200300 status =
301 usb_bulk_msg(serial->dev,
302 usb_rcvbulkpipe(serial->dev,
303 port->bulk_in_endpointAddress), buf,
Johan Hovold6c13ff62013-05-27 14:44:42 +0200304 count, &actual, 1000);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200305
Alan Cox9e8e2d22008-07-22 11:12:59 +0100306 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700307 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100308 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700309 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200310 return status;
311}
312
313static int iuu_led(struct usb_serial_port *port, unsigned int R,
314 unsigned int G, unsigned int B, u8 f)
315{
316 int status;
317 u8 *buf;
318 buf = kmalloc(8, GFP_KERNEL);
319 if (!buf)
320 return -ENOMEM;
321
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200322 buf[0] = IUU_SET_LED;
323 buf[1] = R & 0xFF;
324 buf[2] = (R >> 8) & 0xFF;
325 buf[3] = G & 0xFF;
326 buf[4] = (G >> 8) & 0xFF;
327 buf[5] = B & 0xFF;
328 buf[6] = (B >> 8) & 0xFF;
329 buf[7] = f;
330 status = bulk_immediate(port, buf, 8);
331 kfree(buf);
332 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700333 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200334 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700335 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200336 return IUU_OPERATION_OK;
337}
338
339static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
340 u8 b2, u8 freq)
341{
342 *buf++ = IUU_SET_LED;
343 *buf++ = r1;
344 *buf++ = r2;
345 *buf++ = g1;
346 *buf++ = g2;
347 *buf++ = b1;
348 *buf++ = b2;
349 *buf = freq;
350}
351
352static void iuu_led_activity_on(struct urb *urb)
353{
Ming Leicdc97792008-02-24 18:41:47 +0800354 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200355 int result;
356 char *buf_ptr = port->write_urb->transfer_buffer;
357 *buf_ptr++ = IUU_SET_LED;
Mathieu OTHACEHEce9d8562016-02-04 19:01:29 +0100358 if (xmas) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200359 get_random_bytes(buf_ptr, 6);
360 *(buf_ptr+7) = 1;
361 } else {
362 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
363 }
364
365 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
366 usb_sndbulkpipe(port->serial->dev,
367 port->bulk_out_endpointAddress),
368 port->write_urb->transfer_buffer, 8 ,
369 iuu_rxcmd, port);
370 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
371}
372
373static void iuu_led_activity_off(struct urb *urb)
374{
Ming Leicdc97792008-02-24 18:41:47 +0800375 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200376 int result;
377 char *buf_ptr = port->write_urb->transfer_buffer;
Mathieu OTHACEHEce9d8562016-02-04 19:01:29 +0100378 if (xmas) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200379 iuu_rxcmd(urb);
380 return;
381 } else {
382 *buf_ptr++ = IUU_SET_LED;
383 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
384 }
385 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
386 usb_sndbulkpipe(port->serial->dev,
387 port->bulk_out_endpointAddress),
388 port->write_urb->transfer_buffer, 8 ,
389 iuu_rxcmd, port);
390 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
391}
392
393
394
395static int iuu_clk(struct usb_serial_port *port, int dwFrq)
396{
397 int status;
398 struct iuu_private *priv = usb_get_serial_port_data(port);
399 int Count = 0;
400 u8 FrqGenAdr = 0x69;
401 u8 DIV = 0; /* 8bit */
402 u8 XDRV = 0; /* 8bit */
403 u8 PUMP = 0; /* 3bit */
404 u8 PBmsb = 0; /* 2bit */
405 u8 PBlsb = 0; /* 8bit */
406 u8 PO = 0; /* 1bit */
407 u8 Q = 0; /* 7bit */
408 /* 24bit = 3bytes */
409 unsigned int P = 0;
410 unsigned int P2 = 0;
411 int frq = (int)dwFrq;
412
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200413 if (frq == 0) {
414 priv->buf[Count++] = IUU_UART_WRITE_I2C;
415 priv->buf[Count++] = FrqGenAdr << 1;
416 priv->buf[Count++] = 0x09;
417 priv->buf[Count++] = 0x00;
418
419 status = bulk_immediate(port, (u8 *) priv->buf, Count);
420 if (status != 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700421 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200422 return status;
423 }
424 } else if (frq == 3579000) {
425 DIV = 100;
426 P = 1193;
427 Q = 40;
428 XDRV = 0;
429 } else if (frq == 3680000) {
430 DIV = 105;
431 P = 161;
432 Q = 5;
433 XDRV = 0;
434 } else if (frq == 6000000) {
435 DIV = 66;
436 P = 66;
437 Q = 2;
438 XDRV = 0x28;
439 } else {
440 unsigned int result = 0;
441 unsigned int tmp = 0;
442 unsigned int check;
443 unsigned int check2;
444 char found = 0x00;
445 unsigned int lQ = 2;
446 unsigned int lP = 2055;
447 unsigned int lDiv = 4;
448
449 for (lQ = 2; lQ <= 47 && !found; lQ++)
450 for (lP = 2055; lP >= 8 && !found; lP--)
451 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
452 tmp = (12000000 / lDiv) * (lP / lQ);
453 if (abs((int)(tmp - frq)) <
454 abs((int)(frq - result))) {
455 check2 = (12000000 / lQ);
456 if (check2 < 250000)
457 continue;
458 check = (12000000 / lQ) * lP;
459 if (check > 400000000)
460 continue;
461 if (check < 100000000)
462 continue;
463 if (lDiv < 4 || lDiv > 127)
464 continue;
465 result = tmp;
466 P = lP;
467 DIV = lDiv;
468 Q = lQ;
469 if (result == frq)
470 found = 0x01;
471 }
472 }
473 }
474 P2 = ((P - PO) / 2) - 4;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200475 PUMP = 0x04;
476 PBmsb = (P2 >> 8 & 0x03);
477 PBlsb = P2 & 0xFF;
478 PO = (P >> 10) & 0x01;
479 Q = Q - 2;
480
481 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
482 priv->buf[Count++] = FrqGenAdr << 1;
483 priv->buf[Count++] = 0x09;
484 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
485 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
486 priv->buf[Count++] = FrqGenAdr << 1;
487 priv->buf[Count++] = 0x0C;
488 priv->buf[Count++] = DIV; /* Adr = 0x0C */
489 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
490 priv->buf[Count++] = FrqGenAdr << 1;
491 priv->buf[Count++] = 0x12;
492 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
493 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
494 priv->buf[Count++] = FrqGenAdr << 1;
495 priv->buf[Count++] = 0x13;
496 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
497 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
498 priv->buf[Count++] = FrqGenAdr << 1;
499 priv->buf[Count++] = 0x40;
500 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
501 (PBmsb & 0x03); /* Adr = 0x40 */
502 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
503 priv->buf[Count++] = FrqGenAdr << 1;
504 priv->buf[Count++] = 0x41;
505 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
506 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
507 priv->buf[Count++] = FrqGenAdr << 1;
508 priv->buf[Count++] = 0x42;
509 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
510 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
511 priv->buf[Count++] = FrqGenAdr << 1;
512 priv->buf[Count++] = 0x44;
513 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
514 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
515 priv->buf[Count++] = FrqGenAdr << 1;
516 priv->buf[Count++] = 0x45;
517 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
518 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
519 priv->buf[Count++] = FrqGenAdr << 1;
520 priv->buf[Count++] = 0x46;
521 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
522 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
523 priv->buf[Count++] = FrqGenAdr << 1;
524 priv->buf[Count++] = 0x47;
525 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
526
527 status = bulk_immediate(port, (u8 *) priv->buf, Count);
528 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700529 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200530 return status;
531}
532
533static int iuu_uart_flush(struct usb_serial_port *port)
534{
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700535 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200536 int i;
537 int status;
538 u8 rxcmd = IUU_UART_RX;
539 struct iuu_private *priv = usb_get_serial_port_data(port);
540
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200541 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
542 return -EIO;
543
544 for (i = 0; i < 2; i++) {
545 status = bulk_immediate(port, &rxcmd, 1);
546 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700547 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200548 return status;
549 }
550
551 status = read_immediate(port, &priv->len, 1);
552 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700553 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200554 return status;
555 }
556
557 if (priv->len > 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700558 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200559 status = read_immediate(port, priv->buf, priv->len);
560 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700561 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200562 return status;
563 }
564 }
565 }
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700566 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200567 iuu_led(port, 0, 0xF000, 0, 0xFF);
568 return status;
569}
570
571static void read_buf_callback(struct urb *urb)
572{
Ming Leicdc97792008-02-24 18:41:47 +0800573 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200574 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800575 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200576
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800577 if (status) {
578 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200579 /* reschedule needed */
580 }
581 return;
582 }
583
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700584 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
Johan Hovold7aac5e72017-04-03 11:57:14 +0200585
586 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100587 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100588 tty_flip_buffer_push(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200589 }
590 iuu_led_activity_on(urb);
591}
592
593static int iuu_bulk_write(struct usb_serial_port *port)
594{
595 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700596 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200597 int result;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100598 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200599 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200600
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100601 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200602 *buf_ptr++ = IUU_UART_ESC;
603 *buf_ptr++ = IUU_UART_TX;
604 *buf_ptr++ = priv->writelen;
605
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100606 memcpy(buf_ptr, priv->writebuf, priv->writelen);
607 buf_len = priv->writelen;
608 priv->writelen = 0;
609 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700610 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
611 buf_len, buf_len, buf_ptr);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200612 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
613 usb_sndbulkpipe(port->serial->dev,
614 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100615 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200616 iuu_rxcmd, port);
617 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200618 usb_serial_port_softint(port);
619 return result;
620}
621
622static int iuu_read_buf(struct usb_serial_port *port, int len)
623{
624 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200625
626 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
627 usb_rcvbulkpipe(port->serial->dev,
628 port->bulk_in_endpointAddress),
629 port->read_urb->transfer_buffer, len,
630 read_buf_callback, port);
631 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
632 return result;
633}
634
635static void iuu_uart_read_callback(struct urb *urb)
636{
Ming Leicdc97792008-02-24 18:41:47 +0800637 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200638 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700639 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800640 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200641 int error = 0;
642 int len = 0;
643 unsigned char *data = urb->transfer_buffer;
644 priv->poll++;
645
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800646 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700647 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200648 /* error stop all */
649 return;
650 }
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200651
Johan Hovold7aac5e72017-04-03 11:57:14 +0200652 if (urb->actual_length == 1)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200653 len = (int) data[0];
654
655 if (urb->actual_length > 1) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700656 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200657 urb->actual_length);
658 error = 1;
659 return;
660 }
661 /* if len > 0 call readbuf */
662
663 if (len > 0 && error == 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700664 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200666 status = iuu_read_buf(port, len);
667 return;
668 }
669 /* need to update status ? */
670 if (priv->poll > 99) {
671 status = iuu_status(port);
672 priv->poll = 0;
673 return;
674 }
675
676 /* reset waiting ? */
677
678 if (priv->reset == 1) {
679 status = iuu_reset(port, 0xC);
680 return;
681 }
682 /* Writebuf is waiting */
683 spin_lock_irqsave(&priv->lock, flags);
684 if (priv->writelen > 0) {
685 spin_unlock_irqrestore(&priv->lock, flags);
686 status = iuu_bulk_write(port);
687 return;
688 }
689 spin_unlock_irqrestore(&priv->lock, flags);
690 /* if nothing to write call again rxcmd */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700691 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200692 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200693}
694
Alan Cox95da3102008-07-22 11:09:07 +0100695static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
696 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200697{
698 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700699 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200700
701 if (count > 256)
702 return -ENOMEM;
703
704 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100705
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200706 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100707 memcpy(priv->writebuf + priv->writelen, buf, count);
708 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200709 spin_unlock_irqrestore(&priv->lock, flags);
710
Alan Cox9e8e2d22008-07-22 11:12:59 +0100711 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712}
713
714static void read_rxcmd_callback(struct urb *urb)
715{
Ming Leicdc97792008-02-24 18:41:47 +0800716 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200717 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800718 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200719
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800720 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200721 /* error stop all */
722 return;
723 }
724
725 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
726 usb_rcvbulkpipe(port->serial->dev,
727 port->bulk_in_endpointAddress),
728 port->read_urb->transfer_buffer, 256,
729 iuu_uart_read_callback, port);
730 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700731 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200732}
733
734static int iuu_uart_on(struct usb_serial_port *port)
735{
736 int status;
737 u8 *buf;
738
Kees Cook6da2ec52018-06-12 13:55:00 -0700739 buf = kmalloc(4, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200740
741 if (!buf)
742 return -ENOMEM;
743
744 buf[0] = IUU_UART_ENABLE;
745 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
746 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100747 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200748
749 status = bulk_immediate(port, buf, 4);
750 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700751 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200752 goto uart_enable_failed;
753 }
754 /* iuu_reset() the card after iuu_uart_on() */
755 status = iuu_uart_flush(port);
756 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700757 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200758uart_enable_failed:
759 kfree(buf);
760 return status;
761}
762
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530763/* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200764static int iuu_uart_off(struct usb_serial_port *port)
765{
766 int status;
767 u8 *buf;
768 buf = kmalloc(1, GFP_KERNEL);
769 if (!buf)
770 return -ENOMEM;
771 buf[0] = IUU_UART_DISABLE;
772
773 status = bulk_immediate(port, buf, 1);
774 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700775 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200776
777 kfree(buf);
778 return status;
779}
780
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100781static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200782 u32 *actual, u8 parity)
783{
784 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100785 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200786 u8 *dataout;
787 u8 DataCount = 0;
788 u8 T1Frekvens = 0;
789 u8 T1reload = 0;
790 unsigned int T1FrekvensHZ = 0;
791
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700792 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
Kees Cook6da2ec52018-06-12 13:55:00 -0700793 dataout = kmalloc(5, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200794
795 if (!dataout)
796 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100797 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
798 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200799
800 if (baud < 1200 || baud > 230400) {
801 kfree(dataout);
802 return IUU_INVALID_PARAMETER;
803 }
804 if (baud > 977) {
805 T1Frekvens = 3;
806 T1FrekvensHZ = 500000;
807 }
808
809 if (baud > 3906) {
810 T1Frekvens = 2;
811 T1FrekvensHZ = 2000000;
812 }
813
814 if (baud > 11718) {
815 T1Frekvens = 1;
816 T1FrekvensHZ = 6000000;
817 }
818
819 if (baud > 46875) {
820 T1Frekvens = 0;
821 T1FrekvensHZ = 24000000;
822 }
823
824 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
825
826 /* magic number here: ENTER_FIRMWARE_UPDATE; */
827 dataout[DataCount++] = IUU_UART_ESC;
828 /* magic number here: CHANGE_BAUD; */
829 dataout[DataCount++] = IUU_UART_CHANGE;
830 dataout[DataCount++] = T1Frekvens;
831 dataout[DataCount++] = T1reload;
832
833 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
834
835 switch (parity & 0x0F) {
836 case IUU_PARITY_NONE:
837 dataout[DataCount++] = 0x00;
838 break;
839 case IUU_PARITY_EVEN:
840 dataout[DataCount++] = 0x01;
841 break;
842 case IUU_PARITY_ODD:
843 dataout[DataCount++] = 0x02;
844 break;
845 case IUU_PARITY_MARK:
846 dataout[DataCount++] = 0x03;
847 break;
848 case IUU_PARITY_SPACE:
849 dataout[DataCount++] = 0x04;
850 break;
851 default:
852 kfree(dataout);
853 return IUU_INVALID_PARAMETER;
854 break;
855 }
856
857 switch (parity & 0xF0) {
858 case IUU_ONE_STOP_BIT:
859 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
860 break;
861
862 case IUU_TWO_STOP_BITS:
863 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
864 break;
865 default:
866 kfree(dataout);
867 return IUU_INVALID_PARAMETER;
868 break;
869 }
870
871 status = bulk_immediate(port, dataout, DataCount);
872 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700873 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200874 kfree(dataout);
875 return status;
876}
877
Olivier Bornet96dab772009-06-11 12:54:20 +0100878static void iuu_set_termios(struct tty_struct *tty,
879 struct usb_serial_port *port, struct ktermios *old_termios)
880{
881 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100882 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100883 unsigned int cflag = tty->termios.c_cflag;
Olivier Bornet96dab772009-06-11 12:54:20 +0100884 int status;
885 u32 actual;
886 u32 parity;
887 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100888 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100889 u32 newval = cflag & supported_mask;
890
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100891 /* Just use the ospeed. ispeed should be the same. */
Alan Coxadc8d742012-07-14 15:31:47 +0100892 baud = tty->termios.c_ospeed;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100893
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700894 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100895
Olivier Bornet96dab772009-06-11 12:54:20 +0100896 /* compute the parity parameter */
897 parity = 0;
898 if (cflag & CMSPAR) { /* Using mark space */
899 if (cflag & PARODD)
900 parity |= IUU_PARITY_SPACE;
901 else
902 parity |= IUU_PARITY_MARK;
903 } else if (!(cflag & PARENB)) {
904 parity |= IUU_PARITY_NONE;
905 csize = CS8;
906 } else if (cflag & PARODD)
907 parity |= IUU_PARITY_ODD;
908 else
909 parity |= IUU_PARITY_EVEN;
910
911 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
912
913 /* set it */
914 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100915 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100916 &actual, parity);
917
918 /* set the termios value to the real one, so the user now what has
919 * changed. We support few fields so its easies to copy the old hw
920 * settings back over and then adjust them
921 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100922 if (old_termios)
Alan Coxadc8d742012-07-14 15:31:47 +0100923 tty_termios_copy_hw(&tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100924 if (status != 0) /* Set failed - return old bits */
925 return;
926 /* Re-encode speed, parity and csize */
927 tty_encode_baud_rate(tty, baud, baud);
Alan Coxadc8d742012-07-14 15:31:47 +0100928 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
929 tty->termios.c_cflag |= newval | csize;
Olivier Bornet96dab772009-06-11 12:54:20 +0100930}
931
Alan Cox335f8512009-06-11 12:26:29 +0100932static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200933{
934 /* iuu_led (port,255,0,0,0); */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200935
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200936 iuu_uart_off(port);
Johan Hovold853127f2013-03-21 12:36:31 +0100937
938 usb_kill_urb(port->write_urb);
939 usb_kill_urb(port->read_urb);
940
941 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200942}
943
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700944static void iuu_init_termios(struct tty_struct *tty)
945{
Alan Coxadc8d742012-07-14 15:31:47 +0100946 tty->termios = tty_std_termios;
947 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700948 | TIOCM_CTS | CSTOPB | PARENB;
Alan Coxadc8d742012-07-14 15:31:47 +0100949 tty->termios.c_ispeed = 9600;
950 tty->termios.c_ospeed = 9600;
951 tty->termios.c_lflag = 0;
952 tty->termios.c_oflag = 0;
953 tty->termios.c_iflag = 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700954}
955
Alan Coxa509a7e2009-09-19 13:13:26 -0700956static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200957{
958 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700959 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200960 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100961 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200962 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200963 struct iuu_private *priv = usb_get_serial_port_data(port);
964
Alan Coxadc8d742012-07-14 15:31:47 +0100965 baud = tty->termios.c_ospeed;
966 tty->termios.c_ispeed = baud;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100967 /* Re-encode speed */
968 tty_encode_baud_rate(tty, baud, baud);
969
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700970 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200971 usb_clear_halt(serial->dev, port->write_urb->pipe);
972 usb_clear_halt(serial->dev, port->read_urb->pipe);
973
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700974 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200975
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200976#define SOUP(a, b, c, d) do { \
977 result = usb_control_msg(port->serial->dev, \
978 usb_sndctrlpipe(port->serial->dev, 0), \
979 b, a, c, d, NULL, 0, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700980 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200981
982 /* This is not UART related but IUU USB driver related or something */
983 /* like that. Basically no IUU will accept any commands from the USB */
984 /* host unless it has received the following message */
985 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
986
987 SOUP(0x03, 0x02, 0x02, 0x0);
Johan Hovold750acdd2017-01-12 14:56:15 +0100988
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200989 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
990 iuu_uart_on(port);
991 if (boost < 100)
992 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100993 priv->boost = boost;
994 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200995 switch (clockmode) {
996 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100997 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200998 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
999 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001000 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001001 IUU_PARITY_EVEN);
1002 break;
1003 case 3: /* 6.00 Mhz */
1004 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001005 priv->clk = IUU_CLK_6000000;
1006 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001007 result =
1008 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1009 IUU_PARITY_EVEN);
1010 break;
1011 default: /* 3.579 Mhz */
1012 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001013 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001014 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001015 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001016 IUU_PARITY_EVEN);
1017 }
1018
1019 /* set the cardin cardout signals */
1020 switch (cdmode) {
1021 case 0:
1022 iuu_cardin = 0;
1023 iuu_cardout = 0;
1024 break;
1025 case 1:
1026 iuu_cardin = TIOCM_CD;
1027 iuu_cardout = 0;
1028 break;
1029 case 2:
1030 iuu_cardin = 0;
1031 iuu_cardout = TIOCM_CD;
1032 break;
1033 case 3:
1034 iuu_cardin = TIOCM_DSR;
1035 iuu_cardout = 0;
1036 break;
1037 case 4:
1038 iuu_cardin = 0;
1039 iuu_cardout = TIOCM_DSR;
1040 break;
1041 case 5:
1042 iuu_cardin = TIOCM_CTS;
1043 iuu_cardout = 0;
1044 break;
1045 case 6:
1046 iuu_cardin = 0;
1047 iuu_cardout = TIOCM_CTS;
1048 break;
1049 case 7:
1050 iuu_cardin = TIOCM_RNG;
1051 iuu_cardout = 0;
1052 break;
1053 case 8:
1054 iuu_cardin = 0;
1055 iuu_cardout = TIOCM_RNG;
1056 }
1057
1058 iuu_uart_flush(port);
1059
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001060 dev_dbg(dev, "%s - initialization done\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001061
1062 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1063 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1064 usb_sndbulkpipe(port->serial->dev,
1065 port->bulk_out_endpointAddress),
1066 port->write_urb->transfer_buffer, 1,
1067 read_rxcmd_callback, port);
1068 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001069 if (result) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001070 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001071 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001072 } else {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001073 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001074 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001075
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001076 return result;
1077}
1078
Olivier Bornet20eda942009-08-18 21:05:55 +02001079/* how to change VCC */
1080static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1081{
1082 int status;
1083 u8 *buf;
1084
1085 buf = kmalloc(5, GFP_KERNEL);
1086 if (!buf)
1087 return -ENOMEM;
1088
Olivier Bornet20eda942009-08-18 21:05:55 +02001089 buf[0] = IUU_SET_VCC;
1090 buf[1] = vcc & 0xFF;
1091 buf[2] = (vcc >> 8) & 0xFF;
1092 buf[3] = (vcc >> 16) & 0xFF;
1093 buf[4] = (vcc >> 24) & 0xFF;
1094
1095 status = bulk_immediate(port, buf, 5);
1096 kfree(buf);
1097
1098 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001099 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
Olivier Bornet20eda942009-08-18 21:05:55 +02001100 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001101 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
Olivier Bornet20eda942009-08-18 21:05:55 +02001102
1103 return status;
1104}
1105
1106/*
1107 * Sysfs Attributes
1108 */
1109
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07001110static ssize_t vcc_mode_show(struct device *dev,
Olivier Bornet20eda942009-08-18 21:05:55 +02001111 struct device_attribute *attr, char *buf)
1112{
1113 struct usb_serial_port *port = to_usb_serial_port(dev);
1114 struct iuu_private *priv = usb_get_serial_port_data(port);
1115
1116 return sprintf(buf, "%d\n", priv->vcc);
1117}
1118
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07001119static ssize_t vcc_mode_store(struct device *dev,
Olivier Bornet20eda942009-08-18 21:05:55 +02001120 struct device_attribute *attr, const char *buf, size_t count)
1121{
1122 struct usb_serial_port *port = to_usb_serial_port(dev);
1123 struct iuu_private *priv = usb_get_serial_port_data(port);
1124 unsigned long v;
1125
Jingoo Han487c1512012-10-29 18:37:31 +09001126 if (kstrtoul(buf, 10, &v)) {
Olivier Bornet20eda942009-08-18 21:05:55 +02001127 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1128 __func__, buf);
1129 goto fail_store_vcc_mode;
1130 }
1131
Johan Hovoldd9a38a82014-03-12 19:09:42 +01001132 dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
Olivier Bornet20eda942009-08-18 21:05:55 +02001133
1134 if ((v != 3) && (v != 5)) {
1135 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1136 } else {
1137 iuu_vcc_set(port, v);
1138 priv->vcc = v;
1139 }
1140fail_store_vcc_mode:
1141 return count;
1142}
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07001143static DEVICE_ATTR_RW(vcc_mode);
Olivier Bornet20eda942009-08-18 21:05:55 +02001144
1145static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1146{
Olivier Bornet20eda942009-08-18 21:05:55 +02001147 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1148}
1149
1150static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1151{
Olivier Bornet20eda942009-08-18 21:05:55 +02001152 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1153 return 0;
1154}
1155
1156/*
1157 * End Sysfs Attributes
1158 */
1159
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001160static struct usb_serial_driver iuu_device = {
1161 .driver = {
1162 .owner = THIS_MODULE,
1163 .name = "iuu_phoenix",
1164 },
1165 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001166 .num_ports = 1,
Johan Hovoldfb527732017-03-02 12:51:24 +01001167 .num_bulk_in = 1,
1168 .num_bulk_out = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001169 .bulk_in_size = 512,
1170 .bulk_out_size = 512,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001171 .open = iuu_open,
1172 .close = iuu_close,
1173 .write = iuu_uart_write,
1174 .read_bulk_callback = iuu_uart_read_callback,
1175 .tiocmget = iuu_tiocmget,
1176 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001177 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001178 .init_termios = iuu_init_termios,
Johan Hovold53636552012-10-17 13:34:59 +02001179 .port_probe = iuu_port_probe,
1180 .port_remove = iuu_port_remove,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001181};
1182
Alan Stern7dbe2462012-02-23 14:56:57 -05001183static struct usb_serial_driver * const serial_drivers[] = {
1184 &iuu_device, NULL
1185};
1186
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001187module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001188
1189MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1190
1191MODULE_DESCRIPTION(DRIVER_DESC);
1192MODULE_LICENSE("GPL");
1193
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001194module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001195MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001196
1197module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001198MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001199
1200module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001201MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1202 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001203
1204module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001205MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1206 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001207
1208module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1209MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1210 "for 5V). Default to 5.");