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