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