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