root/drivers/usb/serial/iuu_phoenix.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. iuu_port_probe
  2. iuu_port_remove
  3. iuu_tiocmset
  4. iuu_tiocmget
  5. iuu_rxcmd
  6. iuu_reset
  7. iuu_update_status_callback
  8. iuu_status_callback
  9. iuu_status
  10. bulk_immediate
  11. read_immediate
  12. iuu_led
  13. iuu_rgbf_fill_buffer
  14. iuu_led_activity_on
  15. iuu_led_activity_off
  16. iuu_clk
  17. iuu_uart_flush
  18. read_buf_callback
  19. iuu_bulk_write
  20. iuu_read_buf
  21. iuu_uart_read_callback
  22. iuu_uart_write
  23. read_rxcmd_callback
  24. iuu_uart_on
  25. iuu_uart_off
  26. iuu_uart_baud
  27. iuu_set_termios
  28. iuu_close
  29. iuu_init_termios
  30. iuu_open
  31. iuu_vcc_set
  32. vcc_mode_show
  33. vcc_mode_store
  34. iuu_create_sysfs_attrs
  35. iuu_remove_sysfs_attrs

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

/* [<][>][^][v][top][bottom][index][help] */