1 /****************************************************************************** 2 * 3 * Driver for Option High Speed Mobile Devices. 4 * 5 * Copyright (C) 2008 Option International 6 * Filip Aben <f.aben@option.com> 7 * Denis Joseph Barrow <d.barow@option.com> 8 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd) 9 * <ajb@spheresystems.co.uk> 10 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de> 11 * Copyright (C) 2008 Novell, Inc. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License version 2 as 15 * published by the Free Software Foundation. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 25 * USA 26 * 27 * 28 *****************************************************************************/ 29 30 /****************************************************************************** 31 * 32 * Description of the device: 33 * 34 * Interface 0: Contains the IP network interface on the bulk end points. 35 * The multiplexed serial ports are using the interrupt and 36 * control endpoints. 37 * Interrupt contains a bitmap telling which multiplexed 38 * serialport needs servicing. 39 * 40 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the 41 * port is opened, as this have a huge impact on the network port 42 * throughput. 43 * 44 * Interface 2: Standard modem interface - circuit switched interface, this 45 * can be used to make a standard ppp connection however it 46 * should not be used in conjunction with the IP network interface 47 * enabled for USB performance reasons i.e. if using this set 48 * ideally disable_net=1. 49 * 50 *****************************************************************************/ 51 52 #include <linux/sched.h> 53 #include <linux/slab.h> 54 #include <linux/init.h> 55 #include <linux/delay.h> 56 #include <linux/netdevice.h> 57 #include <linux/module.h> 58 #include <linux/ethtool.h> 59 #include <linux/usb.h> 60 #include <linux/timer.h> 61 #include <linux/tty.h> 62 #include <linux/tty_driver.h> 63 #include <linux/tty_flip.h> 64 #include <linux/kmod.h> 65 #include <linux/rfkill.h> 66 #include <linux/ip.h> 67 #include <linux/uaccess.h> 68 #include <linux/usb/cdc.h> 69 #include <net/arp.h> 70 #include <asm/byteorder.h> 71 #include <linux/serial_core.h> 72 #include <linux/serial.h> 73 74 75 #define DRIVER_VERSION "1.2" 76 #define MOD_AUTHOR "Option Wireless" 77 #define MOD_DESCRIPTION "USB High Speed Option driver" 78 #define MOD_LICENSE "GPL" 79 80 #define HSO_MAX_NET_DEVICES 10 81 #define HSO__MAX_MTU 2048 82 #define DEFAULT_MTU 1500 83 #define DEFAULT_MRU 1500 84 85 #define CTRL_URB_RX_SIZE 1024 86 #define CTRL_URB_TX_SIZE 64 87 88 #define BULK_URB_RX_SIZE 4096 89 #define BULK_URB_TX_SIZE 8192 90 91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU 92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU 93 #define MUX_BULK_RX_BUF_COUNT 4 94 #define USB_TYPE_OPTION_VENDOR 0x20 95 96 /* These definitions are used with the struct hso_net flags element */ 97 /* - use *_bit operations on it. (bit indices not values.) */ 98 #define HSO_NET_RUNNING 0 99 100 #define HSO_NET_TX_TIMEOUT (HZ*10) 101 102 #define HSO_SERIAL_MAGIC 0x48534f31 103 104 /* Number of ttys to handle */ 105 #define HSO_SERIAL_TTY_MINORS 256 106 107 #define MAX_RX_URBS 2 108 109 static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty) 110 { 111 if (tty) 112 return tty->driver_data; 113 return NULL; 114 } 115 116 /*****************************************************************************/ 117 /* Debugging functions */ 118 /*****************************************************************************/ 119 #define D__(lvl_, fmt, arg...) \ 120 do { \ 121 printk(lvl_ "[%d:%s]: " fmt "\n", \ 122 __LINE__, __func__, ## arg); \ 123 } while (0) 124 125 #define D_(lvl, args...) \ 126 do { \ 127 if (lvl & debug) \ 128 D__(KERN_INFO, args); \ 129 } while (0) 130 131 #define D1(args...) D_(0x01, ##args) 132 #define D2(args...) D_(0x02, ##args) 133 #define D3(args...) D_(0x04, ##args) 134 #define D4(args...) D_(0x08, ##args) 135 #define D5(args...) D_(0x10, ##args) 136 137 /*****************************************************************************/ 138 /* Enumerators */ 139 /*****************************************************************************/ 140 enum pkt_parse_state { 141 WAIT_IP, 142 WAIT_DATA, 143 WAIT_SYNC 144 }; 145 146 /*****************************************************************************/ 147 /* Structs */ 148 /*****************************************************************************/ 149 150 struct hso_shared_int { 151 struct usb_endpoint_descriptor *intr_endp; 152 void *shared_intr_buf; 153 struct urb *shared_intr_urb; 154 struct usb_device *usb; 155 int use_count; 156 int ref_count; 157 struct mutex shared_int_lock; 158 }; 159 160 struct hso_net { 161 struct hso_device *parent; 162 struct net_device *net; 163 struct rfkill *rfkill; 164 165 struct usb_endpoint_descriptor *in_endp; 166 struct usb_endpoint_descriptor *out_endp; 167 168 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT]; 169 struct urb *mux_bulk_tx_urb; 170 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT]; 171 void *mux_bulk_tx_buf; 172 173 struct sk_buff *skb_rx_buf; 174 struct sk_buff *skb_tx_buf; 175 176 enum pkt_parse_state rx_parse_state; 177 spinlock_t net_lock; 178 179 unsigned short rx_buf_size; 180 unsigned short rx_buf_missing; 181 struct iphdr rx_ip_hdr; 182 183 unsigned long flags; 184 }; 185 186 enum rx_ctrl_state{ 187 RX_IDLE, 188 RX_SENT, 189 RX_PENDING 190 }; 191 192 #define BM_REQUEST_TYPE (0xa1) 193 #define B_NOTIFICATION (0x20) 194 #define W_VALUE (0x0) 195 #define W_INDEX (0x2) 196 #define W_LENGTH (0x2) 197 198 #define B_OVERRUN (0x1<<6) 199 #define B_PARITY (0x1<<5) 200 #define B_FRAMING (0x1<<4) 201 #define B_RING_SIGNAL (0x1<<3) 202 #define B_BREAK (0x1<<2) 203 #define B_TX_CARRIER (0x1<<1) 204 #define B_RX_CARRIER (0x1<<0) 205 206 struct hso_serial_state_notification { 207 u8 bmRequestType; 208 u8 bNotification; 209 u16 wValue; 210 u16 wIndex; 211 u16 wLength; 212 u16 UART_state_bitmap; 213 } __attribute__((packed)); 214 215 struct hso_tiocmget { 216 struct mutex mutex; 217 wait_queue_head_t waitq; 218 int intr_completed; 219 struct usb_endpoint_descriptor *endp; 220 struct urb *urb; 221 struct hso_serial_state_notification serial_state_notification; 222 u16 prev_UART_state_bitmap; 223 struct uart_icount icount; 224 }; 225 226 227 struct hso_serial { 228 struct hso_device *parent; 229 int magic; 230 u8 minor; 231 232 struct hso_shared_int *shared_int; 233 234 /* rx/tx urb could be either a bulk urb or a control urb depending 235 on which serial port it is used on. */ 236 struct urb *rx_urb[MAX_RX_URBS]; 237 u8 num_rx_urbs; 238 u8 *rx_data[MAX_RX_URBS]; 239 u16 rx_data_length; /* should contain allocated length */ 240 241 struct urb *tx_urb; 242 u8 *tx_data; 243 u8 *tx_buffer; 244 u16 tx_data_length; /* should contain allocated length */ 245 u16 tx_data_count; 246 u16 tx_buffer_count; 247 struct usb_ctrlrequest ctrl_req_tx; 248 struct usb_ctrlrequest ctrl_req_rx; 249 250 struct usb_endpoint_descriptor *in_endp; 251 struct usb_endpoint_descriptor *out_endp; 252 253 enum rx_ctrl_state rx_state; 254 u8 rts_state; 255 u8 dtr_state; 256 unsigned tx_urb_used:1; 257 258 /* from usb_serial_port */ 259 struct tty_struct *tty; 260 int open_count; 261 spinlock_t serial_lock; 262 263 int (*write_data) (struct hso_serial *serial); 264 struct hso_tiocmget *tiocmget; 265 /* Hacks required to get flow control 266 * working on the serial receive buffers 267 * so as not to drop characters on the floor. 268 */ 269 int curr_rx_urb_idx; 270 u16 curr_rx_urb_offset; 271 u8 rx_urb_filled[MAX_RX_URBS]; 272 struct tasklet_struct unthrottle_tasklet; 273 struct work_struct retry_unthrottle_workqueue; 274 }; 275 276 struct hso_device { 277 union { 278 struct hso_serial *dev_serial; 279 struct hso_net *dev_net; 280 } port_data; 281 282 u32 port_spec; 283 284 u8 is_active; 285 u8 usb_gone; 286 struct work_struct async_get_intf; 287 struct work_struct async_put_intf; 288 289 struct usb_device *usb; 290 struct usb_interface *interface; 291 292 struct device *dev; 293 struct kref ref; 294 struct mutex mutex; 295 }; 296 297 /* Type of interface */ 298 #define HSO_INTF_MASK 0xFF00 299 #define HSO_INTF_MUX 0x0100 300 #define HSO_INTF_BULK 0x0200 301 302 /* Type of port */ 303 #define HSO_PORT_MASK 0xFF 304 #define HSO_PORT_NO_PORT 0x0 305 #define HSO_PORT_CONTROL 0x1 306 #define HSO_PORT_APP 0x2 307 #define HSO_PORT_GPS 0x3 308 #define HSO_PORT_PCSC 0x4 309 #define HSO_PORT_APP2 0x5 310 #define HSO_PORT_GPS_CONTROL 0x6 311 #define HSO_PORT_MSD 0x7 312 #define HSO_PORT_VOICE 0x8 313 #define HSO_PORT_DIAG2 0x9 314 #define HSO_PORT_DIAG 0x10 315 #define HSO_PORT_MODEM 0x11 316 #define HSO_PORT_NETWORK 0x12 317 318 /* Additional device info */ 319 #define HSO_INFO_MASK 0xFF000000 320 #define HSO_INFO_CRC_BUG 0x01000000 321 322 /*****************************************************************************/ 323 /* Prototypes */ 324 /*****************************************************************************/ 325 /* Serial driver functions */ 326 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file, 327 unsigned int set, unsigned int clear); 328 static void ctrl_callback(struct urb *urb); 329 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial); 330 static void hso_kick_transmit(struct hso_serial *serial); 331 /* Helper functions */ 332 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int, 333 struct usb_device *usb, gfp_t gfp); 334 static void log_usb_status(int status, const char *function); 335 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf, 336 int type, int dir); 337 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports); 338 static void hso_free_interface(struct usb_interface *intf); 339 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags); 340 static int hso_stop_serial_device(struct hso_device *hso_dev); 341 static int hso_start_net_device(struct hso_device *hso_dev); 342 static void hso_free_shared_int(struct hso_shared_int *shared_int); 343 static int hso_stop_net_device(struct hso_device *hso_dev); 344 static void hso_serial_ref_free(struct kref *ref); 345 static void hso_std_serial_read_bulk_callback(struct urb *urb); 346 static int hso_mux_serial_read(struct hso_serial *serial); 347 static void async_get_intf(struct work_struct *data); 348 static void async_put_intf(struct work_struct *data); 349 static int hso_put_activity(struct hso_device *hso_dev); 350 static int hso_get_activity(struct hso_device *hso_dev); 351 static void tiocmget_intr_callback(struct urb *urb); 352 /*****************************************************************************/ 353 /* Helping functions */ 354 /*****************************************************************************/ 355 356 /* #define DEBUG */ 357 358 static inline struct hso_net *dev2net(struct hso_device *hso_dev) 359 { 360 return hso_dev->port_data.dev_net; 361 } 362 363 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev) 364 { 365 return hso_dev->port_data.dev_serial; 366 } 367 368 /* Debugging functions */ 369 #ifdef DEBUG 370 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf, 371 unsigned int len) 372 { 373 static char name[255]; 374 375 sprintf(name, "hso[%d:%s]", line_count, func_name); 376 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len); 377 } 378 379 #define DUMP(buf_, len_) \ 380 dbg_dump(__LINE__, __func__, buf_, len_) 381 382 #define DUMP1(buf_, len_) \ 383 do { \ 384 if (0x01 & debug) \ 385 DUMP(buf_, len_); \ 386 } while (0) 387 #else 388 #define DUMP(buf_, len_) 389 #define DUMP1(buf_, len_) 390 #endif 391 392 /* module parameters */ 393 static int debug; 394 static int tty_major; 395 static int disable_net; 396 397 /* driver info */ 398 static const char driver_name[] = "hso"; 399 static const char tty_filename[] = "ttyHS"; 400 static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR; 401 /* the usb driver itself (registered in hso_init) */ 402 static struct usb_driver hso_driver; 403 /* serial structures */ 404 static struct tty_driver *tty_drv; 405 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS]; 406 static struct hso_device *network_table[HSO_MAX_NET_DEVICES]; 407 static spinlock_t serial_table_lock; 408 409 static const s32 default_port_spec[] = { 410 HSO_INTF_MUX | HSO_PORT_NETWORK, 411 HSO_INTF_BULK | HSO_PORT_DIAG, 412 HSO_INTF_BULK | HSO_PORT_MODEM, 413 0 414 }; 415 416 static const s32 icon321_port_spec[] = { 417 HSO_INTF_MUX | HSO_PORT_NETWORK, 418 HSO_INTF_BULK | HSO_PORT_DIAG2, 419 HSO_INTF_BULK | HSO_PORT_MODEM, 420 HSO_INTF_BULK | HSO_PORT_DIAG, 421 0 422 }; 423 424 #define default_port_device(vendor, product) \ 425 USB_DEVICE(vendor, product), \ 426 .driver_info = (kernel_ulong_t)default_port_spec 427 428 #define icon321_port_device(vendor, product) \ 429 USB_DEVICE(vendor, product), \ 430 .driver_info = (kernel_ulong_t)icon321_port_spec 431 432 /* list of devices we support */ 433 static const struct usb_device_id hso_ids[] = { 434 {default_port_device(0x0af0, 0x6711)}, 435 {default_port_device(0x0af0, 0x6731)}, 436 {default_port_device(0x0af0, 0x6751)}, 437 {default_port_device(0x0af0, 0x6771)}, 438 {default_port_device(0x0af0, 0x6791)}, 439 {default_port_device(0x0af0, 0x6811)}, 440 {default_port_device(0x0af0, 0x6911)}, 441 {default_port_device(0x0af0, 0x6951)}, 442 {default_port_device(0x0af0, 0x6971)}, 443 {default_port_device(0x0af0, 0x7011)}, 444 {default_port_device(0x0af0, 0x7031)}, 445 {default_port_device(0x0af0, 0x7051)}, 446 {default_port_device(0x0af0, 0x7071)}, 447 {default_port_device(0x0af0, 0x7111)}, 448 {default_port_device(0x0af0, 0x7211)}, 449 {default_port_device(0x0af0, 0x7251)}, 450 {default_port_device(0x0af0, 0x7271)}, 451 {default_port_device(0x0af0, 0x7311)}, 452 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */ 453 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */ 454 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */ 455 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */ 456 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */ 457 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */ 458 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */ 459 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */ 460 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */ 461 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */ 462 {USB_DEVICE(0x0af0, 0x7701)}, 463 {USB_DEVICE(0x0af0, 0x7801)}, 464 {USB_DEVICE(0x0af0, 0x7901)}, 465 {USB_DEVICE(0x0af0, 0x7361)}, 466 {USB_DEVICE(0x0af0, 0xd057)}, 467 {USB_DEVICE(0x0af0, 0xd055)}, 468 {} 469 }; 470 MODULE_DEVICE_TABLE(usb, hso_ids); 471 472 /* Sysfs attribute */ 473 static ssize_t hso_sysfs_show_porttype(struct device *dev, 474 struct device_attribute *attr, 475 char *buf) 476 { 477 struct hso_device *hso_dev = dev->driver_data; 478 char *port_name; 479 480 if (!hso_dev) 481 return 0; 482 483 switch (hso_dev->port_spec & HSO_PORT_MASK) { 484 case HSO_PORT_CONTROL: 485 port_name = "Control"; 486 break; 487 case HSO_PORT_APP: 488 port_name = "Application"; 489 break; 490 case HSO_PORT_APP2: 491 port_name = "Application2"; 492 break; 493 case HSO_PORT_GPS: 494 port_name = "GPS"; 495 break; 496 case HSO_PORT_GPS_CONTROL: 497 port_name = "GPS Control"; 498 break; 499 case HSO_PORT_PCSC: 500 port_name = "PCSC"; 501 break; 502 case HSO_PORT_DIAG: 503 port_name = "Diagnostic"; 504 break; 505 case HSO_PORT_DIAG2: 506 port_name = "Diagnostic2"; 507 break; 508 case HSO_PORT_MODEM: 509 port_name = "Modem"; 510 break; 511 case HSO_PORT_NETWORK: 512 port_name = "Network"; 513 break; 514 default: 515 port_name = "Unknown"; 516 break; 517 } 518 519 return sprintf(buf, "%s\n", port_name); 520 } 521 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL); 522 523 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb) 524 { 525 int idx; 526 527 for (idx = 0; idx < serial->num_rx_urbs; idx++) 528 if (serial->rx_urb[idx] == urb) 529 return idx; 530 dev_err(serial->parent->dev, "hso_urb_to_index failed\n"); 531 return -1; 532 } 533 534 /* converts mux value to a port spec value */ 535 static u32 hso_mux_to_port(int mux) 536 { 537 u32 result; 538 539 switch (mux) { 540 case 0x1: 541 result = HSO_PORT_CONTROL; 542 break; 543 case 0x2: 544 result = HSO_PORT_APP; 545 break; 546 case 0x4: 547 result = HSO_PORT_PCSC; 548 break; 549 case 0x8: 550 result = HSO_PORT_GPS; 551 break; 552 case 0x10: 553 result = HSO_PORT_APP2; 554 break; 555 default: 556 result = HSO_PORT_NO_PORT; 557 } 558 return result; 559 } 560 561 /* converts port spec value to a mux value */ 562 static u32 hso_port_to_mux(int port) 563 { 564 u32 result; 565 566 switch (port & HSO_PORT_MASK) { 567 case HSO_PORT_CONTROL: 568 result = 0x0; 569 break; 570 case HSO_PORT_APP: 571 result = 0x1; 572 break; 573 case HSO_PORT_PCSC: 574 result = 0x2; 575 break; 576 case HSO_PORT_GPS: 577 result = 0x3; 578 break; 579 case HSO_PORT_APP2: 580 result = 0x4; 581 break; 582 default: 583 result = 0x0; 584 } 585 return result; 586 } 587 588 static struct hso_serial *get_serial_by_shared_int_and_type( 589 struct hso_shared_int *shared_int, 590 int mux) 591 { 592 int i, port; 593 594 port = hso_mux_to_port(mux); 595 596 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 597 if (serial_table[i] 598 && (dev2ser(serial_table[i])->shared_int == shared_int) 599 && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) { 600 return dev2ser(serial_table[i]); 601 } 602 } 603 604 return NULL; 605 } 606 607 static struct hso_serial *get_serial_by_index(unsigned index) 608 { 609 struct hso_serial *serial = NULL; 610 unsigned long flags; 611 612 spin_lock_irqsave(&serial_table_lock, flags); 613 if (serial_table[index]) 614 serial = dev2ser(serial_table[index]); 615 spin_unlock_irqrestore(&serial_table_lock, flags); 616 617 return serial; 618 } 619 620 static int get_free_serial_index(void) 621 { 622 int index; 623 unsigned long flags; 624 625 spin_lock_irqsave(&serial_table_lock, flags); 626 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) { 627 if (serial_table[index] == NULL) { 628 spin_unlock_irqrestore(&serial_table_lock, flags); 629 return index; 630 } 631 } 632 spin_unlock_irqrestore(&serial_table_lock, flags); 633 634 printk(KERN_ERR "%s: no free serial devices in table\n", __func__); 635 return -1; 636 } 637 638 static void set_serial_by_index(unsigned index, struct hso_serial *serial) 639 { 640 unsigned long flags; 641 642 spin_lock_irqsave(&serial_table_lock, flags); 643 if (serial) 644 serial_table[index] = serial->parent; 645 else 646 serial_table[index] = NULL; 647 spin_unlock_irqrestore(&serial_table_lock, flags); 648 } 649 650 /* log a meaningful explanation of an USB status */ 651 static void log_usb_status(int status, const char *function) 652 { 653 char *explanation; 654 655 switch (status) { 656 case -ENODEV: 657 explanation = "no device"; 658 break; 659 case -ENOENT: 660 explanation = "endpoint not enabled"; 661 break; 662 case -EPIPE: 663 explanation = "endpoint stalled"; 664 break; 665 case -ENOSPC: 666 explanation = "not enough bandwidth"; 667 break; 668 case -ESHUTDOWN: 669 explanation = "device disabled"; 670 break; 671 case -EHOSTUNREACH: 672 explanation = "device suspended"; 673 break; 674 case -EINVAL: 675 case -EAGAIN: 676 case -EFBIG: 677 case -EMSGSIZE: 678 explanation = "internal error"; 679 break; 680 default: 681 explanation = "unknown status"; 682 break; 683 } 684 D1("%s: received USB status - %s (%d)", function, explanation, status); 685 } 686 687 /* Network interface functions */ 688 689 /* called when net interface is brought up by ifconfig */ 690 static int hso_net_open(struct net_device *net) 691 { 692 struct hso_net *odev = netdev_priv(net); 693 unsigned long flags = 0; 694 695 if (!odev) { 696 dev_err(&net->dev, "No net device !\n"); 697 return -ENODEV; 698 } 699 700 odev->skb_tx_buf = NULL; 701 702 /* setup environment */ 703 spin_lock_irqsave(&odev->net_lock, flags); 704 odev->rx_parse_state = WAIT_IP; 705 odev->rx_buf_size = 0; 706 odev->rx_buf_missing = sizeof(struct iphdr); 707 spin_unlock_irqrestore(&odev->net_lock, flags); 708 709 /* We are up and running. */ 710 set_bit(HSO_NET_RUNNING, &odev->flags); 711 hso_start_net_device(odev->parent); 712 713 /* Tell the kernel we are ready to start receiving from it */ 714 netif_start_queue(net); 715 716 return 0; 717 } 718 719 /* called when interface is brought down by ifconfig */ 720 static int hso_net_close(struct net_device *net) 721 { 722 struct hso_net *odev = netdev_priv(net); 723 724 /* we don't need the queue anymore */ 725 netif_stop_queue(net); 726 /* no longer running */ 727 clear_bit(HSO_NET_RUNNING, &odev->flags); 728 729 hso_stop_net_device(odev->parent); 730 731 /* done */ 732 return 0; 733 } 734 735 /* USB tells is xmit done, we should start the netqueue again */ 736 static void write_bulk_callback(struct urb *urb) 737 { 738 struct hso_net *odev = urb->context; 739 int status = urb->status; 740 741 /* Sanity check */ 742 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) { 743 dev_err(&urb->dev->dev, "%s: device not running\n", __func__); 744 return; 745 } 746 747 /* Do we still have a valid kernel network device? */ 748 if (!netif_device_present(odev->net)) { 749 dev_err(&urb->dev->dev, "%s: net device not present\n", 750 __func__); 751 return; 752 } 753 754 /* log status, but don't act on it, we don't need to resubmit anything 755 * anyhow */ 756 if (status) 757 log_usb_status(status, __func__); 758 759 hso_put_activity(odev->parent); 760 761 /* Tell the network interface we are ready for another frame */ 762 netif_wake_queue(odev->net); 763 } 764 765 /* called by kernel when we need to transmit a packet */ 766 static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net) 767 { 768 struct hso_net *odev = netdev_priv(net); 769 int result; 770 771 /* Tell the kernel, "No more frames 'til we are done with this one." */ 772 netif_stop_queue(net); 773 if (hso_get_activity(odev->parent) == -EAGAIN) { 774 odev->skb_tx_buf = skb; 775 return 0; 776 } 777 778 /* log if asked */ 779 DUMP1(skb->data, skb->len); 780 /* Copy it from kernel memory to OUR memory */ 781 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len); 782 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE); 783 784 /* Fill in the URB for shipping it out. */ 785 usb_fill_bulk_urb(odev->mux_bulk_tx_urb, 786 odev->parent->usb, 787 usb_sndbulkpipe(odev->parent->usb, 788 odev->out_endp-> 789 bEndpointAddress & 0x7F), 790 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback, 791 odev); 792 793 /* Deal with the Zero Length packet problem, I hope */ 794 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET; 795 796 /* Send the URB on its merry way. */ 797 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC); 798 if (result) { 799 dev_warn(&odev->parent->interface->dev, 800 "failed mux_bulk_tx_urb %d", result); 801 net->stats.tx_errors++; 802 netif_start_queue(net); 803 } else { 804 net->stats.tx_packets++; 805 net->stats.tx_bytes += skb->len; 806 /* And tell the kernel when the last transmit started. */ 807 net->trans_start = jiffies; 808 } 809 dev_kfree_skb(skb); 810 /* we're done */ 811 return result; 812 } 813 814 static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 815 { 816 struct hso_net *odev = netdev_priv(net); 817 818 strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN); 819 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); 820 usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info); 821 } 822 823 static struct ethtool_ops ops = { 824 .get_drvinfo = hso_get_drvinfo, 825 .get_link = ethtool_op_get_link 826 }; 827 828 /* called when a packet did not ack after watchdogtimeout */ 829 static void hso_net_tx_timeout(struct net_device *net) 830 { 831 struct hso_net *odev = netdev_priv(net); 832 833 if (!odev) 834 return; 835 836 /* Tell syslog we are hosed. */ 837 dev_warn(&net->dev, "Tx timed out.\n"); 838 839 /* Tear the waiting frame off the list */ 840 if (odev->mux_bulk_tx_urb 841 && (odev->mux_bulk_tx_urb->status == -EINPROGRESS)) 842 usb_unlink_urb(odev->mux_bulk_tx_urb); 843 844 /* Update statistics */ 845 net->stats.tx_errors++; 846 } 847 848 /* make a real packet from the received USB buffer */ 849 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt, 850 unsigned int count, unsigned char is_eop) 851 { 852 unsigned short temp_bytes; 853 unsigned short buffer_offset = 0; 854 unsigned short frame_len; 855 unsigned char *tmp_rx_buf; 856 857 /* log if needed */ 858 D1("Rx %d bytes", count); 859 DUMP(ip_pkt, min(128, (int)count)); 860 861 while (count) { 862 switch (odev->rx_parse_state) { 863 case WAIT_IP: 864 /* waiting for IP header. */ 865 /* wanted bytes - size of ip header */ 866 temp_bytes = 867 (count < 868 odev->rx_buf_missing) ? count : odev-> 869 rx_buf_missing; 870 871 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) + 872 odev->rx_buf_size, ip_pkt + buffer_offset, 873 temp_bytes); 874 875 odev->rx_buf_size += temp_bytes; 876 buffer_offset += temp_bytes; 877 odev->rx_buf_missing -= temp_bytes; 878 count -= temp_bytes; 879 880 if (!odev->rx_buf_missing) { 881 /* header is complete allocate an sk_buffer and 882 * continue to WAIT_DATA */ 883 frame_len = ntohs(odev->rx_ip_hdr.tot_len); 884 885 if ((frame_len > DEFAULT_MRU) || 886 (frame_len < sizeof(struct iphdr))) { 887 dev_err(&odev->net->dev, 888 "Invalid frame (%d) length\n", 889 frame_len); 890 odev->rx_parse_state = WAIT_SYNC; 891 continue; 892 } 893 /* Allocate an sk_buff */ 894 odev->skb_rx_buf = dev_alloc_skb(frame_len); 895 if (!odev->skb_rx_buf) { 896 /* We got no receive buffer. */ 897 D1("could not allocate memory"); 898 odev->rx_parse_state = WAIT_SYNC; 899 return; 900 } 901 /* Here's where it came from */ 902 odev->skb_rx_buf->dev = odev->net; 903 904 /* Copy what we got so far. make room for iphdr 905 * after tail. */ 906 tmp_rx_buf = 907 skb_put(odev->skb_rx_buf, 908 sizeof(struct iphdr)); 909 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr), 910 sizeof(struct iphdr)); 911 912 /* ETH_HLEN */ 913 odev->rx_buf_size = sizeof(struct iphdr); 914 915 /* Filip actually use .tot_len */ 916 odev->rx_buf_missing = 917 frame_len - sizeof(struct iphdr); 918 odev->rx_parse_state = WAIT_DATA; 919 } 920 break; 921 922 case WAIT_DATA: 923 temp_bytes = (count < odev->rx_buf_missing) 924 ? count : odev->rx_buf_missing; 925 926 /* Copy the rest of the bytes that are left in the 927 * buffer into the waiting sk_buf. */ 928 /* Make room for temp_bytes after tail. */ 929 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes); 930 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes); 931 932 odev->rx_buf_missing -= temp_bytes; 933 count -= temp_bytes; 934 buffer_offset += temp_bytes; 935 odev->rx_buf_size += temp_bytes; 936 if (!odev->rx_buf_missing) { 937 /* Packet is complete. Inject into stack. */ 938 /* We have IP packet here */ 939 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP); 940 /* don't check it */ 941 odev->skb_rx_buf->ip_summed = 942 CHECKSUM_UNNECESSARY; 943 944 skb_reset_mac_header(odev->skb_rx_buf); 945 946 /* Ship it off to the kernel */ 947 netif_rx(odev->skb_rx_buf); 948 /* No longer our buffer. */ 949 odev->skb_rx_buf = NULL; 950 951 /* update out statistics */ 952 odev->net->stats.rx_packets++; 953 954 odev->net->stats.rx_bytes += odev->rx_buf_size; 955 956 odev->rx_buf_size = 0; 957 odev->rx_buf_missing = sizeof(struct iphdr); 958 odev->rx_parse_state = WAIT_IP; 959 } 960 break; 961 962 case WAIT_SYNC: 963 D1(" W_S"); 964 count = 0; 965 break; 966 default: 967 D1(" "); 968 count--; 969 break; 970 } 971 } 972 973 /* Recovery mechanism for WAIT_SYNC state. */ 974 if (is_eop) { 975 if (odev->rx_parse_state == WAIT_SYNC) { 976 odev->rx_parse_state = WAIT_IP; 977 odev->rx_buf_size = 0; 978 odev->rx_buf_missing = sizeof(struct iphdr); 979 } 980 } 981 } 982 983 /* Moving data from usb to kernel (in interrupt state) */ 984 static void read_bulk_callback(struct urb *urb) 985 { 986 struct hso_net *odev = urb->context; 987 struct net_device *net; 988 int result; 989 int status = urb->status; 990 991 /* is al ok? (Filip: Who's Al ?) */ 992 if (status) { 993 log_usb_status(status, __func__); 994 return; 995 } 996 997 /* Sanity check */ 998 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) { 999 D1("BULK IN callback but driver is not active!"); 1000 return; 1001 } 1002 usb_mark_last_busy(urb->dev); 1003 1004 net = odev->net; 1005 1006 if (!netif_device_present(net)) { 1007 /* Somebody killed our network interface... */ 1008 return; 1009 } 1010 1011 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) { 1012 u32 rest; 1013 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; 1014 rest = urb->actual_length % odev->in_endp->wMaxPacketSize; 1015 if (((rest == 5) || (rest == 6)) 1016 && !memcmp(((u8 *) urb->transfer_buffer) + 1017 urb->actual_length - 4, crc_check, 4)) { 1018 urb->actual_length -= 4; 1019 } 1020 } 1021 1022 /* do we even have a packet? */ 1023 if (urb->actual_length) { 1024 /* Handle the IP stream, add header and push it onto network 1025 * stack if the packet is complete. */ 1026 spin_lock(&odev->net_lock); 1027 packetizeRx(odev, urb->transfer_buffer, urb->actual_length, 1028 (urb->transfer_buffer_length > 1029 urb->actual_length) ? 1 : 0); 1030 spin_unlock(&odev->net_lock); 1031 } 1032 1033 /* We are done with this URB, resubmit it. Prep the USB to wait for 1034 * another frame. Reuse same as received. */ 1035 usb_fill_bulk_urb(urb, 1036 odev->parent->usb, 1037 usb_rcvbulkpipe(odev->parent->usb, 1038 odev->in_endp-> 1039 bEndpointAddress & 0x7F), 1040 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE, 1041 read_bulk_callback, odev); 1042 1043 /* Give this to the USB subsystem so it can tell us when more data 1044 * arrives. */ 1045 result = usb_submit_urb(urb, GFP_ATOMIC); 1046 if (result) 1047 dev_warn(&odev->parent->interface->dev, 1048 "%s failed submit mux_bulk_rx_urb %d", __func__, 1049 result); 1050 } 1051 1052 /* Serial driver functions */ 1053 1054 static void hso_init_termios(struct ktermios *termios) 1055 { 1056 /* 1057 * The default requirements for this device are: 1058 */ 1059 termios->c_iflag &= 1060 ~(IGNBRK /* disable ignore break */ 1061 | BRKINT /* disable break causes interrupt */ 1062 | PARMRK /* disable mark parity errors */ 1063 | ISTRIP /* disable clear high bit of input characters */ 1064 | INLCR /* disable translate NL to CR */ 1065 | IGNCR /* disable ignore CR */ 1066 | ICRNL /* disable translate CR to NL */ 1067 | IXON); /* disable enable XON/XOFF flow control */ 1068 1069 /* disable postprocess output characters */ 1070 termios->c_oflag &= ~OPOST; 1071 1072 termios->c_lflag &= 1073 ~(ECHO /* disable echo input characters */ 1074 | ECHONL /* disable echo new line */ 1075 | ICANON /* disable erase, kill, werase, and rprnt 1076 special characters */ 1077 | ISIG /* disable interrupt, quit, and suspend special 1078 characters */ 1079 | IEXTEN); /* disable non-POSIX special characters */ 1080 1081 termios->c_cflag &= 1082 ~(CSIZE /* no size */ 1083 | PARENB /* disable parity bit */ 1084 | CBAUD /* clear current baud rate */ 1085 | CBAUDEX); /* clear current buad rate */ 1086 1087 termios->c_cflag |= CS8; /* character size 8 bits */ 1088 1089 /* baud rate 115200 */ 1090 tty_termios_encode_baud_rate(termios, 115200, 115200); 1091 } 1092 1093 static void _hso_serial_set_termios(struct tty_struct *tty, 1094 struct ktermios *old) 1095 { 1096 struct hso_serial *serial = get_serial_by_tty(tty); 1097 struct ktermios *termios; 1098 1099 if (!serial) { 1100 printk(KERN_ERR "%s: no tty structures", __func__); 1101 return; 1102 } 1103 1104 D4("port %d", serial->minor); 1105 1106 /* 1107 * Fix up unsupported bits 1108 */ 1109 termios = tty->termios; 1110 termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */ 1111 1112 termios->c_cflag &= 1113 ~(CSIZE /* no size */ 1114 | PARENB /* disable parity bit */ 1115 | CBAUD /* clear current baud rate */ 1116 | CBAUDEX); /* clear current buad rate */ 1117 1118 termios->c_cflag |= CS8; /* character size 8 bits */ 1119 1120 /* baud rate 115200 */ 1121 tty_encode_baud_rate(tty, 115200, 115200); 1122 } 1123 1124 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb) 1125 { 1126 int result; 1127 #ifdef CONFIG_HSO_AUTOPM 1128 usb_mark_last_busy(urb->dev); 1129 #endif 1130 /* We are done with this URB, resubmit it. Prep the USB to wait for 1131 * another frame */ 1132 usb_fill_bulk_urb(urb, serial->parent->usb, 1133 usb_rcvbulkpipe(serial->parent->usb, 1134 serial->in_endp-> 1135 bEndpointAddress & 0x7F), 1136 urb->transfer_buffer, serial->rx_data_length, 1137 hso_std_serial_read_bulk_callback, serial); 1138 /* Give this to the USB subsystem so it can tell us when more data 1139 * arrives. */ 1140 result = usb_submit_urb(urb, GFP_ATOMIC); 1141 if (result) { 1142 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n", 1143 __func__, result); 1144 } 1145 } 1146 1147 1148 1149 1150 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial) 1151 { 1152 int count; 1153 struct urb *curr_urb; 1154 1155 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) { 1156 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx]; 1157 count = put_rxbuf_data(curr_urb, serial); 1158 if (count == -1) 1159 return; 1160 if (count == 0) { 1161 serial->curr_rx_urb_idx++; 1162 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs) 1163 serial->curr_rx_urb_idx = 0; 1164 hso_resubmit_rx_bulk_urb(serial, curr_urb); 1165 } 1166 } 1167 } 1168 1169 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial) 1170 { 1171 int count = 0; 1172 struct urb *urb; 1173 1174 urb = serial->rx_urb[0]; 1175 if (serial->open_count > 0) { 1176 count = put_rxbuf_data(urb, serial); 1177 if (count == -1) 1178 return; 1179 } 1180 /* Re issue a read as long as we receive data. */ 1181 1182 if (count == 0 && ((urb->actual_length != 0) || 1183 (serial->rx_state == RX_PENDING))) { 1184 serial->rx_state = RX_SENT; 1185 hso_mux_serial_read(serial); 1186 } else 1187 serial->rx_state = RX_IDLE; 1188 } 1189 1190 1191 /* read callback for Diag and CS port */ 1192 static void hso_std_serial_read_bulk_callback(struct urb *urb) 1193 { 1194 struct hso_serial *serial = urb->context; 1195 int status = urb->status; 1196 1197 /* sanity check */ 1198 if (!serial) { 1199 D1("serial == NULL"); 1200 return; 1201 } else if (status) { 1202 log_usb_status(status, __func__); 1203 return; 1204 } 1205 1206 D4("\n--- Got serial_read_bulk callback %02x ---", status); 1207 D1("Actual length = %d\n", urb->actual_length); 1208 DUMP1(urb->transfer_buffer, urb->actual_length); 1209 1210 /* Anyone listening? */ 1211 if (serial->open_count == 0) 1212 return; 1213 1214 if (status == 0) { 1215 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) { 1216 u32 rest; 1217 u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; 1218 rest = 1219 urb->actual_length % 1220 serial->in_endp->wMaxPacketSize; 1221 if (((rest == 5) || (rest == 6)) 1222 && !memcmp(((u8 *) urb->transfer_buffer) + 1223 urb->actual_length - 4, crc_check, 4)) { 1224 urb->actual_length -= 4; 1225 } 1226 } 1227 /* Valid data, handle RX data */ 1228 spin_lock(&serial->serial_lock); 1229 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1; 1230 put_rxbuf_data_and_resubmit_bulk_urb(serial); 1231 spin_unlock(&serial->serial_lock); 1232 } else if (status == -ENOENT || status == -ECONNRESET) { 1233 /* Unlinked - check for throttled port. */ 1234 D2("Port %d, successfully unlinked urb", serial->minor); 1235 spin_lock(&serial->serial_lock); 1236 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0; 1237 hso_resubmit_rx_bulk_urb(serial, urb); 1238 spin_unlock(&serial->serial_lock); 1239 } else { 1240 D2("Port %d, status = %d for read urb", serial->minor, status); 1241 return; 1242 } 1243 } 1244 1245 /* 1246 * This needs to be a tasklet otherwise we will 1247 * end up recursively calling this function. 1248 */ 1249 static void hso_unthrottle_tasklet(struct hso_serial *serial) 1250 { 1251 unsigned long flags; 1252 1253 spin_lock_irqsave(&serial->serial_lock, flags); 1254 if ((serial->parent->port_spec & HSO_INTF_MUX)) 1255 put_rxbuf_data_and_resubmit_ctrl_urb(serial); 1256 else 1257 put_rxbuf_data_and_resubmit_bulk_urb(serial); 1258 spin_unlock_irqrestore(&serial->serial_lock, flags); 1259 } 1260 1261 static void hso_unthrottle(struct tty_struct *tty) 1262 { 1263 struct hso_serial *serial = get_serial_by_tty(tty); 1264 1265 tasklet_hi_schedule(&serial->unthrottle_tasklet); 1266 } 1267 1268 static void hso_unthrottle_workfunc(struct work_struct *work) 1269 { 1270 struct hso_serial *serial = 1271 container_of(work, struct hso_serial, 1272 retry_unthrottle_workqueue); 1273 hso_unthrottle_tasklet(serial); 1274 } 1275 1276 /* open the requested serial port */ 1277 static int hso_serial_open(struct tty_struct *tty, struct file *filp) 1278 { 1279 struct hso_serial *serial = get_serial_by_index(tty->index); 1280 int result; 1281 1282 /* sanity check */ 1283 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) { 1284 WARN_ON(1); 1285 tty->driver_data = NULL; 1286 D1("Failed to open port"); 1287 return -ENODEV; 1288 } 1289 1290 mutex_lock(&serial->parent->mutex); 1291 result = usb_autopm_get_interface(serial->parent->interface); 1292 if (result < 0) 1293 goto err_out; 1294 1295 D1("Opening %d", serial->minor); 1296 kref_get(&serial->parent->ref); 1297 1298 /* setup */ 1299 spin_lock_irq(&serial->serial_lock); 1300 tty->driver_data = serial; 1301 tty_kref_put(serial->tty); 1302 serial->tty = tty_kref_get(tty); 1303 spin_unlock_irq(&serial->serial_lock); 1304 1305 /* check for port already opened, if not set the termios */ 1306 serial->open_count++; 1307 if (serial->open_count == 1) { 1308 tty->low_latency = 1; 1309 serial->rx_state = RX_IDLE; 1310 /* Force default termio settings */ 1311 _hso_serial_set_termios(tty, NULL); 1312 tasklet_init(&serial->unthrottle_tasklet, 1313 (void (*)(unsigned long))hso_unthrottle_tasklet, 1314 (unsigned long)serial); 1315 INIT_WORK(&serial->retry_unthrottle_workqueue, 1316 hso_unthrottle_workfunc); 1317 result = hso_start_serial_device(serial->parent, GFP_KERNEL); 1318 if (result) { 1319 hso_stop_serial_device(serial->parent); 1320 serial->open_count--; 1321 kref_put(&serial->parent->ref, hso_serial_ref_free); 1322 } 1323 } else { 1324 D1("Port was already open"); 1325 } 1326 1327 usb_autopm_put_interface(serial->parent->interface); 1328 1329 /* done */ 1330 if (result) 1331 hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0); 1332 err_out: 1333 mutex_unlock(&serial->parent->mutex); 1334 return result; 1335 } 1336 1337 /* close the requested serial port */ 1338 static void hso_serial_close(struct tty_struct *tty, struct file *filp) 1339 { 1340 struct hso_serial *serial = tty->driver_data; 1341 u8 usb_gone; 1342 1343 D1("Closing serial port"); 1344 1345 /* Open failed, no close cleanup required */ 1346 if (serial == NULL) 1347 return; 1348 1349 mutex_lock(&serial->parent->mutex); 1350 usb_gone = serial->parent->usb_gone; 1351 1352 if (!usb_gone) 1353 usb_autopm_get_interface(serial->parent->interface); 1354 1355 /* reset the rts and dtr */ 1356 /* do the actual close */ 1357 serial->open_count--; 1358 kref_put(&serial->parent->ref, hso_serial_ref_free); 1359 if (serial->open_count <= 0) { 1360 serial->open_count = 0; 1361 spin_lock_irq(&serial->serial_lock); 1362 if (serial->tty == tty) { 1363 serial->tty->driver_data = NULL; 1364 serial->tty = NULL; 1365 tty_kref_put(tty); 1366 } 1367 spin_unlock_irq(&serial->serial_lock); 1368 if (!usb_gone) 1369 hso_stop_serial_device(serial->parent); 1370 tasklet_kill(&serial->unthrottle_tasklet); 1371 cancel_work_sync(&serial->retry_unthrottle_workqueue); 1372 } 1373 1374 if (!usb_gone) 1375 usb_autopm_put_interface(serial->parent->interface); 1376 1377 mutex_unlock(&serial->parent->mutex); 1378 } 1379 1380 /* close the requested serial port */ 1381 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf, 1382 int count) 1383 { 1384 struct hso_serial *serial = get_serial_by_tty(tty); 1385 int space, tx_bytes; 1386 unsigned long flags; 1387 1388 /* sanity check */ 1389 if (serial == NULL) { 1390 printk(KERN_ERR "%s: serial is NULL\n", __func__); 1391 return -ENODEV; 1392 } 1393 1394 spin_lock_irqsave(&serial->serial_lock, flags); 1395 1396 space = serial->tx_data_length - serial->tx_buffer_count; 1397 tx_bytes = (count < space) ? count : space; 1398 1399 if (!tx_bytes) 1400 goto out; 1401 1402 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes); 1403 serial->tx_buffer_count += tx_bytes; 1404 1405 out: 1406 spin_unlock_irqrestore(&serial->serial_lock, flags); 1407 1408 hso_kick_transmit(serial); 1409 /* done */ 1410 return tx_bytes; 1411 } 1412 1413 /* how much room is there for writing */ 1414 static int hso_serial_write_room(struct tty_struct *tty) 1415 { 1416 struct hso_serial *serial = get_serial_by_tty(tty); 1417 int room; 1418 unsigned long flags; 1419 1420 spin_lock_irqsave(&serial->serial_lock, flags); 1421 room = serial->tx_data_length - serial->tx_buffer_count; 1422 spin_unlock_irqrestore(&serial->serial_lock, flags); 1423 1424 /* return free room */ 1425 return room; 1426 } 1427 1428 /* setup the term */ 1429 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old) 1430 { 1431 struct hso_serial *serial = get_serial_by_tty(tty); 1432 unsigned long flags; 1433 1434 if (old) 1435 D5("Termios called with: cflags new[%d] - old[%d]", 1436 tty->termios->c_cflag, old->c_cflag); 1437 1438 /* the actual setup */ 1439 spin_lock_irqsave(&serial->serial_lock, flags); 1440 if (serial->open_count) 1441 _hso_serial_set_termios(tty, old); 1442 else 1443 tty->termios = old; 1444 spin_unlock_irqrestore(&serial->serial_lock, flags); 1445 1446 /* done */ 1447 return; 1448 } 1449 1450 /* how many characters in the buffer */ 1451 static int hso_serial_chars_in_buffer(struct tty_struct *tty) 1452 { 1453 struct hso_serial *serial = get_serial_by_tty(tty); 1454 int chars; 1455 unsigned long flags; 1456 1457 /* sanity check */ 1458 if (serial == NULL) 1459 return 0; 1460 1461 spin_lock_irqsave(&serial->serial_lock, flags); 1462 chars = serial->tx_buffer_count; 1463 spin_unlock_irqrestore(&serial->serial_lock, flags); 1464 1465 return chars; 1466 } 1467 static int tiocmget_submit_urb(struct hso_serial *serial, 1468 struct hso_tiocmget *tiocmget, 1469 struct usb_device *usb) 1470 { 1471 int result; 1472 1473 if (serial->parent->usb_gone) 1474 return -ENODEV; 1475 usb_fill_int_urb(tiocmget->urb, usb, 1476 usb_rcvintpipe(usb, 1477 tiocmget->endp-> 1478 bEndpointAddress & 0x7F), 1479 &tiocmget->serial_state_notification, 1480 sizeof(struct hso_serial_state_notification), 1481 tiocmget_intr_callback, serial, 1482 tiocmget->endp->bInterval); 1483 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC); 1484 if (result) { 1485 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__, 1486 result); 1487 } 1488 return result; 1489 1490 } 1491 1492 static void tiocmget_intr_callback(struct urb *urb) 1493 { 1494 struct hso_serial *serial = urb->context; 1495 struct hso_tiocmget *tiocmget; 1496 int status = urb->status; 1497 u16 UART_state_bitmap, prev_UART_state_bitmap; 1498 struct uart_icount *icount; 1499 struct hso_serial_state_notification *serial_state_notification; 1500 struct usb_device *usb; 1501 1502 /* Sanity checks */ 1503 if (!serial) 1504 return; 1505 if (status) { 1506 log_usb_status(status, __func__); 1507 return; 1508 } 1509 tiocmget = serial->tiocmget; 1510 if (!tiocmget) 1511 return; 1512 usb = serial->parent->usb; 1513 serial_state_notification = &tiocmget->serial_state_notification; 1514 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE || 1515 serial_state_notification->bNotification != B_NOTIFICATION || 1516 le16_to_cpu(serial_state_notification->wValue) != W_VALUE || 1517 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX || 1518 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) { 1519 dev_warn(&usb->dev, 1520 "hso received invalid serial state notification\n"); 1521 DUMP(serial_state_notification, 1522 sizeof(hso_serial_state_notifation)) 1523 } else { 1524 1525 UART_state_bitmap = le16_to_cpu(serial_state_notification-> 1526 UART_state_bitmap); 1527 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap; 1528 icount = &tiocmget->icount; 1529 spin_lock(&serial->serial_lock); 1530 if ((UART_state_bitmap & B_OVERRUN) != 1531 (prev_UART_state_bitmap & B_OVERRUN)) 1532 icount->parity++; 1533 if ((UART_state_bitmap & B_PARITY) != 1534 (prev_UART_state_bitmap & B_PARITY)) 1535 icount->parity++; 1536 if ((UART_state_bitmap & B_FRAMING) != 1537 (prev_UART_state_bitmap & B_FRAMING)) 1538 icount->frame++; 1539 if ((UART_state_bitmap & B_RING_SIGNAL) && 1540 !(prev_UART_state_bitmap & B_RING_SIGNAL)) 1541 icount->rng++; 1542 if ((UART_state_bitmap & B_BREAK) != 1543 (prev_UART_state_bitmap & B_BREAK)) 1544 icount->brk++; 1545 if ((UART_state_bitmap & B_TX_CARRIER) != 1546 (prev_UART_state_bitmap & B_TX_CARRIER)) 1547 icount->dsr++; 1548 if ((UART_state_bitmap & B_RX_CARRIER) != 1549 (prev_UART_state_bitmap & B_RX_CARRIER)) 1550 icount->dcd++; 1551 tiocmget->prev_UART_state_bitmap = UART_state_bitmap; 1552 spin_unlock(&serial->serial_lock); 1553 tiocmget->intr_completed = 1; 1554 wake_up_interruptible(&tiocmget->waitq); 1555 } 1556 memset(serial_state_notification, 0, 1557 sizeof(struct hso_serial_state_notification)); 1558 tiocmget_submit_urb(serial, 1559 tiocmget, 1560 serial->parent->usb); 1561 } 1562 1563 /* 1564 * next few functions largely stolen from drivers/serial/serial_core.c 1565 */ 1566 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1567 * - mask passed in arg for lines of interest 1568 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1569 * Caller should use TIOCGICOUNT to see which one it was 1570 */ 1571 static int 1572 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg) 1573 { 1574 DECLARE_WAITQUEUE(wait, current); 1575 struct uart_icount cprev, cnow; 1576 struct hso_tiocmget *tiocmget; 1577 int ret; 1578 1579 tiocmget = serial->tiocmget; 1580 if (!tiocmget) 1581 return -ENOENT; 1582 /* 1583 * note the counters on entry 1584 */ 1585 spin_lock_irq(&serial->serial_lock); 1586 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount)); 1587 spin_unlock_irq(&serial->serial_lock); 1588 add_wait_queue(&tiocmget->waitq, &wait); 1589 for (;;) { 1590 spin_lock_irq(&serial->serial_lock); 1591 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount)); 1592 spin_unlock_irq(&serial->serial_lock); 1593 set_current_state(TASK_INTERRUPTIBLE); 1594 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1595 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1596 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) { 1597 ret = 0; 1598 break; 1599 } 1600 schedule(); 1601 /* see if a signal did it */ 1602 if (signal_pending(current)) { 1603 ret = -ERESTARTSYS; 1604 break; 1605 } 1606 cprev = cnow; 1607 } 1608 current->state = TASK_RUNNING; 1609 remove_wait_queue(&tiocmget->waitq, &wait); 1610 1611 return ret; 1612 } 1613 1614 /* 1615 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1616 * Return: write counters to the user passed counter struct 1617 * NB: both 1->0 and 0->1 transitions are counted except for 1618 * RI where only 0->1 is counted. 1619 */ 1620 static int hso_get_count(struct hso_serial *serial, 1621 struct serial_icounter_struct __user *icnt) 1622 { 1623 struct serial_icounter_struct icount; 1624 struct uart_icount cnow; 1625 struct hso_tiocmget *tiocmget = serial->tiocmget; 1626 1627 if (!tiocmget) 1628 return -ENOENT; 1629 spin_lock_irq(&serial->serial_lock); 1630 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount)); 1631 spin_unlock_irq(&serial->serial_lock); 1632 1633 icount.cts = cnow.cts; 1634 icount.dsr = cnow.dsr; 1635 icount.rng = cnow.rng; 1636 icount.dcd = cnow.dcd; 1637 icount.rx = cnow.rx; 1638 icount.tx = cnow.tx; 1639 icount.frame = cnow.frame; 1640 icount.overrun = cnow.overrun; 1641 icount.parity = cnow.parity; 1642 icount.brk = cnow.brk; 1643 icount.buf_overrun = cnow.buf_overrun; 1644 1645 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0; 1646 } 1647 1648 1649 static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file) 1650 { 1651 int retval; 1652 struct hso_serial *serial = get_serial_by_tty(tty); 1653 struct hso_tiocmget *tiocmget; 1654 u16 UART_state_bitmap; 1655 1656 /* sanity check */ 1657 if (!serial) { 1658 D1("no tty structures"); 1659 return -EINVAL; 1660 } 1661 spin_lock_irq(&serial->serial_lock); 1662 retval = ((serial->rts_state) ? TIOCM_RTS : 0) | 1663 ((serial->dtr_state) ? TIOCM_DTR : 0); 1664 tiocmget = serial->tiocmget; 1665 if (tiocmget) { 1666 1667 UART_state_bitmap = le16_to_cpu( 1668 tiocmget->prev_UART_state_bitmap); 1669 if (UART_state_bitmap & B_RING_SIGNAL) 1670 retval |= TIOCM_RNG; 1671 if (UART_state_bitmap & B_RX_CARRIER) 1672 retval |= TIOCM_CD; 1673 if (UART_state_bitmap & B_TX_CARRIER) 1674 retval |= TIOCM_DSR; 1675 } 1676 spin_unlock_irq(&serial->serial_lock); 1677 return retval; 1678 } 1679 1680 static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file, 1681 unsigned int set, unsigned int clear) 1682 { 1683 int val = 0; 1684 unsigned long flags; 1685 int if_num; 1686 struct hso_serial *serial = get_serial_by_tty(tty); 1687 1688 /* sanity check */ 1689 if (!serial) { 1690 D1("no tty structures"); 1691 return -EINVAL; 1692 } 1693 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber; 1694 1695 spin_lock_irqsave(&serial->serial_lock, flags); 1696 if (set & TIOCM_RTS) 1697 serial->rts_state = 1; 1698 if (set & TIOCM_DTR) 1699 serial->dtr_state = 1; 1700 1701 if (clear & TIOCM_RTS) 1702 serial->rts_state = 0; 1703 if (clear & TIOCM_DTR) 1704 serial->dtr_state = 0; 1705 1706 if (serial->dtr_state) 1707 val |= 0x01; 1708 if (serial->rts_state) 1709 val |= 0x02; 1710 1711 spin_unlock_irqrestore(&serial->serial_lock, flags); 1712 1713 return usb_control_msg(serial->parent->usb, 1714 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22, 1715 0x21, val, if_num, NULL, 0, 1716 USB_CTRL_SET_TIMEOUT); 1717 } 1718 1719 static int hso_serial_ioctl(struct tty_struct *tty, struct file *file, 1720 unsigned int cmd, unsigned long arg) 1721 { 1722 struct hso_serial *serial = get_serial_by_tty(tty); 1723 void __user *uarg = (void __user *)arg; 1724 int ret = 0; 1725 D4("IOCTL cmd: %d, arg: %ld", cmd, arg); 1726 1727 if (!serial) 1728 return -ENODEV; 1729 switch (cmd) { 1730 case TIOCMIWAIT: 1731 ret = hso_wait_modem_status(serial, arg); 1732 break; 1733 1734 case TIOCGICOUNT: 1735 ret = hso_get_count(serial, uarg); 1736 break; 1737 default: 1738 ret = -ENOIOCTLCMD; 1739 break; 1740 } 1741 return ret; 1742 } 1743 1744 1745 /* starts a transmit */ 1746 static void hso_kick_transmit(struct hso_serial *serial) 1747 { 1748 u8 *temp; 1749 unsigned long flags; 1750 int res; 1751 1752 spin_lock_irqsave(&serial->serial_lock, flags); 1753 if (!serial->tx_buffer_count) 1754 goto out; 1755 1756 if (serial->tx_urb_used) 1757 goto out; 1758 1759 /* Wakeup USB interface if necessary */ 1760 if (hso_get_activity(serial->parent) == -EAGAIN) 1761 goto out; 1762 1763 /* Switch pointers around to avoid memcpy */ 1764 temp = serial->tx_buffer; 1765 serial->tx_buffer = serial->tx_data; 1766 serial->tx_data = temp; 1767 serial->tx_data_count = serial->tx_buffer_count; 1768 serial->tx_buffer_count = 0; 1769 1770 /* If temp is set, it means we switched buffers */ 1771 if (temp && serial->write_data) { 1772 res = serial->write_data(serial); 1773 if (res >= 0) 1774 serial->tx_urb_used = 1; 1775 } 1776 out: 1777 spin_unlock_irqrestore(&serial->serial_lock, flags); 1778 } 1779 1780 /* make a request (for reading and writing data to muxed serial port) */ 1781 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port, 1782 struct urb *ctrl_urb, 1783 struct usb_ctrlrequest *ctrl_req, 1784 u8 *ctrl_urb_data, u32 size) 1785 { 1786 int result; 1787 int pipe; 1788 1789 /* Sanity check */ 1790 if (!serial || !ctrl_urb || !ctrl_req) { 1791 printk(KERN_ERR "%s: Wrong arguments\n", __func__); 1792 return -EINVAL; 1793 } 1794 1795 /* initialize */ 1796 ctrl_req->wValue = 0; 1797 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port)); 1798 ctrl_req->wLength = cpu_to_le16(size); 1799 1800 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) { 1801 /* Reading command */ 1802 ctrl_req->bRequestType = USB_DIR_IN | 1803 USB_TYPE_OPTION_VENDOR | 1804 USB_RECIP_INTERFACE; 1805 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 1806 pipe = usb_rcvctrlpipe(serial->parent->usb, 0); 1807 } else { 1808 /* Writing command */ 1809 ctrl_req->bRequestType = USB_DIR_OUT | 1810 USB_TYPE_OPTION_VENDOR | 1811 USB_RECIP_INTERFACE; 1812 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; 1813 pipe = usb_sndctrlpipe(serial->parent->usb, 0); 1814 } 1815 /* syslog */ 1816 D2("%s command (%02x) len: %d, port: %d", 1817 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write", 1818 ctrl_req->bRequestType, ctrl_req->wLength, port); 1819 1820 /* Load ctrl urb */ 1821 ctrl_urb->transfer_flags = 0; 1822 usb_fill_control_urb(ctrl_urb, 1823 serial->parent->usb, 1824 pipe, 1825 (u8 *) ctrl_req, 1826 ctrl_urb_data, size, ctrl_callback, serial); 1827 /* Send it on merry way */ 1828 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC); 1829 if (result) { 1830 dev_err(&ctrl_urb->dev->dev, 1831 "%s failed submit ctrl_urb %d type %d", __func__, 1832 result, type); 1833 return result; 1834 } 1835 1836 /* done */ 1837 return size; 1838 } 1839 1840 /* called by intr_callback when read occurs */ 1841 static int hso_mux_serial_read(struct hso_serial *serial) 1842 { 1843 if (!serial) 1844 return -EINVAL; 1845 1846 /* clean data */ 1847 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE); 1848 /* make the request */ 1849 1850 if (serial->num_rx_urbs != 1) { 1851 dev_err(&serial->parent->interface->dev, 1852 "ERROR: mux'd reads with multiple buffers " 1853 "not possible\n"); 1854 return 0; 1855 } 1856 return mux_device_request(serial, 1857 USB_CDC_GET_ENCAPSULATED_RESPONSE, 1858 serial->parent->port_spec & HSO_PORT_MASK, 1859 serial->rx_urb[0], 1860 &serial->ctrl_req_rx, 1861 serial->rx_data[0], serial->rx_data_length); 1862 } 1863 1864 /* used for muxed serial port callback (muxed serial read) */ 1865 static void intr_callback(struct urb *urb) 1866 { 1867 struct hso_shared_int *shared_int = urb->context; 1868 struct hso_serial *serial; 1869 unsigned char *port_req; 1870 int status = urb->status; 1871 int i; 1872 1873 usb_mark_last_busy(urb->dev); 1874 1875 /* sanity check */ 1876 if (!shared_int) 1877 return; 1878 1879 /* status check */ 1880 if (status) { 1881 log_usb_status(status, __func__); 1882 return; 1883 } 1884 D4("\n--- Got intr callback 0x%02X ---", status); 1885 1886 /* what request? */ 1887 port_req = urb->transfer_buffer; 1888 D4(" port_req = 0x%.2X\n", *port_req); 1889 /* loop over all muxed ports to find the one sending this */ 1890 for (i = 0; i < 8; i++) { 1891 /* max 8 channels on MUX */ 1892 if (*port_req & (1 << i)) { 1893 serial = get_serial_by_shared_int_and_type(shared_int, 1894 (1 << i)); 1895 if (serial != NULL) { 1896 D1("Pending read interrupt on port %d\n", i); 1897 spin_lock(&serial->serial_lock); 1898 if (serial->rx_state == RX_IDLE) { 1899 /* Setup and send a ctrl req read on 1900 * port i */ 1901 if (!serial->rx_urb_filled[0]) { 1902 serial->rx_state = RX_SENT; 1903 hso_mux_serial_read(serial); 1904 } else 1905 serial->rx_state = RX_PENDING; 1906 1907 } else { 1908 D1("Already pending a read on " 1909 "port %d\n", i); 1910 } 1911 spin_unlock(&serial->serial_lock); 1912 } 1913 } 1914 } 1915 /* Resubmit interrupt urb */ 1916 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC); 1917 } 1918 1919 /* called for writing to muxed serial port */ 1920 static int hso_mux_serial_write_data(struct hso_serial *serial) 1921 { 1922 if (NULL == serial) 1923 return -EINVAL; 1924 1925 return mux_device_request(serial, 1926 USB_CDC_SEND_ENCAPSULATED_COMMAND, 1927 serial->parent->port_spec & HSO_PORT_MASK, 1928 serial->tx_urb, 1929 &serial->ctrl_req_tx, 1930 serial->tx_data, serial->tx_data_count); 1931 } 1932 1933 /* write callback for Diag and CS port */ 1934 static void hso_std_serial_write_bulk_callback(struct urb *urb) 1935 { 1936 struct hso_serial *serial = urb->context; 1937 int status = urb->status; 1938 struct tty_struct *tty; 1939 1940 /* sanity check */ 1941 if (!serial) { 1942 D1("serial == NULL"); 1943 return; 1944 } 1945 1946 spin_lock(&serial->serial_lock); 1947 serial->tx_urb_used = 0; 1948 tty = tty_kref_get(serial->tty); 1949 spin_unlock(&serial->serial_lock); 1950 if (status) { 1951 log_usb_status(status, __func__); 1952 tty_kref_put(tty); 1953 return; 1954 } 1955 hso_put_activity(serial->parent); 1956 if (tty) { 1957 tty_wakeup(tty); 1958 tty_kref_put(tty); 1959 } 1960 hso_kick_transmit(serial); 1961 1962 D1(" "); 1963 return; 1964 } 1965 1966 /* called for writing diag or CS serial port */ 1967 static int hso_std_serial_write_data(struct hso_serial *serial) 1968 { 1969 int count = serial->tx_data_count; 1970 int result; 1971 1972 usb_fill_bulk_urb(serial->tx_urb, 1973 serial->parent->usb, 1974 usb_sndbulkpipe(serial->parent->usb, 1975 serial->out_endp-> 1976 bEndpointAddress & 0x7F), 1977 serial->tx_data, serial->tx_data_count, 1978 hso_std_serial_write_bulk_callback, serial); 1979 1980 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC); 1981 if (result) { 1982 dev_warn(&serial->parent->usb->dev, 1983 "Failed to submit urb - res %d\n", result); 1984 return result; 1985 } 1986 1987 return count; 1988 } 1989 1990 /* callback after read or write on muxed serial port */ 1991 static void ctrl_callback(struct urb *urb) 1992 { 1993 struct hso_serial *serial = urb->context; 1994 struct usb_ctrlrequest *req; 1995 int status = urb->status; 1996 struct tty_struct *tty; 1997 1998 /* sanity check */ 1999 if (!serial) 2000 return; 2001 2002 spin_lock(&serial->serial_lock); 2003 serial->tx_urb_used = 0; 2004 tty = tty_kref_get(serial->tty); 2005 spin_unlock(&serial->serial_lock); 2006 if (status) { 2007 log_usb_status(status, __func__); 2008 tty_kref_put(tty); 2009 return; 2010 } 2011 2012 /* what request? */ 2013 req = (struct usb_ctrlrequest *)(urb->setup_packet); 2014 D4("\n--- Got muxed ctrl callback 0x%02X ---", status); 2015 D4("Actual length of urb = %d\n", urb->actual_length); 2016 DUMP1(urb->transfer_buffer, urb->actual_length); 2017 2018 if (req->bRequestType == 2019 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) { 2020 /* response to a read command */ 2021 serial->rx_urb_filled[0] = 1; 2022 spin_lock(&serial->serial_lock); 2023 put_rxbuf_data_and_resubmit_ctrl_urb(serial); 2024 spin_unlock(&serial->serial_lock); 2025 } else { 2026 hso_put_activity(serial->parent); 2027 if (tty) 2028 tty_wakeup(tty); 2029 /* response to a write command */ 2030 hso_kick_transmit(serial); 2031 } 2032 tty_kref_put(tty); 2033 } 2034 2035 /* handle RX data for serial port */ 2036 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial) 2037 { 2038 struct tty_struct *tty; 2039 int write_length_remaining = 0; 2040 int curr_write_len; 2041 2042 /* Sanity check */ 2043 if (urb == NULL || serial == NULL) { 2044 D1("serial = NULL"); 2045 return -2; 2046 } 2047 2048 /* All callers to put_rxbuf_data hold serial_lock */ 2049 tty = tty_kref_get(serial->tty); 2050 2051 /* Push data to tty */ 2052 if (tty) { 2053 write_length_remaining = urb->actual_length - 2054 serial->curr_rx_urb_offset; 2055 D1("data to push to tty"); 2056 while (write_length_remaining) { 2057 if (test_bit(TTY_THROTTLED, &tty->flags)) { 2058 tty_kref_put(tty); 2059 return -1; 2060 } 2061 curr_write_len = tty_insert_flip_string 2062 (tty, urb->transfer_buffer + 2063 serial->curr_rx_urb_offset, 2064 write_length_remaining); 2065 serial->curr_rx_urb_offset += curr_write_len; 2066 write_length_remaining -= curr_write_len; 2067 tty_flip_buffer_push(tty); 2068 } 2069 } 2070 if (write_length_remaining == 0) { 2071 serial->curr_rx_urb_offset = 0; 2072 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0; 2073 } 2074 tty_kref_put(tty); 2075 return write_length_remaining; 2076 } 2077 2078 2079 /* Base driver functions */ 2080 2081 static void hso_log_port(struct hso_device *hso_dev) 2082 { 2083 char *port_type; 2084 char port_dev[20]; 2085 2086 switch (hso_dev->port_spec & HSO_PORT_MASK) { 2087 case HSO_PORT_CONTROL: 2088 port_type = "Control"; 2089 break; 2090 case HSO_PORT_APP: 2091 port_type = "Application"; 2092 break; 2093 case HSO_PORT_GPS: 2094 port_type = "GPS"; 2095 break; 2096 case HSO_PORT_GPS_CONTROL: 2097 port_type = "GPS control"; 2098 break; 2099 case HSO_PORT_APP2: 2100 port_type = "Application2"; 2101 break; 2102 case HSO_PORT_PCSC: 2103 port_type = "PCSC"; 2104 break; 2105 case HSO_PORT_DIAG: 2106 port_type = "Diagnostic"; 2107 break; 2108 case HSO_PORT_DIAG2: 2109 port_type = "Diagnostic2"; 2110 break; 2111 case HSO_PORT_MODEM: 2112 port_type = "Modem"; 2113 break; 2114 case HSO_PORT_NETWORK: 2115 port_type = "Network"; 2116 break; 2117 default: 2118 port_type = "Unknown"; 2119 break; 2120 } 2121 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) { 2122 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name); 2123 } else 2124 sprintf(port_dev, "/dev/%s%d", tty_filename, 2125 dev2ser(hso_dev)->minor); 2126 2127 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n", 2128 port_type, port_dev); 2129 } 2130 2131 static int hso_start_net_device(struct hso_device *hso_dev) 2132 { 2133 int i, result = 0; 2134 struct hso_net *hso_net = dev2net(hso_dev); 2135 2136 if (!hso_net) 2137 return -ENODEV; 2138 2139 /* send URBs for all read buffers */ 2140 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2141 2142 /* Prep a receive URB */ 2143 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i], 2144 hso_dev->usb, 2145 usb_rcvbulkpipe(hso_dev->usb, 2146 hso_net->in_endp-> 2147 bEndpointAddress & 0x7F), 2148 hso_net->mux_bulk_rx_buf_pool[i], 2149 MUX_BULK_RX_BUF_SIZE, read_bulk_callback, 2150 hso_net); 2151 2152 /* Put it out there so the device can send us stuff */ 2153 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i], 2154 GFP_NOIO); 2155 if (result) 2156 dev_warn(&hso_dev->usb->dev, 2157 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__, 2158 i, result); 2159 } 2160 2161 return result; 2162 } 2163 2164 static int hso_stop_net_device(struct hso_device *hso_dev) 2165 { 2166 int i; 2167 struct hso_net *hso_net = dev2net(hso_dev); 2168 2169 if (!hso_net) 2170 return -ENODEV; 2171 2172 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2173 if (hso_net->mux_bulk_rx_urb_pool[i]) 2174 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]); 2175 2176 } 2177 if (hso_net->mux_bulk_tx_urb) 2178 usb_kill_urb(hso_net->mux_bulk_tx_urb); 2179 2180 return 0; 2181 } 2182 2183 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags) 2184 { 2185 int i, result = 0; 2186 struct hso_serial *serial = dev2ser(hso_dev); 2187 2188 if (!serial) 2189 return -ENODEV; 2190 2191 /* If it is not the MUX port fill in and submit a bulk urb (already 2192 * allocated in hso_serial_start) */ 2193 if (!(serial->parent->port_spec & HSO_INTF_MUX)) { 2194 for (i = 0; i < serial->num_rx_urbs; i++) { 2195 usb_fill_bulk_urb(serial->rx_urb[i], 2196 serial->parent->usb, 2197 usb_rcvbulkpipe(serial->parent->usb, 2198 serial->in_endp-> 2199 bEndpointAddress & 2200 0x7F), 2201 serial->rx_data[i], 2202 serial->rx_data_length, 2203 hso_std_serial_read_bulk_callback, 2204 serial); 2205 result = usb_submit_urb(serial->rx_urb[i], flags); 2206 if (result) { 2207 dev_warn(&serial->parent->usb->dev, 2208 "Failed to submit urb - res %d\n", 2209 result); 2210 break; 2211 } 2212 } 2213 } else { 2214 mutex_lock(&serial->shared_int->shared_int_lock); 2215 if (!serial->shared_int->use_count) { 2216 result = 2217 hso_mux_submit_intr_urb(serial->shared_int, 2218 hso_dev->usb, flags); 2219 } 2220 serial->shared_int->use_count++; 2221 mutex_unlock(&serial->shared_int->shared_int_lock); 2222 } 2223 if (serial->tiocmget) 2224 tiocmget_submit_urb(serial, 2225 serial->tiocmget, 2226 serial->parent->usb); 2227 return result; 2228 } 2229 2230 static int hso_stop_serial_device(struct hso_device *hso_dev) 2231 { 2232 int i; 2233 struct hso_serial *serial = dev2ser(hso_dev); 2234 struct hso_tiocmget *tiocmget; 2235 2236 if (!serial) 2237 return -ENODEV; 2238 2239 for (i = 0; i < serial->num_rx_urbs; i++) { 2240 if (serial->rx_urb[i]) { 2241 usb_kill_urb(serial->rx_urb[i]); 2242 serial->rx_urb_filled[i] = 0; 2243 } 2244 } 2245 serial->curr_rx_urb_idx = 0; 2246 serial->curr_rx_urb_offset = 0; 2247 2248 if (serial->tx_urb) 2249 usb_kill_urb(serial->tx_urb); 2250 2251 if (serial->shared_int) { 2252 mutex_lock(&serial->shared_int->shared_int_lock); 2253 if (serial->shared_int->use_count && 2254 (--serial->shared_int->use_count == 0)) { 2255 struct urb *urb; 2256 2257 urb = serial->shared_int->shared_intr_urb; 2258 if (urb) 2259 usb_kill_urb(urb); 2260 } 2261 mutex_unlock(&serial->shared_int->shared_int_lock); 2262 } 2263 tiocmget = serial->tiocmget; 2264 if (tiocmget) { 2265 wake_up_interruptible(&tiocmget->waitq); 2266 usb_kill_urb(tiocmget->urb); 2267 } 2268 2269 return 0; 2270 } 2271 2272 static void hso_serial_common_free(struct hso_serial *serial) 2273 { 2274 int i; 2275 2276 if (serial->parent->dev) 2277 device_remove_file(serial->parent->dev, &dev_attr_hsotype); 2278 2279 tty_unregister_device(tty_drv, serial->minor); 2280 2281 for (i = 0; i < serial->num_rx_urbs; i++) { 2282 /* unlink and free RX URB */ 2283 usb_free_urb(serial->rx_urb[i]); 2284 /* free the RX buffer */ 2285 kfree(serial->rx_data[i]); 2286 } 2287 2288 /* unlink and free TX URB */ 2289 usb_free_urb(serial->tx_urb); 2290 kfree(serial->tx_data); 2291 } 2292 2293 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs, 2294 int rx_size, int tx_size) 2295 { 2296 struct device *dev; 2297 int minor; 2298 int i; 2299 2300 minor = get_free_serial_index(); 2301 if (minor < 0) 2302 goto exit; 2303 2304 /* register our minor number */ 2305 serial->parent->dev = tty_register_device(tty_drv, minor, 2306 &serial->parent->interface->dev); 2307 dev = serial->parent->dev; 2308 dev->driver_data = serial->parent; 2309 i = device_create_file(dev, &dev_attr_hsotype); 2310 2311 /* fill in specific data for later use */ 2312 serial->minor = minor; 2313 serial->magic = HSO_SERIAL_MAGIC; 2314 spin_lock_init(&serial->serial_lock); 2315 serial->num_rx_urbs = num_urbs; 2316 2317 /* RX, allocate urb and initialize */ 2318 2319 /* prepare our RX buffer */ 2320 serial->rx_data_length = rx_size; 2321 for (i = 0; i < serial->num_rx_urbs; i++) { 2322 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL); 2323 if (!serial->rx_urb[i]) { 2324 dev_err(dev, "Could not allocate urb?\n"); 2325 goto exit; 2326 } 2327 serial->rx_urb[i]->transfer_buffer = NULL; 2328 serial->rx_urb[i]->transfer_buffer_length = 0; 2329 serial->rx_data[i] = kzalloc(serial->rx_data_length, 2330 GFP_KERNEL); 2331 if (!serial->rx_data[i]) { 2332 dev_err(dev, "%s - Out of memory\n", __func__); 2333 goto exit; 2334 } 2335 } 2336 2337 /* TX, allocate urb and initialize */ 2338 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL); 2339 if (!serial->tx_urb) { 2340 dev_err(dev, "Could not allocate urb?\n"); 2341 goto exit; 2342 } 2343 serial->tx_urb->transfer_buffer = NULL; 2344 serial->tx_urb->transfer_buffer_length = 0; 2345 /* prepare our TX buffer */ 2346 serial->tx_data_count = 0; 2347 serial->tx_buffer_count = 0; 2348 serial->tx_data_length = tx_size; 2349 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL); 2350 if (!serial->tx_data) { 2351 dev_err(dev, "%s - Out of memory", __func__); 2352 goto exit; 2353 } 2354 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL); 2355 if (!serial->tx_buffer) { 2356 dev_err(dev, "%s - Out of memory", __func__); 2357 goto exit; 2358 } 2359 2360 return 0; 2361 exit: 2362 hso_serial_common_free(serial); 2363 return -1; 2364 } 2365 2366 /* Creates a general hso device */ 2367 static struct hso_device *hso_create_device(struct usb_interface *intf, 2368 int port_spec) 2369 { 2370 struct hso_device *hso_dev; 2371 2372 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC); 2373 if (!hso_dev) 2374 return NULL; 2375 2376 hso_dev->port_spec = port_spec; 2377 hso_dev->usb = interface_to_usbdev(intf); 2378 hso_dev->interface = intf; 2379 kref_init(&hso_dev->ref); 2380 mutex_init(&hso_dev->mutex); 2381 2382 INIT_WORK(&hso_dev->async_get_intf, async_get_intf); 2383 INIT_WORK(&hso_dev->async_put_intf, async_put_intf); 2384 2385 return hso_dev; 2386 } 2387 2388 /* Removes a network device in the network device table */ 2389 static int remove_net_device(struct hso_device *hso_dev) 2390 { 2391 int i; 2392 2393 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 2394 if (network_table[i] == hso_dev) { 2395 network_table[i] = NULL; 2396 break; 2397 } 2398 } 2399 if (i == HSO_MAX_NET_DEVICES) 2400 return -1; 2401 return 0; 2402 } 2403 2404 /* Frees our network device */ 2405 static void hso_free_net_device(struct hso_device *hso_dev) 2406 { 2407 int i; 2408 struct hso_net *hso_net = dev2net(hso_dev); 2409 2410 if (!hso_net) 2411 return; 2412 2413 /* start freeing */ 2414 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2415 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]); 2416 kfree(hso_net->mux_bulk_rx_buf_pool[i]); 2417 } 2418 usb_free_urb(hso_net->mux_bulk_tx_urb); 2419 kfree(hso_net->mux_bulk_tx_buf); 2420 2421 remove_net_device(hso_net->parent); 2422 2423 if (hso_net->net) { 2424 unregister_netdev(hso_net->net); 2425 free_netdev(hso_net->net); 2426 } 2427 2428 kfree(hso_dev); 2429 } 2430 2431 static const struct net_device_ops hso_netdev_ops = { 2432 .ndo_open = hso_net_open, 2433 .ndo_stop = hso_net_close, 2434 .ndo_start_xmit = hso_net_start_xmit, 2435 .ndo_tx_timeout = hso_net_tx_timeout, 2436 }; 2437 2438 /* initialize the network interface */ 2439 static void hso_net_init(struct net_device *net) 2440 { 2441 struct hso_net *hso_net = netdev_priv(net); 2442 2443 D1("sizeof hso_net is %d", (int)sizeof(*hso_net)); 2444 2445 /* fill in the other fields */ 2446 net->netdev_ops = &hso_netdev_ops; 2447 net->watchdog_timeo = HSO_NET_TX_TIMEOUT; 2448 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 2449 net->type = ARPHRD_NONE; 2450 net->mtu = DEFAULT_MTU - 14; 2451 net->tx_queue_len = 10; 2452 SET_ETHTOOL_OPS(net, &ops); 2453 2454 /* and initialize the semaphore */ 2455 spin_lock_init(&hso_net->net_lock); 2456 } 2457 2458 /* Adds a network device in the network device table */ 2459 static int add_net_device(struct hso_device *hso_dev) 2460 { 2461 int i; 2462 2463 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 2464 if (network_table[i] == NULL) { 2465 network_table[i] = hso_dev; 2466 break; 2467 } 2468 } 2469 if (i == HSO_MAX_NET_DEVICES) 2470 return -1; 2471 return 0; 2472 } 2473 2474 static int hso_radio_toggle(void *data, enum rfkill_state state) 2475 { 2476 struct hso_device *hso_dev = data; 2477 int enabled = (state == RFKILL_STATE_ON); 2478 int rv; 2479 2480 mutex_lock(&hso_dev->mutex); 2481 if (hso_dev->usb_gone) 2482 rv = 0; 2483 else 2484 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0), 2485 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0, 2486 USB_CTRL_SET_TIMEOUT); 2487 mutex_unlock(&hso_dev->mutex); 2488 return rv; 2489 } 2490 2491 /* Creates and sets up everything for rfkill */ 2492 static void hso_create_rfkill(struct hso_device *hso_dev, 2493 struct usb_interface *interface) 2494 { 2495 struct hso_net *hso_net = dev2net(hso_dev); 2496 struct device *dev = &hso_net->net->dev; 2497 char *rfkn; 2498 2499 hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev, 2500 RFKILL_TYPE_WWAN); 2501 if (!hso_net->rfkill) { 2502 dev_err(dev, "%s - Out of memory\n", __func__); 2503 return; 2504 } 2505 rfkn = kzalloc(20, GFP_KERNEL); 2506 if (!rfkn) { 2507 rfkill_free(hso_net->rfkill); 2508 hso_net->rfkill = NULL; 2509 dev_err(dev, "%s - Out of memory\n", __func__); 2510 return; 2511 } 2512 snprintf(rfkn, 20, "hso-%d", 2513 interface->altsetting->desc.bInterfaceNumber); 2514 hso_net->rfkill->name = rfkn; 2515 hso_net->rfkill->state = RFKILL_STATE_ON; 2516 hso_net->rfkill->data = hso_dev; 2517 hso_net->rfkill->toggle_radio = hso_radio_toggle; 2518 if (rfkill_register(hso_net->rfkill) < 0) { 2519 kfree(rfkn); 2520 hso_net->rfkill->name = NULL; 2521 rfkill_free(hso_net->rfkill); 2522 hso_net->rfkill = NULL; 2523 dev_err(dev, "%s - Failed to register rfkill\n", __func__); 2524 return; 2525 } 2526 } 2527 2528 /* Creates our network device */ 2529 static struct hso_device *hso_create_net_device(struct usb_interface *interface) 2530 { 2531 int result, i; 2532 struct net_device *net; 2533 struct hso_net *hso_net; 2534 struct hso_device *hso_dev; 2535 2536 hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK); 2537 if (!hso_dev) 2538 return NULL; 2539 2540 /* allocate our network device, then we can put in our private data */ 2541 /* call hso_net_init to do the basic initialization */ 2542 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init); 2543 if (!net) { 2544 dev_err(&interface->dev, "Unable to create ethernet device\n"); 2545 goto exit; 2546 } 2547 2548 hso_net = netdev_priv(net); 2549 2550 hso_dev->port_data.dev_net = hso_net; 2551 hso_net->net = net; 2552 hso_net->parent = hso_dev; 2553 2554 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2555 USB_DIR_IN); 2556 if (!hso_net->in_endp) { 2557 dev_err(&interface->dev, "Can't find BULK IN endpoint\n"); 2558 goto exit; 2559 } 2560 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2561 USB_DIR_OUT); 2562 if (!hso_net->out_endp) { 2563 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n"); 2564 goto exit; 2565 } 2566 SET_NETDEV_DEV(net, &interface->dev); 2567 2568 /* registering our net device */ 2569 result = register_netdev(net); 2570 if (result) { 2571 dev_err(&interface->dev, "Failed to register device\n"); 2572 goto exit; 2573 } 2574 2575 /* start allocating */ 2576 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2577 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL); 2578 if (!hso_net->mux_bulk_rx_urb_pool[i]) { 2579 dev_err(&interface->dev, "Could not allocate rx urb\n"); 2580 goto exit; 2581 } 2582 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE, 2583 GFP_KERNEL); 2584 if (!hso_net->mux_bulk_rx_buf_pool[i]) { 2585 dev_err(&interface->dev, "Could not allocate rx buf\n"); 2586 goto exit; 2587 } 2588 } 2589 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL); 2590 if (!hso_net->mux_bulk_tx_urb) { 2591 dev_err(&interface->dev, "Could not allocate tx urb\n"); 2592 goto exit; 2593 } 2594 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL); 2595 if (!hso_net->mux_bulk_tx_buf) { 2596 dev_err(&interface->dev, "Could not allocate tx buf\n"); 2597 goto exit; 2598 } 2599 2600 add_net_device(hso_dev); 2601 2602 hso_log_port(hso_dev); 2603 2604 hso_create_rfkill(hso_dev, interface); 2605 2606 return hso_dev; 2607 exit: 2608 hso_free_net_device(hso_dev); 2609 return NULL; 2610 } 2611 2612 static void hso_free_tiomget(struct hso_serial *serial) 2613 { 2614 struct hso_tiocmget *tiocmget = serial->tiocmget; 2615 if (tiocmget) { 2616 kfree(tiocmget); 2617 if (tiocmget->urb) { 2618 usb_free_urb(tiocmget->urb); 2619 tiocmget->urb = NULL; 2620 } 2621 serial->tiocmget = NULL; 2622 2623 } 2624 } 2625 2626 /* Frees an AT channel ( goes for both mux and non-mux ) */ 2627 static void hso_free_serial_device(struct hso_device *hso_dev) 2628 { 2629 struct hso_serial *serial = dev2ser(hso_dev); 2630 2631 if (!serial) 2632 return; 2633 set_serial_by_index(serial->minor, NULL); 2634 2635 hso_serial_common_free(serial); 2636 2637 if (serial->shared_int) { 2638 mutex_lock(&serial->shared_int->shared_int_lock); 2639 if (--serial->shared_int->ref_count == 0) 2640 hso_free_shared_int(serial->shared_int); 2641 else 2642 mutex_unlock(&serial->shared_int->shared_int_lock); 2643 } 2644 hso_free_tiomget(serial); 2645 kfree(serial); 2646 kfree(hso_dev); 2647 } 2648 2649 /* Creates a bulk AT channel */ 2650 static struct hso_device *hso_create_bulk_serial_device( 2651 struct usb_interface *interface, int port) 2652 { 2653 struct hso_device *hso_dev; 2654 struct hso_serial *serial; 2655 int num_urbs; 2656 struct hso_tiocmget *tiocmget; 2657 2658 hso_dev = hso_create_device(interface, port); 2659 if (!hso_dev) 2660 return NULL; 2661 2662 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2663 if (!serial) 2664 goto exit; 2665 2666 serial->parent = hso_dev; 2667 hso_dev->port_data.dev_serial = serial; 2668 2669 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) { 2670 num_urbs = 2; 2671 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget), 2672 GFP_KERNEL); 2673 /* it isn't going to break our heart if serial->tiocmget 2674 * allocation fails don't bother checking this. 2675 */ 2676 if (serial->tiocmget) { 2677 tiocmget = serial->tiocmget; 2678 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL); 2679 if (tiocmget->urb) { 2680 mutex_init(&tiocmget->mutex); 2681 init_waitqueue_head(&tiocmget->waitq); 2682 tiocmget->endp = hso_get_ep( 2683 interface, 2684 USB_ENDPOINT_XFER_INT, 2685 USB_DIR_IN); 2686 } else 2687 hso_free_tiomget(serial); 2688 } 2689 } 2690 else 2691 num_urbs = 1; 2692 2693 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE, 2694 BULK_URB_TX_SIZE)) 2695 goto exit; 2696 2697 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2698 USB_DIR_IN); 2699 if (!serial->in_endp) { 2700 dev_err(&interface->dev, "Failed to find BULK IN ep\n"); 2701 goto exit2; 2702 } 2703 2704 if (! 2705 (serial->out_endp = 2706 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) { 2707 dev_err(&interface->dev, "Failed to find BULK IN ep\n"); 2708 goto exit2; 2709 } 2710 2711 serial->write_data = hso_std_serial_write_data; 2712 2713 /* and record this serial */ 2714 set_serial_by_index(serial->minor, serial); 2715 2716 /* setup the proc dirs and files if needed */ 2717 hso_log_port(hso_dev); 2718 2719 /* done, return it */ 2720 return hso_dev; 2721 2722 exit2: 2723 hso_serial_common_free(serial); 2724 exit: 2725 hso_free_tiomget(serial); 2726 kfree(serial); 2727 kfree(hso_dev); 2728 return NULL; 2729 } 2730 2731 /* Creates a multiplexed AT channel */ 2732 static 2733 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface, 2734 int port, 2735 struct hso_shared_int *mux) 2736 { 2737 struct hso_device *hso_dev; 2738 struct hso_serial *serial; 2739 int port_spec; 2740 2741 port_spec = HSO_INTF_MUX; 2742 port_spec &= ~HSO_PORT_MASK; 2743 2744 port_spec |= hso_mux_to_port(port); 2745 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT) 2746 return NULL; 2747 2748 hso_dev = hso_create_device(interface, port_spec); 2749 if (!hso_dev) 2750 return NULL; 2751 2752 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2753 if (!serial) 2754 goto exit; 2755 2756 hso_dev->port_data.dev_serial = serial; 2757 serial->parent = hso_dev; 2758 2759 if (hso_serial_common_create 2760 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE)) 2761 goto exit; 2762 2763 serial->tx_data_length--; 2764 serial->write_data = hso_mux_serial_write_data; 2765 2766 serial->shared_int = mux; 2767 mutex_lock(&serial->shared_int->shared_int_lock); 2768 serial->shared_int->ref_count++; 2769 mutex_unlock(&serial->shared_int->shared_int_lock); 2770 2771 /* and record this serial */ 2772 set_serial_by_index(serial->minor, serial); 2773 2774 /* setup the proc dirs and files if needed */ 2775 hso_log_port(hso_dev); 2776 2777 /* done, return it */ 2778 return hso_dev; 2779 2780 exit: 2781 if (serial) { 2782 tty_unregister_device(tty_drv, serial->minor); 2783 kfree(serial); 2784 } 2785 if (hso_dev) 2786 kfree(hso_dev); 2787 return NULL; 2788 2789 } 2790 2791 static void hso_free_shared_int(struct hso_shared_int *mux) 2792 { 2793 usb_free_urb(mux->shared_intr_urb); 2794 kfree(mux->shared_intr_buf); 2795 mutex_unlock(&mux->shared_int_lock); 2796 kfree(mux); 2797 } 2798 2799 static 2800 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface) 2801 { 2802 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL); 2803 2804 if (!mux) 2805 return NULL; 2806 2807 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT, 2808 USB_DIR_IN); 2809 if (!mux->intr_endp) { 2810 dev_err(&interface->dev, "Can't find INT IN endpoint\n"); 2811 goto exit; 2812 } 2813 2814 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL); 2815 if (!mux->shared_intr_urb) { 2816 dev_err(&interface->dev, "Could not allocate intr urb?"); 2817 goto exit; 2818 } 2819 mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize, 2820 GFP_KERNEL); 2821 if (!mux->shared_intr_buf) { 2822 dev_err(&interface->dev, "Could not allocate intr buf?"); 2823 goto exit; 2824 } 2825 2826 mutex_init(&mux->shared_int_lock); 2827 2828 return mux; 2829 2830 exit: 2831 kfree(mux->shared_intr_buf); 2832 usb_free_urb(mux->shared_intr_urb); 2833 kfree(mux); 2834 return NULL; 2835 } 2836 2837 /* Gets the port spec for a certain interface */ 2838 static int hso_get_config_data(struct usb_interface *interface) 2839 { 2840 struct usb_device *usbdev = interface_to_usbdev(interface); 2841 u8 config_data[17]; 2842 u32 if_num = interface->altsetting->desc.bInterfaceNumber; 2843 s32 result; 2844 2845 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 2846 0x86, 0xC0, 0, 0, config_data, 17, 2847 USB_CTRL_SET_TIMEOUT) != 0x11) { 2848 return -EIO; 2849 } 2850 2851 switch (config_data[if_num]) { 2852 case 0x0: 2853 result = 0; 2854 break; 2855 case 0x1: 2856 result = HSO_PORT_DIAG; 2857 break; 2858 case 0x2: 2859 result = HSO_PORT_GPS; 2860 break; 2861 case 0x3: 2862 result = HSO_PORT_GPS_CONTROL; 2863 break; 2864 case 0x4: 2865 result = HSO_PORT_APP; 2866 break; 2867 case 0x5: 2868 result = HSO_PORT_APP2; 2869 break; 2870 case 0x6: 2871 result = HSO_PORT_CONTROL; 2872 break; 2873 case 0x7: 2874 result = HSO_PORT_NETWORK; 2875 break; 2876 case 0x8: 2877 result = HSO_PORT_MODEM; 2878 break; 2879 case 0x9: 2880 result = HSO_PORT_MSD; 2881 break; 2882 case 0xa: 2883 result = HSO_PORT_PCSC; 2884 break; 2885 case 0xb: 2886 result = HSO_PORT_VOICE; 2887 break; 2888 default: 2889 result = 0; 2890 } 2891 2892 if (result) 2893 result |= HSO_INTF_BULK; 2894 2895 if (config_data[16] & 0x1) 2896 result |= HSO_INFO_CRC_BUG; 2897 2898 return result; 2899 } 2900 2901 /* called once for each interface upon device insertion */ 2902 static int hso_probe(struct usb_interface *interface, 2903 const struct usb_device_id *id) 2904 { 2905 int mux, i, if_num, port_spec; 2906 unsigned char port_mask; 2907 struct hso_device *hso_dev = NULL; 2908 struct hso_shared_int *shared_int; 2909 struct hso_device *tmp_dev = NULL; 2910 2911 if_num = interface->altsetting->desc.bInterfaceNumber; 2912 2913 /* Get the interface/port specification from either driver_info or from 2914 * the device itself */ 2915 if (id->driver_info) 2916 port_spec = ((u32 *)(id->driver_info))[if_num]; 2917 else 2918 port_spec = hso_get_config_data(interface); 2919 2920 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) { 2921 dev_err(&interface->dev, "Not our interface\n"); 2922 return -ENODEV; 2923 } 2924 /* Check if we need to switch to alt interfaces prior to port 2925 * configuration */ 2926 if (interface->num_altsetting > 1) 2927 usb_set_interface(interface_to_usbdev(interface), if_num, 1); 2928 interface->needs_remote_wakeup = 1; 2929 2930 /* Allocate new hso device(s) */ 2931 switch (port_spec & HSO_INTF_MASK) { 2932 case HSO_INTF_MUX: 2933 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) { 2934 /* Create the network device */ 2935 if (!disable_net) { 2936 hso_dev = hso_create_net_device(interface); 2937 if (!hso_dev) 2938 goto exit; 2939 tmp_dev = hso_dev; 2940 } 2941 } 2942 2943 if (hso_get_mux_ports(interface, &port_mask)) 2944 /* TODO: de-allocate everything */ 2945 goto exit; 2946 2947 shared_int = hso_create_shared_int(interface); 2948 if (!shared_int) 2949 goto exit; 2950 2951 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) { 2952 if (port_mask & i) { 2953 hso_dev = hso_create_mux_serial_device( 2954 interface, i, shared_int); 2955 if (!hso_dev) 2956 goto exit; 2957 } 2958 } 2959 2960 if (tmp_dev) 2961 hso_dev = tmp_dev; 2962 break; 2963 2964 case HSO_INTF_BULK: 2965 /* It's a regular bulk interface */ 2966 if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) 2967 && !disable_net) 2968 hso_dev = hso_create_net_device(interface); 2969 else 2970 hso_dev = 2971 hso_create_bulk_serial_device(interface, port_spec); 2972 if (!hso_dev) 2973 goto exit; 2974 break; 2975 default: 2976 goto exit; 2977 } 2978 2979 /* save our data pointer in this device */ 2980 usb_set_intfdata(interface, hso_dev); 2981 2982 /* done */ 2983 return 0; 2984 exit: 2985 hso_free_interface(interface); 2986 return -ENODEV; 2987 } 2988 2989 /* device removed, cleaning up */ 2990 static void hso_disconnect(struct usb_interface *interface) 2991 { 2992 hso_free_interface(interface); 2993 2994 /* remove reference of our private data */ 2995 usb_set_intfdata(interface, NULL); 2996 } 2997 2998 static void async_get_intf(struct work_struct *data) 2999 { 3000 struct hso_device *hso_dev = 3001 container_of(data, struct hso_device, async_get_intf); 3002 usb_autopm_get_interface(hso_dev->interface); 3003 } 3004 3005 static void async_put_intf(struct work_struct *data) 3006 { 3007 struct hso_device *hso_dev = 3008 container_of(data, struct hso_device, async_put_intf); 3009 usb_autopm_put_interface(hso_dev->interface); 3010 } 3011 3012 static int hso_get_activity(struct hso_device *hso_dev) 3013 { 3014 if (hso_dev->usb->state == USB_STATE_SUSPENDED) { 3015 if (!hso_dev->is_active) { 3016 hso_dev->is_active = 1; 3017 schedule_work(&hso_dev->async_get_intf); 3018 } 3019 } 3020 3021 if (hso_dev->usb->state != USB_STATE_CONFIGURED) 3022 return -EAGAIN; 3023 3024 usb_mark_last_busy(hso_dev->usb); 3025 3026 return 0; 3027 } 3028 3029 static int hso_put_activity(struct hso_device *hso_dev) 3030 { 3031 if (hso_dev->usb->state != USB_STATE_SUSPENDED) { 3032 if (hso_dev->is_active) { 3033 hso_dev->is_active = 0; 3034 schedule_work(&hso_dev->async_put_intf); 3035 return -EAGAIN; 3036 } 3037 } 3038 hso_dev->is_active = 0; 3039 return 0; 3040 } 3041 3042 /* called by kernel when we need to suspend device */ 3043 static int hso_suspend(struct usb_interface *iface, pm_message_t message) 3044 { 3045 int i, result; 3046 3047 /* Stop all serial ports */ 3048 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3049 if (serial_table[i] && (serial_table[i]->interface == iface)) { 3050 result = hso_stop_serial_device(serial_table[i]); 3051 if (result) 3052 goto out; 3053 } 3054 } 3055 3056 /* Stop all network ports */ 3057 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3058 if (network_table[i] && 3059 (network_table[i]->interface == iface)) { 3060 result = hso_stop_net_device(network_table[i]); 3061 if (result) 3062 goto out; 3063 } 3064 } 3065 3066 out: 3067 return 0; 3068 } 3069 3070 /* called by kernel when we need to resume device */ 3071 static int hso_resume(struct usb_interface *iface) 3072 { 3073 int i, result = 0; 3074 struct hso_net *hso_net; 3075 3076 /* Start all serial ports */ 3077 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3078 if (serial_table[i] && (serial_table[i]->interface == iface)) { 3079 if (dev2ser(serial_table[i])->open_count) { 3080 result = 3081 hso_start_serial_device(serial_table[i], GFP_NOIO); 3082 hso_kick_transmit(dev2ser(serial_table[i])); 3083 if (result) 3084 goto out; 3085 } 3086 } 3087 } 3088 3089 /* Start all network ports */ 3090 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3091 if (network_table[i] && 3092 (network_table[i]->interface == iface)) { 3093 hso_net = dev2net(network_table[i]); 3094 if (hso_net->flags & IFF_UP) { 3095 /* First transmit any lingering data, 3096 then restart the device. */ 3097 if (hso_net->skb_tx_buf) { 3098 dev_dbg(&iface->dev, 3099 "Transmitting" 3100 " lingering data\n"); 3101 hso_net_start_xmit(hso_net->skb_tx_buf, 3102 hso_net->net); 3103 hso_net->skb_tx_buf = NULL; 3104 } 3105 result = hso_start_net_device(network_table[i]); 3106 if (result) 3107 goto out; 3108 } 3109 } 3110 } 3111 3112 out: 3113 return result; 3114 } 3115 3116 static void hso_serial_ref_free(struct kref *ref) 3117 { 3118 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref); 3119 3120 hso_free_serial_device(hso_dev); 3121 } 3122 3123 static void hso_free_interface(struct usb_interface *interface) 3124 { 3125 struct hso_serial *hso_dev; 3126 struct tty_struct *tty; 3127 int i; 3128 3129 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3130 if (serial_table[i] 3131 && (serial_table[i]->interface == interface)) { 3132 hso_dev = dev2ser(serial_table[i]); 3133 spin_lock_irq(&hso_dev->serial_lock); 3134 tty = tty_kref_get(hso_dev->tty); 3135 spin_unlock_irq(&hso_dev->serial_lock); 3136 if (tty) 3137 tty_hangup(tty); 3138 mutex_lock(&hso_dev->parent->mutex); 3139 tty_kref_put(tty); 3140 hso_dev->parent->usb_gone = 1; 3141 mutex_unlock(&hso_dev->parent->mutex); 3142 kref_put(&serial_table[i]->ref, hso_serial_ref_free); 3143 } 3144 } 3145 3146 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3147 if (network_table[i] 3148 && (network_table[i]->interface == interface)) { 3149 struct rfkill *rfk = dev2net(network_table[i])->rfkill; 3150 /* hso_stop_net_device doesn't stop the net queue since 3151 * traffic needs to start it again when suspended */ 3152 netif_stop_queue(dev2net(network_table[i])->net); 3153 hso_stop_net_device(network_table[i]); 3154 cancel_work_sync(&network_table[i]->async_put_intf); 3155 cancel_work_sync(&network_table[i]->async_get_intf); 3156 if (rfk) 3157 rfkill_unregister(rfk); 3158 hso_free_net_device(network_table[i]); 3159 } 3160 } 3161 } 3162 3163 /* Helper functions */ 3164 3165 /* Get the endpoint ! */ 3166 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf, 3167 int type, int dir) 3168 { 3169 int i; 3170 struct usb_host_interface *iface = intf->cur_altsetting; 3171 struct usb_endpoint_descriptor *endp; 3172 3173 for (i = 0; i < iface->desc.bNumEndpoints; i++) { 3174 endp = &iface->endpoint[i].desc; 3175 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) && 3176 (usb_endpoint_type(endp) == type)) 3177 return endp; 3178 } 3179 3180 return NULL; 3181 } 3182 3183 /* Get the byte that describes which ports are enabled */ 3184 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports) 3185 { 3186 int i; 3187 struct usb_host_interface *iface = intf->cur_altsetting; 3188 3189 if (iface->extralen == 3) { 3190 *ports = iface->extra[2]; 3191 return 0; 3192 } 3193 3194 for (i = 0; i < iface->desc.bNumEndpoints; i++) { 3195 if (iface->endpoint[i].extralen == 3) { 3196 *ports = iface->endpoint[i].extra[2]; 3197 return 0; 3198 } 3199 } 3200 3201 return -1; 3202 } 3203 3204 /* interrupt urb needs to be submitted, used for serial read of muxed port */ 3205 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int, 3206 struct usb_device *usb, gfp_t gfp) 3207 { 3208 int result; 3209 3210 usb_fill_int_urb(shared_int->shared_intr_urb, usb, 3211 usb_rcvintpipe(usb, 3212 shared_int->intr_endp->bEndpointAddress & 0x7F), 3213 shared_int->shared_intr_buf, 3214 shared_int->intr_endp->wMaxPacketSize, 3215 intr_callback, shared_int, 3216 shared_int->intr_endp->bInterval); 3217 3218 result = usb_submit_urb(shared_int->shared_intr_urb, gfp); 3219 if (result) 3220 dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__, 3221 result); 3222 3223 return result; 3224 } 3225 3226 /* operations setup of the serial interface */ 3227 static const struct tty_operations hso_serial_ops = { 3228 .open = hso_serial_open, 3229 .close = hso_serial_close, 3230 .write = hso_serial_write, 3231 .write_room = hso_serial_write_room, 3232 .ioctl = hso_serial_ioctl, 3233 .set_termios = hso_serial_set_termios, 3234 .chars_in_buffer = hso_serial_chars_in_buffer, 3235 .tiocmget = hso_serial_tiocmget, 3236 .tiocmset = hso_serial_tiocmset, 3237 .unthrottle = hso_unthrottle 3238 }; 3239 3240 static struct usb_driver hso_driver = { 3241 .name = driver_name, 3242 .probe = hso_probe, 3243 .disconnect = hso_disconnect, 3244 .id_table = hso_ids, 3245 .suspend = hso_suspend, 3246 .resume = hso_resume, 3247 .reset_resume = hso_resume, 3248 .supports_autosuspend = 1, 3249 }; 3250 3251 static int __init hso_init(void) 3252 { 3253 int i; 3254 int result; 3255 3256 /* put it in the log */ 3257 printk(KERN_INFO "hso: %s\n", version); 3258 3259 /* Initialise the serial table semaphore and table */ 3260 spin_lock_init(&serial_table_lock); 3261 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) 3262 serial_table[i] = NULL; 3263 3264 /* allocate our driver using the proper amount of supported minors */ 3265 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS); 3266 if (!tty_drv) 3267 return -ENOMEM; 3268 3269 /* fill in all needed values */ 3270 tty_drv->magic = TTY_DRIVER_MAGIC; 3271 tty_drv->owner = THIS_MODULE; 3272 tty_drv->driver_name = driver_name; 3273 tty_drv->name = tty_filename; 3274 3275 /* if major number is provided as parameter, use that one */ 3276 if (tty_major) 3277 tty_drv->major = tty_major; 3278 3279 tty_drv->minor_start = 0; 3280 tty_drv->num = HSO_SERIAL_TTY_MINORS; 3281 tty_drv->type = TTY_DRIVER_TYPE_SERIAL; 3282 tty_drv->subtype = SERIAL_TYPE_NORMAL; 3283 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 3284 tty_drv->init_termios = tty_std_termios; 3285 hso_init_termios(&tty_drv->init_termios); 3286 tty_set_operations(tty_drv, &hso_serial_ops); 3287 3288 /* register the tty driver */ 3289 result = tty_register_driver(tty_drv); 3290 if (result) { 3291 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n", 3292 __func__, result); 3293 return result; 3294 } 3295 3296 /* register this module as an usb driver */ 3297 result = usb_register(&hso_driver); 3298 if (result) { 3299 printk(KERN_ERR "Could not register hso driver? error: %d\n", 3300 result); 3301 /* cleanup serial interface */ 3302 tty_unregister_driver(tty_drv); 3303 return result; 3304 } 3305 3306 /* done */ 3307 return 0; 3308 } 3309 3310 static void __exit hso_exit(void) 3311 { 3312 printk(KERN_INFO "hso: unloaded\n"); 3313 3314 tty_unregister_driver(tty_drv); 3315 /* deregister the usb driver */ 3316 usb_deregister(&hso_driver); 3317 } 3318 3319 /* Module definitions */ 3320 module_init(hso_init); 3321 module_exit(hso_exit); 3322 3323 MODULE_AUTHOR(MOD_AUTHOR); 3324 MODULE_DESCRIPTION(MOD_DESCRIPTION); 3325 MODULE_LICENSE(MOD_LICENSE); 3326 MODULE_INFO(Version, DRIVER_VERSION); 3327 3328 /* change the debug level (eg: insmod hso.ko debug=0x04) */ 3329 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]"); 3330 module_param(debug, int, S_IRUGO | S_IWUSR); 3331 3332 /* set the major tty number (eg: insmod hso.ko tty_major=245) */ 3333 MODULE_PARM_DESC(tty_major, "Set the major tty number"); 3334 module_param(tty_major, int, S_IRUGO | S_IWUSR); 3335 3336 /* disable network interface (eg: insmod hso.ko disable_net=1) */ 3337 MODULE_PARM_DESC(disable_net, "Disable the network interface"); 3338 module_param(disable_net, int, S_IRUGO | S_IWUSR); 3339