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