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