xref: /openbmc/linux/drivers/usb/serial/iuu_phoenix.c (revision 97e6ea6d)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Infinity Unlimited USB Phoenix driver
4  *
5  * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
6 
7  * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
8  *
9  * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
10  *
11  *  And tested with help of WB Electronics
12  */
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #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 
29 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
30 
31 static const struct usb_device_id id_table[] = {
32 	{USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
33 	{}			/* Terminating entry */
34 };
35 MODULE_DEVICE_TABLE(usb, id_table);
36 
37 /* turbo parameter */
38 static int boost = 100;
39 static int clockmode = 1;
40 static int cdmode = 1;
41 static int iuu_cardin;
42 static int iuu_cardout;
43 static bool xmas;
44 static int vcc_default = 5;
45 
46 static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
47 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
48 static void read_rxcmd_callback(struct urb *urb);
49 
50 struct iuu_private {
51 	spinlock_t lock;	/* store irq state */
52 	u8 line_status;
53 	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 */
59 	u8 len;
60 	int vcc;		/* vcc (either 3 or 5 V) */
61 	u32 boost;
62 	u32 clk;
63 };
64 
65 static int iuu_port_probe(struct usb_serial_port *port)
66 {
67 	struct iuu_private *priv;
68 	int ret;
69 
70 	priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
71 	if (!priv)
72 		return -ENOMEM;
73 
74 	priv->buf = kzalloc(256, GFP_KERNEL);
75 	if (!priv->buf) {
76 		kfree(priv);
77 		return -ENOMEM;
78 	}
79 
80 	priv->writebuf = kzalloc(256, GFP_KERNEL);
81 	if (!priv->writebuf) {
82 		kfree(priv->buf);
83 		kfree(priv);
84 		return -ENOMEM;
85 	}
86 
87 	priv->vcc = vcc_default;
88 	spin_lock_init(&priv->lock);
89 
90 	usb_set_serial_port_data(port, priv);
91 
92 	ret = iuu_create_sysfs_attrs(port);
93 	if (ret) {
94 		kfree(priv->writebuf);
95 		kfree(priv->buf);
96 		kfree(priv);
97 		return ret;
98 	}
99 
100 	return 0;
101 }
102 
103 static void iuu_port_remove(struct usb_serial_port *port)
104 {
105 	struct iuu_private *priv = usb_get_serial_port_data(port);
106 
107 	iuu_remove_sysfs_attrs(port);
108 	kfree(priv->writebuf);
109 	kfree(priv->buf);
110 	kfree(priv);
111 }
112 
113 static int iuu_tiocmset(struct tty_struct *tty,
114 			unsigned int set, unsigned int clear)
115 {
116 	struct usb_serial_port *port = tty->driver_data;
117 	struct iuu_private *priv = usb_get_serial_port_data(port);
118 	unsigned long flags;
119 
120 	/* FIXME: locking on tiomstatus */
121 	dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
122 		__func__, set, clear);
123 
124 	spin_lock_irqsave(&priv->lock, flags);
125 
126 	if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
127 		dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
128 		priv->reset = 1;
129 	}
130 	if (set & TIOCM_RTS)
131 		priv->tiostatus = TIOCM_RTS;
132 
133 	spin_unlock_irqrestore(&priv->lock, flags);
134 	return 0;
135 }
136 
137 /* This is used to provide a carrier detect mechanism
138  * When a card is present, the response is 0x00
139  * When no card , the reader respond with TIOCM_CD
140  * This is known as CD autodetect mechanism
141  */
142 static int iuu_tiocmget(struct tty_struct *tty)
143 {
144 	struct usb_serial_port *port = tty->driver_data;
145 	struct iuu_private *priv = usb_get_serial_port_data(port);
146 	unsigned long flags;
147 	int rc;
148 
149 	spin_lock_irqsave(&priv->lock, flags);
150 	rc = priv->tiostatus;
151 	spin_unlock_irqrestore(&priv->lock, flags);
152 
153 	return rc;
154 }
155 
156 static void iuu_rxcmd(struct urb *urb)
157 {
158 	struct usb_serial_port *port = urb->context;
159 	int status = urb->status;
160 
161 	if (status) {
162 		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
163 		/* error stop all */
164 		return;
165 	}
166 
167 
168 	memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
169 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
170 			  usb_sndbulkpipe(port->serial->dev,
171 					  port->bulk_out_endpointAddress),
172 			  port->write_urb->transfer_buffer, 1,
173 			  read_rxcmd_callback, port);
174 	usb_submit_urb(port->write_urb, GFP_ATOMIC);
175 }
176 
177 static int iuu_reset(struct usb_serial_port *port, u8 wt)
178 {
179 	struct iuu_private *priv = usb_get_serial_port_data(port);
180 	int result;
181 	char *buf_ptr = port->write_urb->transfer_buffer;
182 
183 	/* Prepare the reset sequence */
184 
185 	*buf_ptr++ = IUU_RST_SET;
186 	*buf_ptr++ = IUU_DELAY_MS;
187 	*buf_ptr++ = wt;
188 	*buf_ptr = IUU_RST_CLEAR;
189 
190 	/* send the sequence */
191 
192 	usb_fill_bulk_urb(port->write_urb,
193 			  port->serial->dev,
194 			  usb_sndbulkpipe(port->serial->dev,
195 					  port->bulk_out_endpointAddress),
196 			  port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
197 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
198 	priv->reset = 0;
199 	return result;
200 }
201 
202 /* Status Function
203  * Return value is
204  * 0x00 = no card
205  * 0x01 = smartcard
206  * 0x02 = sim card
207  */
208 static void iuu_update_status_callback(struct urb *urb)
209 {
210 	struct usb_serial_port *port = urb->context;
211 	struct iuu_private *priv = usb_get_serial_port_data(port);
212 	u8 *st;
213 	int status = urb->status;
214 
215 	if (status) {
216 		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
217 		/* error stop all */
218 		return;
219 	}
220 
221 	st = urb->transfer_buffer;
222 	dev_dbg(&port->dev, "%s - enter\n", __func__);
223 	if (urb->actual_length == 1) {
224 		switch (st[0]) {
225 		case 0x1:
226 			priv->tiostatus = iuu_cardout;
227 			break;
228 		case 0x0:
229 			priv->tiostatus = iuu_cardin;
230 			break;
231 		default:
232 			priv->tiostatus = iuu_cardin;
233 		}
234 	}
235 	iuu_rxcmd(urb);
236 }
237 
238 static void iuu_status_callback(struct urb *urb)
239 {
240 	struct usb_serial_port *port = urb->context;
241 	int status = urb->status;
242 
243 	dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
244 	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
245 			  usb_rcvbulkpipe(port->serial->dev,
246 					  port->bulk_in_endpointAddress),
247 			  port->read_urb->transfer_buffer, 256,
248 			  iuu_update_status_callback, port);
249 	usb_submit_urb(port->read_urb, GFP_ATOMIC);
250 }
251 
252 static int iuu_status(struct usb_serial_port *port)
253 {
254 	int result;
255 
256 	memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
257 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
258 			  usb_sndbulkpipe(port->serial->dev,
259 					  port->bulk_out_endpointAddress),
260 			  port->write_urb->transfer_buffer, 1,
261 			  iuu_status_callback, port);
262 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
263 	return result;
264 
265 }
266 
267 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
268 {
269 	int status;
270 	struct usb_serial *serial = port->serial;
271 	int actual = 0;
272 
273 	/* send the data out the bulk port */
274 
275 	status =
276 	    usb_bulk_msg(serial->dev,
277 			 usb_sndbulkpipe(serial->dev,
278 					 port->bulk_out_endpointAddress), buf,
279 			 count, &actual, 1000);
280 
281 	if (status != IUU_OPERATION_OK)
282 		dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
283 	else
284 		dev_dbg(&port->dev, "%s - write OK !\n", __func__);
285 	return status;
286 }
287 
288 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
289 {
290 	int status;
291 	struct usb_serial *serial = port->serial;
292 	int actual = 0;
293 
294 	/* send the data out the bulk port */
295 	status =
296 	    usb_bulk_msg(serial->dev,
297 			 usb_rcvbulkpipe(serial->dev,
298 					 port->bulk_in_endpointAddress), buf,
299 			 count, &actual, 1000);
300 
301 	if (status != IUU_OPERATION_OK)
302 		dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
303 	else
304 		dev_dbg(&port->dev, "%s - read OK !\n", __func__);
305 	return status;
306 }
307 
308 static int iuu_led(struct usb_serial_port *port, unsigned int R,
309 		   unsigned int G, unsigned int B, u8 f)
310 {
311 	int status;
312 	u8 *buf;
313 	buf = kmalloc(8, GFP_KERNEL);
314 	if (!buf)
315 		return -ENOMEM;
316 
317 	buf[0] = IUU_SET_LED;
318 	buf[1] = R & 0xFF;
319 	buf[2] = (R >> 8) & 0xFF;
320 	buf[3] = G & 0xFF;
321 	buf[4] = (G >> 8) & 0xFF;
322 	buf[5] = B & 0xFF;
323 	buf[6] = (B >> 8) & 0xFF;
324 	buf[7] = f;
325 	status = bulk_immediate(port, buf, 8);
326 	kfree(buf);
327 	if (status != IUU_OPERATION_OK)
328 		dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
329 	else
330 		dev_dbg(&port->dev, "%s - led OK !\n", __func__);
331 	return IUU_OPERATION_OK;
332 }
333 
334 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
335 				 u8 b2, u8 freq)
336 {
337 	*buf++ = IUU_SET_LED;
338 	*buf++ = r1;
339 	*buf++ = r2;
340 	*buf++ = g1;
341 	*buf++ = g2;
342 	*buf++ = b1;
343 	*buf++ = b2;
344 	*buf = freq;
345 }
346 
347 static void iuu_led_activity_on(struct urb *urb)
348 {
349 	struct usb_serial_port *port = urb->context;
350 	char *buf_ptr = port->write_urb->transfer_buffer;
351 
352 	if (xmas) {
353 		buf_ptr[0] = IUU_SET_LED;
354 		get_random_bytes(buf_ptr + 1, 6);
355 		buf_ptr[7] = 1;
356 	} else {
357 		iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
358 	}
359 
360 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
361 			  usb_sndbulkpipe(port->serial->dev,
362 					  port->bulk_out_endpointAddress),
363 			  port->write_urb->transfer_buffer, 8 ,
364 			  iuu_rxcmd, port);
365 	usb_submit_urb(port->write_urb, GFP_ATOMIC);
366 }
367 
368 static void iuu_led_activity_off(struct urb *urb)
369 {
370 	struct usb_serial_port *port = urb->context;
371 	char *buf_ptr = port->write_urb->transfer_buffer;
372 
373 	if (xmas) {
374 		iuu_rxcmd(urb);
375 		return;
376 	}
377 
378 	iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
379 
380 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
381 			  usb_sndbulkpipe(port->serial->dev,
382 					  port->bulk_out_endpointAddress),
383 			  port->write_urb->transfer_buffer, 8 ,
384 			  iuu_rxcmd, port);
385 	usb_submit_urb(port->write_urb, GFP_ATOMIC);
386 }
387 
388 
389 
390 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
391 {
392 	int status;
393 	struct iuu_private *priv = usb_get_serial_port_data(port);
394 	int Count = 0;
395 	u8 FrqGenAdr = 0x69;
396 	u8 DIV = 0;		/* 8bit */
397 	u8 XDRV = 0;		/* 8bit */
398 	u8 PUMP = 0;		/* 3bit */
399 	u8 PBmsb = 0;		/* 2bit */
400 	u8 PBlsb = 0;		/* 8bit */
401 	u8 PO = 0;		/* 1bit */
402 	u8 Q = 0;		/* 7bit */
403 	/* 24bit = 3bytes */
404 	unsigned int P = 0;
405 	unsigned int P2 = 0;
406 	int frq = (int)dwFrq;
407 
408 	if (frq == 0) {
409 		priv->buf[Count++] = IUU_UART_WRITE_I2C;
410 		priv->buf[Count++] = FrqGenAdr << 1;
411 		priv->buf[Count++] = 0x09;
412 		priv->buf[Count++] = 0x00;
413 
414 		status = bulk_immediate(port, (u8 *) priv->buf, Count);
415 		if (status != 0) {
416 			dev_dbg(&port->dev, "%s - write error\n", __func__);
417 			return status;
418 		}
419 	} else if (frq == 3579000) {
420 		DIV = 100;
421 		P = 1193;
422 		Q = 40;
423 		XDRV = 0;
424 	} else if (frq == 3680000) {
425 		DIV = 105;
426 		P = 161;
427 		Q = 5;
428 		XDRV = 0;
429 	} else if (frq == 6000000) {
430 		DIV = 66;
431 		P = 66;
432 		Q = 2;
433 		XDRV = 0x28;
434 	} else {
435 		unsigned int result = 0;
436 		unsigned int tmp = 0;
437 		unsigned int check;
438 		unsigned int check2;
439 		char found = 0x00;
440 		unsigned int lQ = 2;
441 		unsigned int lP = 2055;
442 		unsigned int lDiv = 4;
443 
444 		for (lQ = 2; lQ <= 47 && !found; lQ++)
445 			for (lP = 2055; lP >= 8 && !found; lP--)
446 				for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
447 					tmp = (12000000 / lDiv) * (lP / lQ);
448 					if (abs((int)(tmp - frq)) <
449 					    abs((int)(frq - result))) {
450 						check2 = (12000000 / lQ);
451 						if (check2 < 250000)
452 							continue;
453 						check = (12000000 / lQ) * lP;
454 						if (check > 400000000)
455 							continue;
456 						if (check < 100000000)
457 							continue;
458 						if (lDiv < 4 || lDiv > 127)
459 							continue;
460 						result = tmp;
461 						P = lP;
462 						DIV = lDiv;
463 						Q = lQ;
464 						if (result == frq)
465 							found = 0x01;
466 					}
467 				}
468 	}
469 	P2 = ((P - PO) / 2) - 4;
470 	PUMP = 0x04;
471 	PBmsb = (P2 >> 8 & 0x03);
472 	PBlsb = P2 & 0xFF;
473 	PO = (P >> 10) & 0x01;
474 	Q = Q - 2;
475 
476 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
477 	priv->buf[Count++] = FrqGenAdr << 1;
478 	priv->buf[Count++] = 0x09;
479 	priv->buf[Count++] = 0x20;	/* Adr = 0x09 */
480 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
481 	priv->buf[Count++] = FrqGenAdr << 1;
482 	priv->buf[Count++] = 0x0C;
483 	priv->buf[Count++] = DIV;	/* Adr = 0x0C */
484 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/* 0x4C */
485 	priv->buf[Count++] = FrqGenAdr << 1;
486 	priv->buf[Count++] = 0x12;
487 	priv->buf[Count++] = XDRV;	/* Adr = 0x12 */
488 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
489 	priv->buf[Count++] = FrqGenAdr << 1;
490 	priv->buf[Count++] = 0x13;
491 	priv->buf[Count++] = 0x6B;	/* Adr = 0x13 */
492 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
493 	priv->buf[Count++] = FrqGenAdr << 1;
494 	priv->buf[Count++] = 0x40;
495 	priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
496 			     (PBmsb & 0x03);	/* Adr = 0x40 */
497 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
498 	priv->buf[Count++] = FrqGenAdr << 1;
499 	priv->buf[Count++] = 0x41;
500 	priv->buf[Count++] = PBlsb;	/* Adr = 0x41 */
501 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
502 	priv->buf[Count++] = FrqGenAdr << 1;
503 	priv->buf[Count++] = 0x42;
504 	priv->buf[Count++] = Q | (((PO & 0x01) << 7));	/* Adr = 0x42 */
505 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
506 	priv->buf[Count++] = FrqGenAdr << 1;
507 	priv->buf[Count++] = 0x44;
508 	priv->buf[Count++] = (char)0xFF;	/* Adr = 0x44 */
509 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
510 	priv->buf[Count++] = FrqGenAdr << 1;
511 	priv->buf[Count++] = 0x45;
512 	priv->buf[Count++] = (char)0xFE;	/* Adr = 0x45 */
513 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
514 	priv->buf[Count++] = FrqGenAdr << 1;
515 	priv->buf[Count++] = 0x46;
516 	priv->buf[Count++] = 0x7F;	/* Adr = 0x46 */
517 	priv->buf[Count++] = IUU_UART_WRITE_I2C;	/*  0x4C */
518 	priv->buf[Count++] = FrqGenAdr << 1;
519 	priv->buf[Count++] = 0x47;
520 	priv->buf[Count++] = (char)0x84;	/* Adr = 0x47 */
521 
522 	status = bulk_immediate(port, (u8 *) priv->buf, Count);
523 	if (status != IUU_OPERATION_OK)
524 		dev_dbg(&port->dev, "%s - write error\n", __func__);
525 	return status;
526 }
527 
528 static int iuu_uart_flush(struct usb_serial_port *port)
529 {
530 	struct device *dev = &port->dev;
531 	int i;
532 	int status;
533 	u8 *rxcmd;
534 	struct iuu_private *priv = usb_get_serial_port_data(port);
535 
536 	if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
537 		return -EIO;
538 
539 	rxcmd = kmalloc(1, GFP_KERNEL);
540 	if (!rxcmd)
541 		return -ENOMEM;
542 
543 	rxcmd[0] = IUU_UART_RX;
544 
545 	for (i = 0; i < 2; i++) {
546 		status = bulk_immediate(port, rxcmd, 1);
547 		if (status != IUU_OPERATION_OK) {
548 			dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
549 			goto out_free;
550 		}
551 
552 		status = read_immediate(port, &priv->len, 1);
553 		if (status != IUU_OPERATION_OK) {
554 			dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
555 			goto out_free;
556 		}
557 
558 		if (priv->len > 0) {
559 			dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
560 			status = read_immediate(port, priv->buf, priv->len);
561 			if (status != IUU_OPERATION_OK) {
562 				dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
563 				goto out_free;
564 			}
565 		}
566 	}
567 	dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
568 	iuu_led(port, 0, 0xF000, 0, 0xFF);
569 
570 out_free:
571 	kfree(rxcmd);
572 
573 	return status;
574 }
575 
576 static void read_buf_callback(struct urb *urb)
577 {
578 	struct usb_serial_port *port = urb->context;
579 	unsigned char *data = urb->transfer_buffer;
580 	int status = urb->status;
581 
582 	if (status) {
583 		if (status == -EPROTO) {
584 			/* reschedule needed */
585 		}
586 		return;
587 	}
588 
589 	dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
590 
591 	if (urb->actual_length) {
592 		tty_insert_flip_string(&port->port, data, urb->actual_length);
593 		tty_flip_buffer_push(&port->port);
594 	}
595 	iuu_led_activity_on(urb);
596 }
597 
598 static int iuu_bulk_write(struct usb_serial_port *port)
599 {
600 	struct iuu_private *priv = usb_get_serial_port_data(port);
601 	unsigned long flags;
602 	int result;
603 	int buf_len;
604 	char *buf_ptr = port->write_urb->transfer_buffer;
605 
606 	spin_lock_irqsave(&priv->lock, flags);
607 	*buf_ptr++ = IUU_UART_ESC;
608 	*buf_ptr++ = IUU_UART_TX;
609 	*buf_ptr++ = priv->writelen;
610 
611 	memcpy(buf_ptr, priv->writebuf, priv->writelen);
612 	buf_len = priv->writelen;
613 	priv->writelen = 0;
614 	spin_unlock_irqrestore(&priv->lock, flags);
615 	dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
616 		buf_len, buf_len, buf_ptr);
617 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
618 			  usb_sndbulkpipe(port->serial->dev,
619 					  port->bulk_out_endpointAddress),
620 			  port->write_urb->transfer_buffer, buf_len + 3,
621 			  iuu_rxcmd, port);
622 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
623 	usb_serial_port_softint(port);
624 	return result;
625 }
626 
627 static int iuu_read_buf(struct usb_serial_port *port, int len)
628 {
629 	int result;
630 
631 	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
632 			  usb_rcvbulkpipe(port->serial->dev,
633 					  port->bulk_in_endpointAddress),
634 			  port->read_urb->transfer_buffer, len,
635 			  read_buf_callback, port);
636 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
637 	return result;
638 }
639 
640 static void iuu_uart_read_callback(struct urb *urb)
641 {
642 	struct usb_serial_port *port = urb->context;
643 	struct iuu_private *priv = usb_get_serial_port_data(port);
644 	unsigned long flags;
645 	int status = urb->status;
646 	int len = 0;
647 	unsigned char *data = urb->transfer_buffer;
648 	priv->poll++;
649 
650 	if (status) {
651 		dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
652 		/* error stop all */
653 		return;
654 	}
655 
656 	if (urb->actual_length == 1)
657 		len = (int) data[0];
658 
659 	if (urb->actual_length > 1) {
660 		dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
661 		    urb->actual_length);
662 		return;
663 	}
664 	/* if len > 0 call readbuf */
665 
666 	if (len > 0) {
667 		dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
668 			__func__, len);
669 		status = iuu_read_buf(port, len);
670 		return;
671 	}
672 	/* need to update status  ? */
673 	if (priv->poll > 99) {
674 		status = iuu_status(port);
675 		priv->poll = 0;
676 		return;
677 	}
678 
679 	/* reset waiting ? */
680 
681 	if (priv->reset == 1) {
682 		status = iuu_reset(port, 0xC);
683 		return;
684 	}
685 	/* Writebuf is waiting */
686 	spin_lock_irqsave(&priv->lock, flags);
687 	if (priv->writelen > 0) {
688 		spin_unlock_irqrestore(&priv->lock, flags);
689 		status = iuu_bulk_write(port);
690 		return;
691 	}
692 	spin_unlock_irqrestore(&priv->lock, flags);
693 	/* if nothing to write call again rxcmd */
694 	dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
695 	iuu_led_activity_off(urb);
696 }
697 
698 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
699 			  const u8 *buf, int count)
700 {
701 	struct iuu_private *priv = usb_get_serial_port_data(port);
702 	unsigned long flags;
703 
704 	spin_lock_irqsave(&priv->lock, flags);
705 
706 	count = min(count, 256 - priv->writelen);
707 	if (count == 0)
708 		goto out;
709 
710 	/* fill the buffer */
711 	memcpy(priv->writebuf + priv->writelen, buf, count);
712 	priv->writelen += count;
713 out:
714 	spin_unlock_irqrestore(&priv->lock, flags);
715 
716 	return count;
717 }
718 
719 static void read_rxcmd_callback(struct urb *urb)
720 {
721 	struct usb_serial_port *port = urb->context;
722 	int result;
723 	int status = urb->status;
724 
725 	if (status) {
726 		/* error stop all */
727 		return;
728 	}
729 
730 	usb_fill_bulk_urb(port->read_urb, port->serial->dev,
731 			  usb_rcvbulkpipe(port->serial->dev,
732 					  port->bulk_in_endpointAddress),
733 			  port->read_urb->transfer_buffer, 256,
734 			  iuu_uart_read_callback, port);
735 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
736 	dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
737 }
738 
739 static int iuu_uart_on(struct usb_serial_port *port)
740 {
741 	int status;
742 	u8 *buf;
743 
744 	buf = kmalloc(4, GFP_KERNEL);
745 
746 	if (!buf)
747 		return -ENOMEM;
748 
749 	buf[0] = IUU_UART_ENABLE;
750 	buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
751 	buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
752 	buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
753 
754 	status = bulk_immediate(port, buf, 4);
755 	if (status != IUU_OPERATION_OK) {
756 		dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
757 		goto uart_enable_failed;
758 	}
759 	/*  iuu_reset() the card after iuu_uart_on() */
760 	status = iuu_uart_flush(port);
761 	if (status != IUU_OPERATION_OK)
762 		dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
763 uart_enable_failed:
764 	kfree(buf);
765 	return status;
766 }
767 
768 /*  Disables the IUU UART (a.k.a. the Phoenix voiderface) */
769 static int iuu_uart_off(struct usb_serial_port *port)
770 {
771 	int status;
772 	u8 *buf;
773 	buf = kmalloc(1, GFP_KERNEL);
774 	if (!buf)
775 		return -ENOMEM;
776 	buf[0] = IUU_UART_DISABLE;
777 
778 	status = bulk_immediate(port, buf, 1);
779 	if (status != IUU_OPERATION_OK)
780 		dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
781 
782 	kfree(buf);
783 	return status;
784 }
785 
786 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
787 			 u32 *actual, u8 parity)
788 {
789 	int status;
790 	u32 baud;
791 	u8 *dataout;
792 	u8 DataCount = 0;
793 	u8 T1Frekvens = 0;
794 	u8 T1reload = 0;
795 	unsigned int T1FrekvensHZ = 0;
796 
797 	dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
798 	dataout = kmalloc(5, GFP_KERNEL);
799 
800 	if (!dataout)
801 		return -ENOMEM;
802 	/*baud = (((priv->clk / 35) * baud_base) / 100000); */
803 	baud = baud_base;
804 
805 	if (baud < 1200 || baud > 230400) {
806 		kfree(dataout);
807 		return IUU_INVALID_PARAMETER;
808 	}
809 	if (baud > 977) {
810 		T1Frekvens = 3;
811 		T1FrekvensHZ = 500000;
812 	}
813 
814 	if (baud > 3906) {
815 		T1Frekvens = 2;
816 		T1FrekvensHZ = 2000000;
817 	}
818 
819 	if (baud > 11718) {
820 		T1Frekvens = 1;
821 		T1FrekvensHZ = 6000000;
822 	}
823 
824 	if (baud > 46875) {
825 		T1Frekvens = 0;
826 		T1FrekvensHZ = 24000000;
827 	}
828 
829 	T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
830 
831 	/*  magic number here:  ENTER_FIRMWARE_UPDATE; */
832 	dataout[DataCount++] = IUU_UART_ESC;
833 	/*  magic number here:  CHANGE_BAUD; */
834 	dataout[DataCount++] = IUU_UART_CHANGE;
835 	dataout[DataCount++] = T1Frekvens;
836 	dataout[DataCount++] = T1reload;
837 
838 	*actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
839 
840 	switch (parity & 0x0F) {
841 	case IUU_PARITY_NONE:
842 		dataout[DataCount++] = 0x00;
843 		break;
844 	case IUU_PARITY_EVEN:
845 		dataout[DataCount++] = 0x01;
846 		break;
847 	case IUU_PARITY_ODD:
848 		dataout[DataCount++] = 0x02;
849 		break;
850 	case IUU_PARITY_MARK:
851 		dataout[DataCount++] = 0x03;
852 		break;
853 	case IUU_PARITY_SPACE:
854 		dataout[DataCount++] = 0x04;
855 		break;
856 	default:
857 		kfree(dataout);
858 		return IUU_INVALID_PARAMETER;
859 	}
860 
861 	switch (parity & 0xF0) {
862 	case IUU_ONE_STOP_BIT:
863 		dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
864 		break;
865 
866 	case IUU_TWO_STOP_BITS:
867 		dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
868 		break;
869 	default:
870 		kfree(dataout);
871 		return IUU_INVALID_PARAMETER;
872 	}
873 
874 	status = bulk_immediate(port, dataout, DataCount);
875 	if (status != IUU_OPERATION_OK)
876 		dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
877 	kfree(dataout);
878 	return status;
879 }
880 
881 static void iuu_set_termios(struct tty_struct *tty,
882 		struct usb_serial_port *port, struct ktermios *old_termios)
883 {
884 	const u32 supported_mask = CMSPAR|PARENB|PARODD;
885 	struct iuu_private *priv = usb_get_serial_port_data(port);
886 	unsigned int cflag = tty->termios.c_cflag;
887 	int status;
888 	u32 actual;
889 	u32 parity;
890 	int csize = CS7;
891 	int baud;
892 	u32 newval = cflag & supported_mask;
893 
894 	/* Just use the ospeed. ispeed should be the same. */
895 	baud = tty->termios.c_ospeed;
896 
897 	dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
898 
899 	/* compute the parity parameter */
900 	parity = 0;
901 	if (cflag & CMSPAR) {	/* Using mark space */
902 		if (cflag & PARODD)
903 			parity |= IUU_PARITY_SPACE;
904 		else
905 			parity |= IUU_PARITY_MARK;
906 	} else if (!(cflag & PARENB)) {
907 		parity |= IUU_PARITY_NONE;
908 		csize = CS8;
909 	} else if (cflag & PARODD)
910 		parity |= IUU_PARITY_ODD;
911 	else
912 		parity |= IUU_PARITY_EVEN;
913 
914 	parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
915 
916 	/* set it */
917 	status = iuu_uart_baud(port,
918 			baud * priv->boost / 100,
919 			&actual, parity);
920 
921 	/* set the termios value to the real one, so the user now what has
922 	 * changed. We support few fields so its easies to copy the old hw
923 	 * settings back over and then adjust them
924 	 */
925 	if (old_termios)
926 		tty_termios_copy_hw(&tty->termios, old_termios);
927 	if (status != 0)	/* Set failed - return old bits */
928 		return;
929 	/* Re-encode speed, parity and csize */
930 	tty_encode_baud_rate(tty, baud, baud);
931 	tty->termios.c_cflag &= ~(supported_mask|CSIZE);
932 	tty->termios.c_cflag |= newval | csize;
933 }
934 
935 static void iuu_close(struct usb_serial_port *port)
936 {
937 	/* iuu_led (port,255,0,0,0); */
938 
939 	iuu_uart_off(port);
940 
941 	usb_kill_urb(port->write_urb);
942 	usb_kill_urb(port->read_urb);
943 
944 	iuu_led(port, 0, 0, 0xF000, 0xFF);
945 }
946 
947 static void iuu_init_termios(struct tty_struct *tty)
948 {
949 	tty->termios.c_cflag = B9600 | CS8 | CSTOPB | CREAD | PARENB | CLOCAL;
950 	tty->termios.c_ispeed = 9600;
951 	tty->termios.c_ospeed = 9600;
952 	tty->termios.c_lflag = 0;
953 	tty->termios.c_oflag = 0;
954 	tty->termios.c_iflag = 0;
955 }
956 
957 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
958 {
959 	struct usb_serial *serial = port->serial;
960 	struct device *dev = &port->dev;
961 	int result;
962 	int baud;
963 	u32 actual;
964 	struct iuu_private *priv = usb_get_serial_port_data(port);
965 
966 	baud = tty->termios.c_ospeed;
967 
968 	dev_dbg(dev, "%s - baud %d\n", __func__, baud);
969 	usb_clear_halt(serial->dev, port->write_urb->pipe);
970 	usb_clear_halt(serial->dev, port->read_urb->pipe);
971 
972 	priv->poll = 0;
973 
974 #define SOUP(a, b, c, d)  do { \
975 	result = usb_control_msg(port->serial->dev,	\
976 				usb_sndctrlpipe(port->serial->dev, 0),	\
977 				b, a, c, d, NULL, 0, 1000); \
978 	dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x  %d\n", a, b, c, d, result); } while (0)
979 
980 	/*  This is not UART related but IUU USB driver related or something */
981 	/*  like that. Basically no IUU will accept any commands from the USB */
982 	/*  host unless it has received the following message */
983 	/* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
984 
985 	SOUP(0x03, 0x02, 0x02, 0x0);
986 
987 	iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
988 	iuu_uart_on(port);
989 	if (boost < 100)
990 		boost = 100;
991 	priv->boost = boost;
992 	switch (clockmode) {
993 	case 2:		/*  3.680 Mhz */
994 		priv->clk = IUU_CLK_3680000;
995 		iuu_clk(port, IUU_CLK_3680000 * boost / 100);
996 		result =
997 		    iuu_uart_baud(port, baud * boost / 100, &actual,
998 				  IUU_PARITY_EVEN);
999 		break;
1000 	case 3:		/*  6.00 Mhz */
1001 		iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1002 		priv->clk = IUU_CLK_6000000;
1003 		/* Ratio of 6000000 to 3500000 for baud 9600 */
1004 		result =
1005 		    iuu_uart_baud(port, 16457 * boost / 100, &actual,
1006 				  IUU_PARITY_EVEN);
1007 		break;
1008 	default:		/*  3.579 Mhz */
1009 		iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1010 		priv->clk = IUU_CLK_3579000;
1011 		result =
1012 		    iuu_uart_baud(port, baud * boost / 100, &actual,
1013 				  IUU_PARITY_EVEN);
1014 	}
1015 
1016 	/* set the cardin cardout signals */
1017 	switch (cdmode) {
1018 	case 0:
1019 		iuu_cardin = 0;
1020 		iuu_cardout = 0;
1021 		break;
1022 	case 1:
1023 		iuu_cardin = TIOCM_CD;
1024 		iuu_cardout =  0;
1025 		break;
1026 	case 2:
1027 		iuu_cardin = 0;
1028 		iuu_cardout = TIOCM_CD;
1029 		break;
1030 	case 3:
1031 		iuu_cardin = TIOCM_DSR;
1032 		iuu_cardout = 0;
1033 		break;
1034 	case 4:
1035 		iuu_cardin = 0;
1036 		iuu_cardout = TIOCM_DSR;
1037 		break;
1038 	case 5:
1039 		iuu_cardin = TIOCM_CTS;
1040 		iuu_cardout = 0;
1041 		break;
1042 	case 6:
1043 		iuu_cardin = 0;
1044 		iuu_cardout = TIOCM_CTS;
1045 		break;
1046 	case 7:
1047 		iuu_cardin = TIOCM_RNG;
1048 		iuu_cardout = 0;
1049 		break;
1050 	case 8:
1051 		iuu_cardin = 0;
1052 		iuu_cardout = TIOCM_RNG;
1053 	}
1054 
1055 	iuu_uart_flush(port);
1056 
1057 	dev_dbg(dev, "%s - initialization done\n", __func__);
1058 
1059 	memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1060 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1061 			  usb_sndbulkpipe(port->serial->dev,
1062 					  port->bulk_out_endpointAddress),
1063 			  port->write_urb->transfer_buffer, 1,
1064 			  read_rxcmd_callback, port);
1065 	result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1066 	if (result) {
1067 		dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1068 		iuu_close(port);
1069 	} else {
1070 		dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1071 	}
1072 
1073 	return result;
1074 }
1075 
1076 /* how to change VCC */
1077 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1078 {
1079 	int status;
1080 	u8 *buf;
1081 
1082 	buf = kmalloc(5, GFP_KERNEL);
1083 	if (!buf)
1084 		return -ENOMEM;
1085 
1086 	buf[0] = IUU_SET_VCC;
1087 	buf[1] = vcc & 0xFF;
1088 	buf[2] = (vcc >> 8) & 0xFF;
1089 	buf[3] = (vcc >> 16) & 0xFF;
1090 	buf[4] = (vcc >> 24) & 0xFF;
1091 
1092 	status = bulk_immediate(port, buf, 5);
1093 	kfree(buf);
1094 
1095 	if (status != IUU_OPERATION_OK)
1096 		dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1097 	else
1098 		dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1099 
1100 	return status;
1101 }
1102 
1103 /*
1104  * Sysfs Attributes
1105  */
1106 
1107 static ssize_t vcc_mode_show(struct device *dev,
1108 	struct device_attribute *attr, char *buf)
1109 {
1110 	struct usb_serial_port *port = to_usb_serial_port(dev);
1111 	struct iuu_private *priv = usb_get_serial_port_data(port);
1112 
1113 	return sprintf(buf, "%d\n", priv->vcc);
1114 }
1115 
1116 static ssize_t vcc_mode_store(struct device *dev,
1117 	struct device_attribute *attr, const char *buf, size_t count)
1118 {
1119 	struct usb_serial_port *port = to_usb_serial_port(dev);
1120 	struct iuu_private *priv = usb_get_serial_port_data(port);
1121 	unsigned long v;
1122 
1123 	if (kstrtoul(buf, 10, &v)) {
1124 		dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1125 				__func__, buf);
1126 		goto fail_store_vcc_mode;
1127 	}
1128 
1129 	dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
1130 
1131 	if ((v != 3) && (v != 5)) {
1132 		dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1133 	} else {
1134 		iuu_vcc_set(port, v);
1135 		priv->vcc = v;
1136 	}
1137 fail_store_vcc_mode:
1138 	return count;
1139 }
1140 static DEVICE_ATTR_RW(vcc_mode);
1141 
1142 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1143 {
1144 	return device_create_file(&port->dev, &dev_attr_vcc_mode);
1145 }
1146 
1147 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1148 {
1149 	device_remove_file(&port->dev, &dev_attr_vcc_mode);
1150 	return 0;
1151 }
1152 
1153 /*
1154  * End Sysfs Attributes
1155  */
1156 
1157 static struct usb_serial_driver iuu_device = {
1158 	.driver = {
1159 		   .owner = THIS_MODULE,
1160 		   .name = "iuu_phoenix",
1161 		   },
1162 	.id_table = id_table,
1163 	.num_ports = 1,
1164 	.num_bulk_in = 1,
1165 	.num_bulk_out = 1,
1166 	.bulk_in_size = 512,
1167 	.bulk_out_size = 512,
1168 	.open = iuu_open,
1169 	.close = iuu_close,
1170 	.write = iuu_uart_write,
1171 	.read_bulk_callback = iuu_uart_read_callback,
1172 	.tiocmget = iuu_tiocmget,
1173 	.tiocmset = iuu_tiocmset,
1174 	.set_termios = iuu_set_termios,
1175 	.init_termios = iuu_init_termios,
1176 	.port_probe = iuu_port_probe,
1177 	.port_remove = iuu_port_remove,
1178 };
1179 
1180 static struct usb_serial_driver * const serial_drivers[] = {
1181 	&iuu_device, NULL
1182 };
1183 
1184 module_usb_serial_driver(serial_drivers, id_table);
1185 
1186 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1187 
1188 MODULE_DESCRIPTION(DRIVER_DESC);
1189 MODULE_LICENSE("GPL");
1190 
1191 module_param(xmas, bool, 0644);
1192 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1193 
1194 module_param(boost, int, 0644);
1195 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1196 
1197 module_param(clockmode, int, 0644);
1198 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1199 		"3=6 Mhz)");
1200 
1201 module_param(cdmode, int, 0644);
1202 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1203 		 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1204 
1205 module_param(vcc_default, int, 0644);
1206 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1207 		"for 5V). Default to 5.");
1208