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