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