Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 1 | /* |
| 2 | USB Driver layer for GSM modems |
| 3 | |
| 4 | Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de> |
| 5 | |
| 6 | This driver is free software; you can redistribute it and/or modify |
| 7 | it under the terms of Version 2 of the GNU General Public License as |
| 8 | published by the Free Software Foundation. |
| 9 | |
| 10 | Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org> |
| 11 | |
| 12 | History: see the git log. |
| 13 | |
| 14 | Work sponsored by: Sigos GmbH, Germany <info@sigos.de> |
| 15 | |
| 16 | This driver exists because the "normal" serial driver doesn't work too well |
| 17 | with GSM modems. Issues: |
| 18 | - data loss -- one single Receive URB is not nearly enough |
| 19 | - controlling the baud rate doesn't make sense |
| 20 | */ |
| 21 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 22 | #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>" |
| 23 | #define DRIVER_DESC "USB Driver for GSM modems" |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/jiffies.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/tty.h> |
| 30 | #include <linux/tty_flip.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/bitops.h> |
Peter Huewe | 66921ed | 2010-12-09 23:27:35 +0100 | [diff] [blame] | 33 | #include <linux/uaccess.h> |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb/serial.h> |
Dan Williams | 02303f7 | 2010-11-19 16:04:00 -0600 | [diff] [blame] | 36 | #include <linux/serial.h> |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 37 | #include "usb-wwan.h" |
| 38 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 39 | void usb_wwan_dtr_rts(struct usb_serial_port *port, int on) |
| 40 | { |
| 41 | struct usb_serial *serial = port->serial; |
| 42 | struct usb_wwan_port_private *portdata; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 43 | struct usb_wwan_intf_private *intfdata; |
| 44 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 45 | intfdata = port->serial->private; |
| 46 | |
| 47 | if (!intfdata->send_setup) |
| 48 | return; |
| 49 | |
| 50 | portdata = usb_get_serial_port_data(port); |
| 51 | mutex_lock(&serial->disc_mutex); |
| 52 | portdata->rts_state = on; |
| 53 | portdata->dtr_state = on; |
| 54 | if (serial->dev) |
| 55 | intfdata->send_setup(port); |
| 56 | mutex_unlock(&serial->disc_mutex); |
| 57 | } |
| 58 | EXPORT_SYMBOL(usb_wwan_dtr_rts); |
| 59 | |
| 60 | void usb_wwan_set_termios(struct tty_struct *tty, |
| 61 | struct usb_serial_port *port, |
| 62 | struct ktermios *old_termios) |
| 63 | { |
| 64 | struct usb_wwan_intf_private *intfdata = port->serial->private; |
| 65 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 66 | /* Doesn't support option setting */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 67 | tty_termios_copy_hw(&tty->termios, old_termios); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 68 | |
| 69 | if (intfdata->send_setup) |
| 70 | intfdata->send_setup(port); |
| 71 | } |
| 72 | EXPORT_SYMBOL(usb_wwan_set_termios); |
| 73 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 74 | int usb_wwan_tiocmget(struct tty_struct *tty) |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 75 | { |
| 76 | struct usb_serial_port *port = tty->driver_data; |
| 77 | unsigned int value; |
| 78 | struct usb_wwan_port_private *portdata; |
| 79 | |
| 80 | portdata = usb_get_serial_port_data(port); |
| 81 | |
| 82 | value = ((portdata->rts_state) ? TIOCM_RTS : 0) | |
| 83 | ((portdata->dtr_state) ? TIOCM_DTR : 0) | |
| 84 | ((portdata->cts_state) ? TIOCM_CTS : 0) | |
| 85 | ((portdata->dsr_state) ? TIOCM_DSR : 0) | |
| 86 | ((portdata->dcd_state) ? TIOCM_CAR : 0) | |
| 87 | ((portdata->ri_state) ? TIOCM_RNG : 0); |
| 88 | |
| 89 | return value; |
| 90 | } |
| 91 | EXPORT_SYMBOL(usb_wwan_tiocmget); |
| 92 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 93 | int usb_wwan_tiocmset(struct tty_struct *tty, |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 94 | unsigned int set, unsigned int clear) |
| 95 | { |
| 96 | struct usb_serial_port *port = tty->driver_data; |
| 97 | struct usb_wwan_port_private *portdata; |
| 98 | struct usb_wwan_intf_private *intfdata; |
| 99 | |
| 100 | portdata = usb_get_serial_port_data(port); |
| 101 | intfdata = port->serial->private; |
| 102 | |
| 103 | if (!intfdata->send_setup) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | /* FIXME: what locks portdata fields ? */ |
| 107 | if (set & TIOCM_RTS) |
| 108 | portdata->rts_state = 1; |
| 109 | if (set & TIOCM_DTR) |
| 110 | portdata->dtr_state = 1; |
| 111 | |
| 112 | if (clear & TIOCM_RTS) |
| 113 | portdata->rts_state = 0; |
| 114 | if (clear & TIOCM_DTR) |
| 115 | portdata->dtr_state = 0; |
| 116 | return intfdata->send_setup(port); |
| 117 | } |
| 118 | EXPORT_SYMBOL(usb_wwan_tiocmset); |
| 119 | |
Dan Williams | 02303f7 | 2010-11-19 16:04:00 -0600 | [diff] [blame] | 120 | static int get_serial_info(struct usb_serial_port *port, |
| 121 | struct serial_struct __user *retinfo) |
| 122 | { |
| 123 | struct serial_struct tmp; |
| 124 | |
| 125 | if (!retinfo) |
| 126 | return -EFAULT; |
| 127 | |
| 128 | memset(&tmp, 0, sizeof(tmp)); |
| 129 | tmp.line = port->serial->minor; |
| 130 | tmp.port = port->number; |
| 131 | tmp.baud_base = tty_get_baud_rate(port->port.tty); |
| 132 | tmp.close_delay = port->port.close_delay / 10; |
| 133 | tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? |
| 134 | ASYNC_CLOSING_WAIT_NONE : |
| 135 | port->port.closing_wait / 10; |
| 136 | |
| 137 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 138 | return -EFAULT; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int set_serial_info(struct usb_serial_port *port, |
| 143 | struct serial_struct __user *newinfo) |
| 144 | { |
| 145 | struct serial_struct new_serial; |
| 146 | unsigned int closing_wait, close_delay; |
| 147 | int retval = 0; |
| 148 | |
| 149 | if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) |
| 150 | return -EFAULT; |
| 151 | |
| 152 | close_delay = new_serial.close_delay * 10; |
| 153 | closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? |
| 154 | ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; |
| 155 | |
| 156 | mutex_lock(&port->port.mutex); |
| 157 | |
| 158 | if (!capable(CAP_SYS_ADMIN)) { |
| 159 | if ((close_delay != port->port.close_delay) || |
| 160 | (closing_wait != port->port.closing_wait)) |
| 161 | retval = -EPERM; |
| 162 | else |
| 163 | retval = -EOPNOTSUPP; |
| 164 | } else { |
| 165 | port->port.close_delay = close_delay; |
| 166 | port->port.closing_wait = closing_wait; |
| 167 | } |
| 168 | |
| 169 | mutex_unlock(&port->port.mutex); |
| 170 | return retval; |
| 171 | } |
| 172 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 173 | int usb_wwan_ioctl(struct tty_struct *tty, |
Dan Williams | 02303f7 | 2010-11-19 16:04:00 -0600 | [diff] [blame] | 174 | unsigned int cmd, unsigned long arg) |
| 175 | { |
| 176 | struct usb_serial_port *port = tty->driver_data; |
| 177 | |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 178 | dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); |
Dan Williams | 02303f7 | 2010-11-19 16:04:00 -0600 | [diff] [blame] | 179 | |
| 180 | switch (cmd) { |
| 181 | case TIOCGSERIAL: |
| 182 | return get_serial_info(port, |
| 183 | (struct serial_struct __user *) arg); |
| 184 | case TIOCSSERIAL: |
| 185 | return set_serial_info(port, |
| 186 | (struct serial_struct __user *) arg); |
| 187 | default: |
| 188 | break; |
| 189 | } |
| 190 | |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 191 | dev_dbg(&port->dev, "%s arg not supported\n", __func__); |
Dan Williams | 02303f7 | 2010-11-19 16:04:00 -0600 | [diff] [blame] | 192 | |
| 193 | return -ENOIOCTLCMD; |
| 194 | } |
| 195 | EXPORT_SYMBOL(usb_wwan_ioctl); |
| 196 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 197 | /* Write */ |
| 198 | int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 199 | const unsigned char *buf, int count) |
| 200 | { |
| 201 | struct usb_wwan_port_private *portdata; |
| 202 | struct usb_wwan_intf_private *intfdata; |
| 203 | int i; |
| 204 | int left, todo; |
| 205 | struct urb *this_urb = NULL; /* spurious */ |
| 206 | int err; |
| 207 | unsigned long flags; |
| 208 | |
| 209 | portdata = usb_get_serial_port_data(port); |
| 210 | intfdata = port->serial->private; |
| 211 | |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 212 | dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 213 | |
| 214 | i = 0; |
| 215 | left = count; |
| 216 | for (i = 0; left > 0 && i < N_OUT_URB; i++) { |
| 217 | todo = left; |
| 218 | if (todo > OUT_BUFLEN) |
| 219 | todo = OUT_BUFLEN; |
| 220 | |
| 221 | this_urb = portdata->out_urbs[i]; |
| 222 | if (test_and_set_bit(i, &portdata->out_busy)) { |
| 223 | if (time_before(jiffies, |
| 224 | portdata->tx_start_time[i] + 10 * HZ)) |
| 225 | continue; |
| 226 | usb_unlink_urb(this_urb); |
| 227 | continue; |
| 228 | } |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 229 | dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__, |
| 230 | usb_pipeendpoint(this_urb->pipe), i); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 231 | |
| 232 | err = usb_autopm_get_interface_async(port->serial->interface); |
| 233 | if (err < 0) |
| 234 | break; |
| 235 | |
| 236 | /* send the data */ |
| 237 | memcpy(this_urb->transfer_buffer, buf, todo); |
| 238 | this_urb->transfer_buffer_length = todo; |
| 239 | |
| 240 | spin_lock_irqsave(&intfdata->susp_lock, flags); |
| 241 | if (intfdata->suspended) { |
| 242 | usb_anchor_urb(this_urb, &portdata->delayed); |
| 243 | spin_unlock_irqrestore(&intfdata->susp_lock, flags); |
| 244 | } else { |
| 245 | intfdata->in_flight++; |
| 246 | spin_unlock_irqrestore(&intfdata->susp_lock, flags); |
| 247 | err = usb_submit_urb(this_urb, GFP_ATOMIC); |
| 248 | if (err) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 249 | dev_dbg(&port->dev, |
| 250 | "usb_submit_urb %p (write bulk) failed (%d)\n", |
| 251 | this_urb, err); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 252 | clear_bit(i, &portdata->out_busy); |
| 253 | spin_lock_irqsave(&intfdata->susp_lock, flags); |
| 254 | intfdata->in_flight--; |
| 255 | spin_unlock_irqrestore(&intfdata->susp_lock, |
| 256 | flags); |
Oliver Neukum | 3d06bf1 | 2011-02-10 15:33:17 +0100 | [diff] [blame] | 257 | usb_autopm_put_interface_async(port->serial->interface); |
Oliver Neukum | 433508a | 2011-02-10 15:33:23 +0100 | [diff] [blame] | 258 | break; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | portdata->tx_start_time[i] = jiffies; |
| 263 | buf += todo; |
| 264 | left -= todo; |
| 265 | } |
| 266 | |
| 267 | count -= left; |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 268 | dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 269 | return count; |
| 270 | } |
| 271 | EXPORT_SYMBOL(usb_wwan_write); |
| 272 | |
| 273 | static void usb_wwan_indat_callback(struct urb *urb) |
| 274 | { |
| 275 | int err; |
| 276 | int endpoint; |
| 277 | struct usb_serial_port *port; |
| 278 | struct tty_struct *tty; |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 279 | struct device *dev; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 280 | unsigned char *data = urb->transfer_buffer; |
| 281 | int status = urb->status; |
| 282 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 283 | endpoint = usb_pipeendpoint(urb->pipe); |
| 284 | port = urb->context; |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 285 | dev = &port->dev; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 286 | |
| 287 | if (status) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 288 | dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n", |
| 289 | __func__, status, endpoint); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 290 | } else { |
| 291 | tty = tty_port_tty_get(&port->port); |
Jiri Slaby | 38237fd | 2011-02-15 15:55:07 +0100 | [diff] [blame] | 292 | if (tty) { |
| 293 | if (urb->actual_length) { |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 294 | tty_insert_flip_string(&port->port, data, |
Jiri Slaby | 38237fd | 2011-02-15 15:55:07 +0100 | [diff] [blame] | 295 | urb->actual_length); |
| 296 | tty_flip_buffer_push(tty); |
| 297 | } else |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 298 | dev_dbg(dev, "%s: empty read urb received\n", __func__); |
Jiri Slaby | 38237fd | 2011-02-15 15:55:07 +0100 | [diff] [blame] | 299 | tty_kref_put(tty); |
| 300 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 301 | |
| 302 | /* Resubmit urb so we continue receiving */ |
Dan Carpenter | ec42899 | 2012-04-20 09:33:31 +0300 | [diff] [blame] | 303 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 304 | if (err) { |
| 305 | if (err != -EPERM) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 306 | dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err); |
Dan Carpenter | ec42899 | 2012-04-20 09:33:31 +0300 | [diff] [blame] | 307 | /* busy also in error unless we are killed */ |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 308 | usb_mark_last_busy(port->serial->dev); |
Oliver Neukum | c9c4558 | 2011-02-10 15:33:10 +0100 | [diff] [blame] | 309 | } |
Dan Carpenter | ec42899 | 2012-04-20 09:33:31 +0300 | [diff] [blame] | 310 | } else { |
| 311 | usb_mark_last_busy(port->serial->dev); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 312 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 313 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void usb_wwan_outdat_callback(struct urb *urb) |
| 317 | { |
| 318 | struct usb_serial_port *port; |
| 319 | struct usb_wwan_port_private *portdata; |
| 320 | struct usb_wwan_intf_private *intfdata; |
| 321 | int i; |
| 322 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 323 | port = urb->context; |
| 324 | intfdata = port->serial->private; |
| 325 | |
| 326 | usb_serial_port_softint(port); |
| 327 | usb_autopm_put_interface_async(port->serial->interface); |
| 328 | portdata = usb_get_serial_port_data(port); |
| 329 | spin_lock(&intfdata->susp_lock); |
| 330 | intfdata->in_flight--; |
| 331 | spin_unlock(&intfdata->susp_lock); |
| 332 | |
| 333 | for (i = 0; i < N_OUT_URB; ++i) { |
| 334 | if (portdata->out_urbs[i] == urb) { |
| 335 | smp_mb__before_clear_bit(); |
| 336 | clear_bit(i, &portdata->out_busy); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | int usb_wwan_write_room(struct tty_struct *tty) |
| 343 | { |
| 344 | struct usb_serial_port *port = tty->driver_data; |
| 345 | struct usb_wwan_port_private *portdata; |
| 346 | int i; |
| 347 | int data_len = 0; |
| 348 | struct urb *this_urb; |
| 349 | |
| 350 | portdata = usb_get_serial_port_data(port); |
| 351 | |
| 352 | for (i = 0; i < N_OUT_URB; i++) { |
| 353 | this_urb = portdata->out_urbs[i]; |
| 354 | if (this_urb && !test_bit(i, &portdata->out_busy)) |
| 355 | data_len += OUT_BUFLEN; |
| 356 | } |
| 357 | |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 358 | dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 359 | return data_len; |
| 360 | } |
| 361 | EXPORT_SYMBOL(usb_wwan_write_room); |
| 362 | |
| 363 | int usb_wwan_chars_in_buffer(struct tty_struct *tty) |
| 364 | { |
| 365 | struct usb_serial_port *port = tty->driver_data; |
| 366 | struct usb_wwan_port_private *portdata; |
| 367 | int i; |
| 368 | int data_len = 0; |
| 369 | struct urb *this_urb; |
| 370 | |
| 371 | portdata = usb_get_serial_port_data(port); |
| 372 | |
| 373 | for (i = 0; i < N_OUT_URB; i++) { |
| 374 | this_urb = portdata->out_urbs[i]; |
| 375 | /* FIXME: This locking is insufficient as this_urb may |
| 376 | go unused during the test */ |
| 377 | if (this_urb && test_bit(i, &portdata->out_busy)) |
| 378 | data_len += this_urb->transfer_buffer_length; |
| 379 | } |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 380 | dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 381 | return data_len; |
| 382 | } |
| 383 | EXPORT_SYMBOL(usb_wwan_chars_in_buffer); |
| 384 | |
| 385 | int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 386 | { |
| 387 | struct usb_wwan_port_private *portdata; |
| 388 | struct usb_wwan_intf_private *intfdata; |
| 389 | struct usb_serial *serial = port->serial; |
| 390 | int i, err; |
| 391 | struct urb *urb; |
| 392 | |
| 393 | portdata = usb_get_serial_port_data(port); |
| 394 | intfdata = serial->private; |
| 395 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 396 | /* Start reading from the IN endpoint */ |
| 397 | for (i = 0; i < N_IN_URB; i++) { |
| 398 | urb = portdata->in_urbs[i]; |
| 399 | if (!urb) |
| 400 | continue; |
| 401 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 402 | if (err) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 403 | dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n", |
| 404 | __func__, i, err, urb->transfer_buffer_length); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | if (intfdata->send_setup) |
| 409 | intfdata->send_setup(port); |
| 410 | |
| 411 | serial->interface->needs_remote_wakeup = 1; |
| 412 | spin_lock_irq(&intfdata->susp_lock); |
| 413 | portdata->opened = 1; |
| 414 | spin_unlock_irq(&intfdata->susp_lock); |
Oliver Neukum | 9a91aed | 2011-02-10 15:33:37 +0100 | [diff] [blame] | 415 | /* this balances a get in the generic USB serial code */ |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 416 | usb_autopm_put_interface(serial->interface); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | EXPORT_SYMBOL(usb_wwan_open); |
| 421 | |
| 422 | void usb_wwan_close(struct usb_serial_port *port) |
| 423 | { |
| 424 | int i; |
| 425 | struct usb_serial *serial = port->serial; |
| 426 | struct usb_wwan_port_private *portdata; |
| 427 | struct usb_wwan_intf_private *intfdata = port->serial->private; |
| 428 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 429 | portdata = usb_get_serial_port_data(port); |
| 430 | |
| 431 | if (serial->dev) { |
| 432 | /* Stop reading/writing urbs */ |
| 433 | spin_lock_irq(&intfdata->susp_lock); |
| 434 | portdata->opened = 0; |
| 435 | spin_unlock_irq(&intfdata->susp_lock); |
| 436 | |
| 437 | for (i = 0; i < N_IN_URB; i++) |
| 438 | usb_kill_urb(portdata->in_urbs[i]); |
| 439 | for (i = 0; i < N_OUT_URB; i++) |
| 440 | usb_kill_urb(portdata->out_urbs[i]); |
Oliver Neukum | 9a91aed | 2011-02-10 15:33:37 +0100 | [diff] [blame] | 441 | /* balancing - important as an error cannot be handled*/ |
| 442 | usb_autopm_get_interface_no_resume(serial->interface); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 443 | serial->interface->needs_remote_wakeup = 0; |
| 444 | } |
| 445 | } |
| 446 | EXPORT_SYMBOL(usb_wwan_close); |
| 447 | |
| 448 | /* Helper functions used by usb_wwan_setup_urbs */ |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 449 | static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port, |
| 450 | int endpoint, |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 451 | int dir, void *ctx, char *buf, int len, |
| 452 | void (*callback) (struct urb *)) |
| 453 | { |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 454 | struct usb_serial *serial = port->serial; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 455 | struct urb *urb; |
| 456 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 457 | urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ |
| 458 | if (urb == NULL) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 459 | dev_dbg(&serial->interface->dev, |
| 460 | "%s: alloc for endpoint %d failed.\n", __func__, |
| 461 | endpoint); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | /* Fill URB using supplied data. */ |
| 466 | usb_fill_bulk_urb(urb, serial->dev, |
| 467 | usb_sndbulkpipe(serial->dev, endpoint) | dir, |
| 468 | buf, len, callback, ctx); |
| 469 | |
| 470 | return urb; |
| 471 | } |
| 472 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 473 | int usb_wwan_port_probe(struct usb_serial_port *port) |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 474 | { |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 475 | struct usb_wwan_port_private *portdata; |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 476 | struct urb *urb; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 477 | u8 *buffer; |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 478 | int err; |
| 479 | int i; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 480 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 481 | portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); |
| 482 | if (!portdata) |
| 483 | return -ENOMEM; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 484 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 485 | init_usb_anchor(&portdata->delayed); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 486 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 487 | for (i = 0; i < N_IN_URB; i++) { |
Johan Hovold | 8e493ca1 | 2012-10-26 18:44:20 +0200 | [diff] [blame] | 488 | if (!port->bulk_in_size) |
| 489 | break; |
| 490 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 491 | buffer = (u8 *)__get_free_page(GFP_KERNEL); |
| 492 | if (!buffer) |
| 493 | goto bail_out_error; |
| 494 | portdata->in_buffer[i] = buffer; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 495 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 496 | urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress, |
| 497 | USB_DIR_IN, port, |
| 498 | buffer, IN_BUFLEN, |
| 499 | usb_wwan_indat_callback); |
| 500 | portdata->in_urbs[i] = urb; |
| 501 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 502 | |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 503 | for (i = 0; i < N_OUT_URB; i++) { |
Johan Hovold | 8e493ca1 | 2012-10-26 18:44:20 +0200 | [diff] [blame] | 504 | if (!port->bulk_out_size) |
| 505 | break; |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 506 | |
| 507 | buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); |
| 508 | if (!buffer) |
| 509 | goto bail_out_error2; |
| 510 | portdata->out_buffer[i] = buffer; |
| 511 | |
| 512 | urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress, |
| 513 | USB_DIR_OUT, port, |
| 514 | buffer, OUT_BUFLEN, |
| 515 | usb_wwan_outdat_callback); |
| 516 | portdata->out_urbs[i] = urb; |
| 517 | } |
| 518 | |
| 519 | usb_set_serial_port_data(port, portdata); |
| 520 | |
| 521 | if (port->interrupt_in_urb) { |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 522 | err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 523 | if (err) |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 524 | dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n", |
| 525 | __func__, err); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 526 | } |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 527 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 528 | return 0; |
| 529 | |
| 530 | bail_out_error2: |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 531 | for (i = 0; i < N_OUT_URB; i++) { |
| 532 | usb_free_urb(portdata->out_urbs[i]); |
| 533 | kfree(portdata->out_buffer[i]); |
| 534 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 535 | bail_out_error: |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 536 | for (i = 0; i < N_IN_URB; i++) { |
| 537 | usb_free_urb(portdata->in_urbs[i]); |
| 538 | free_page((unsigned long)portdata->in_buffer[i]); |
| 539 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 540 | kfree(portdata); |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 541 | |
| 542 | return -ENOMEM; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 543 | } |
Johan Hovold | b8f0e82 | 2012-10-25 10:29:16 +0200 | [diff] [blame] | 544 | EXPORT_SYMBOL_GPL(usb_wwan_port_probe); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 545 | |
Bjørn Mork | a1028f0 | 2012-07-27 01:11:41 +0200 | [diff] [blame] | 546 | int usb_wwan_port_remove(struct usb_serial_port *port) |
| 547 | { |
| 548 | int i; |
| 549 | struct usb_wwan_port_private *portdata; |
| 550 | |
| 551 | portdata = usb_get_serial_port_data(port); |
| 552 | usb_set_serial_port_data(port, NULL); |
| 553 | |
| 554 | /* Stop reading/writing urbs and free them */ |
| 555 | for (i = 0; i < N_IN_URB; i++) { |
| 556 | usb_kill_urb(portdata->in_urbs[i]); |
| 557 | usb_free_urb(portdata->in_urbs[i]); |
| 558 | free_page((unsigned long)portdata->in_buffer[i]); |
| 559 | } |
| 560 | for (i = 0; i < N_OUT_URB; i++) { |
| 561 | usb_kill_urb(portdata->out_urbs[i]); |
| 562 | usb_free_urb(portdata->out_urbs[i]); |
| 563 | kfree(portdata->out_buffer[i]); |
| 564 | } |
| 565 | |
| 566 | /* Now free port private data */ |
| 567 | kfree(portdata); |
| 568 | return 0; |
| 569 | } |
| 570 | EXPORT_SYMBOL(usb_wwan_port_remove); |
| 571 | |
| 572 | #ifdef CONFIG_PM |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 573 | static void stop_read_write_urbs(struct usb_serial *serial) |
| 574 | { |
| 575 | int i, j; |
| 576 | struct usb_serial_port *port; |
| 577 | struct usb_wwan_port_private *portdata; |
| 578 | |
| 579 | /* Stop reading/writing urbs */ |
| 580 | for (i = 0; i < serial->num_ports; ++i) { |
| 581 | port = serial->port[i]; |
| 582 | portdata = usb_get_serial_port_data(port); |
Bjørn Mork | 032129c | 2012-07-27 01:11:43 +0200 | [diff] [blame] | 583 | if (!portdata) |
| 584 | continue; |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 585 | for (j = 0; j < N_IN_URB; j++) |
| 586 | usb_kill_urb(portdata->in_urbs[j]); |
| 587 | for (j = 0; j < N_OUT_URB; j++) |
| 588 | usb_kill_urb(portdata->out_urbs[j]); |
| 589 | } |
| 590 | } |
| 591 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 592 | int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message) |
| 593 | { |
| 594 | struct usb_wwan_intf_private *intfdata = serial->private; |
| 595 | int b; |
| 596 | |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 597 | if (PMSG_IS_AUTO(message)) { |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 598 | spin_lock_irq(&intfdata->susp_lock); |
| 599 | b = intfdata->in_flight; |
| 600 | spin_unlock_irq(&intfdata->susp_lock); |
| 601 | |
| 602 | if (b) |
| 603 | return -EBUSY; |
| 604 | } |
| 605 | |
| 606 | spin_lock_irq(&intfdata->susp_lock); |
| 607 | intfdata->suspended = 1; |
| 608 | spin_unlock_irq(&intfdata->susp_lock); |
| 609 | stop_read_write_urbs(serial); |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | EXPORT_SYMBOL(usb_wwan_suspend); |
| 614 | |
Oliver Neukum | 16871dc | 2011-02-10 15:33:29 +0100 | [diff] [blame] | 615 | static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata) |
| 616 | { |
| 617 | int i; |
| 618 | |
| 619 | for (i = 0; i < N_OUT_URB; i++) { |
| 620 | if (urb == portdata->out_urbs[i]) { |
| 621 | clear_bit(i, &portdata->out_busy); |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 627 | static void play_delayed(struct usb_serial_port *port) |
| 628 | { |
| 629 | struct usb_wwan_intf_private *data; |
| 630 | struct usb_wwan_port_private *portdata; |
| 631 | struct urb *urb; |
| 632 | int err; |
| 633 | |
| 634 | portdata = usb_get_serial_port_data(port); |
| 635 | data = port->serial->private; |
| 636 | while ((urb = usb_get_from_anchor(&portdata->delayed))) { |
| 637 | err = usb_submit_urb(urb, GFP_ATOMIC); |
Oliver Neukum | 16871dc | 2011-02-10 15:33:29 +0100 | [diff] [blame] | 638 | if (!err) { |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 639 | data->in_flight++; |
Oliver Neukum | 16871dc | 2011-02-10 15:33:29 +0100 | [diff] [blame] | 640 | } else { |
| 641 | /* we have to throw away the rest */ |
| 642 | do { |
| 643 | unbusy_queued_urb(urb, portdata); |
Oliver Neukum | 97ac01d | 2011-03-18 12:44:17 +0100 | [diff] [blame] | 644 | usb_autopm_put_interface_no_suspend(port->serial->interface); |
Oliver Neukum | 16871dc | 2011-02-10 15:33:29 +0100 | [diff] [blame] | 645 | } while ((urb = usb_get_from_anchor(&portdata->delayed))); |
| 646 | break; |
| 647 | } |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | int usb_wwan_resume(struct usb_serial *serial) |
| 652 | { |
| 653 | int i, j; |
| 654 | struct usb_serial_port *port; |
| 655 | struct usb_wwan_intf_private *intfdata = serial->private; |
| 656 | struct usb_wwan_port_private *portdata; |
| 657 | struct urb *urb; |
| 658 | int err = 0; |
| 659 | |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 660 | /* get the interrupt URBs resubmitted unconditionally */ |
| 661 | for (i = 0; i < serial->num_ports; i++) { |
| 662 | port = serial->port[i]; |
| 663 | if (!port->interrupt_in_urb) { |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 664 | dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 665 | continue; |
| 666 | } |
| 667 | err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); |
Greg Kroah-Hartman | a80be97 | 2012-09-13 17:41:50 -0700 | [diff] [blame] | 668 | dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 669 | if (err < 0) { |
Greg Kroah-Hartman | 3a1c2a8 | 2012-04-20 16:54:01 -0700 | [diff] [blame] | 670 | dev_err(&port->dev, "%s: Error %d for interrupt URB\n", |
| 671 | __func__, err); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 672 | goto err_out; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | for (i = 0; i < serial->num_ports; i++) { |
| 677 | /* walk all ports */ |
| 678 | port = serial->port[i]; |
| 679 | portdata = usb_get_serial_port_data(port); |
| 680 | |
| 681 | /* skip closed ports */ |
| 682 | spin_lock_irq(&intfdata->susp_lock); |
Bjørn Mork | 032129c | 2012-07-27 01:11:43 +0200 | [diff] [blame] | 683 | if (!portdata || !portdata->opened) { |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 684 | spin_unlock_irq(&intfdata->susp_lock); |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | for (j = 0; j < N_IN_URB; j++) { |
| 689 | urb = portdata->in_urbs[j]; |
| 690 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 691 | if (err < 0) { |
Greg Kroah-Hartman | 3a1c2a8 | 2012-04-20 16:54:01 -0700 | [diff] [blame] | 692 | dev_err(&port->dev, "%s: Error %d for bulk URB %d\n", |
| 693 | __func__, err, i); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 694 | spin_unlock_irq(&intfdata->susp_lock); |
| 695 | goto err_out; |
| 696 | } |
| 697 | } |
| 698 | play_delayed(port); |
| 699 | spin_unlock_irq(&intfdata->susp_lock); |
| 700 | } |
| 701 | spin_lock_irq(&intfdata->susp_lock); |
| 702 | intfdata->suspended = 0; |
| 703 | spin_unlock_irq(&intfdata->susp_lock); |
| 704 | err_out: |
| 705 | return err; |
| 706 | } |
| 707 | EXPORT_SYMBOL(usb_wwan_resume); |
| 708 | #endif |
| 709 | |
| 710 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 711 | MODULE_DESCRIPTION(DRIVER_DESC); |
Matthew Garrett | 0d45619 | 2010-04-01 12:31:07 -0400 | [diff] [blame] | 712 | MODULE_LICENSE("GPL"); |