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