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