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