Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cdc-acm.c |
| 3 | * |
| 4 | * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de> |
| 5 | * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> |
| 6 | * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com> |
| 7 | * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> |
| 8 | * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name> |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 9 | * Copyright (c) 2005 David Kubicek <dave@awk.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * USB Abstract Control Model driver for USB modems and ISDN adapters |
| 12 | * |
| 13 | * Sponsored by SuSE |
| 14 | * |
| 15 | * ChangeLog: |
| 16 | * v0.9 - thorough cleaning, URBification, almost a rewrite |
| 17 | * v0.10 - some more cleanups |
| 18 | * v0.11 - fixed flow control, read error doesn't stop reads |
| 19 | * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced |
| 20 | * v0.13 - added termios, added hangup |
| 21 | * v0.14 - sized down struct acm |
| 22 | * v0.15 - fixed flow control again - characters could be lost |
| 23 | * v0.16 - added code for modems with swapped data and control interfaces |
| 24 | * v0.17 - added new style probing |
| 25 | * v0.18 - fixed new style probing for devices with more configurations |
| 26 | * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan) |
| 27 | * v0.20 - switched to probing on interface (rather than device) class |
| 28 | * v0.21 - revert to probing on device for devices with multiple configs |
| 29 | * v0.22 - probe only the control interface. if usbcore doesn't choose the |
| 30 | * config we want, sysadmin changes bConfigurationValue in sysfs. |
| 31 | * v0.23 - use softirq for rx processing, as needed by tty layer |
| 32 | * v0.24 - change probe method to evaluate CDC union descriptor |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 33 | * v0.25 - downstream tasks paralelized to maximize throughput |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 34 | * v0.26 - multiple write urbs, writesize increased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * This program is free software; you can redistribute it and/or modify |
| 39 | * it under the terms of the GNU General Public License as published by |
| 40 | * the Free Software Foundation; either version 2 of the License, or |
| 41 | * (at your option) any later version. |
| 42 | * |
| 43 | * This program is distributed in the hope that it will be useful, |
| 44 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 45 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 46 | * GNU General Public License for more details. |
| 47 | * |
| 48 | * You should have received a copy of the GNU General Public License |
| 49 | * along with this program; if not, write to the Free Software |
| 50 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 51 | */ |
| 52 | |
| 53 | #undef DEBUG |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 54 | #undef VERBOSE_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #include <linux/kernel.h> |
| 57 | #include <linux/errno.h> |
| 58 | #include <linux/init.h> |
| 59 | #include <linux/slab.h> |
| 60 | #include <linux/tty.h> |
| 61 | #include <linux/tty_driver.h> |
| 62 | #include <linux/tty_flip.h> |
| 63 | #include <linux/module.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 64 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <asm/uaccess.h> |
| 66 | #include <linux/usb.h> |
David Brownell | a8c28f2 | 2006-06-13 09:57:47 -0700 | [diff] [blame] | 67 | #include <linux/usb/cdc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <asm/byteorder.h> |
| 69 | #include <asm/unaligned.h> |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 70 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | #include "cdc-acm.h" |
| 73 | |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 74 | |
| 75 | #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */ |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Version Information |
| 79 | */ |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 80 | #define DRIVER_VERSION "v0.26" |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 81 | #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters" |
| 83 | |
| 84 | static struct usb_driver acm_driver; |
| 85 | static struct tty_driver *acm_tty_driver; |
| 86 | static struct acm *acm_table[ACM_TTY_MINORS]; |
| 87 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 88 | static DEFINE_MUTEX(open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | #define ACM_READY(acm) (acm && acm->dev && acm->used) |
| 91 | |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 92 | #ifdef VERBOSE_DEBUG |
| 93 | #define verbose 1 |
| 94 | #else |
| 95 | #define verbose 0 |
| 96 | #endif |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Functions for ACM control messages. |
| 100 | */ |
| 101 | |
| 102 | static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len) |
| 103 | { |
| 104 | int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), |
| 105 | request, USB_RT_ACM, value, |
| 106 | acm->control->altsetting[0].desc.bInterfaceNumber, |
| 107 | buf, len, 5000); |
| 108 | dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval); |
| 109 | return retval < 0 ? retval : 0; |
| 110 | } |
| 111 | |
| 112 | /* devices aren't required to support these requests. |
| 113 | * the cdc acm descriptor tells whether they do... |
| 114 | */ |
| 115 | #define acm_set_control(acm, control) \ |
| 116 | acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0) |
| 117 | #define acm_set_line(acm, line) \ |
| 118 | acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) |
| 119 | #define acm_send_break(acm, ms) \ |
| 120 | acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0) |
| 121 | |
| 122 | /* |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 123 | * Write buffer management. |
| 124 | * All of these assume proper locks taken by the caller. |
| 125 | */ |
| 126 | |
| 127 | static int acm_wb_alloc(struct acm *acm) |
| 128 | { |
| 129 | int i, wbn; |
| 130 | struct acm_wb *wb; |
| 131 | |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 132 | wbn = 0; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 133 | i = 0; |
| 134 | for (;;) { |
| 135 | wb = &acm->wb[wbn]; |
| 136 | if (!wb->use) { |
| 137 | wb->use = 1; |
| 138 | return wbn; |
| 139 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 140 | wbn = (wbn + 1) % ACM_NW; |
| 141 | if (++i >= ACM_NW) |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | } |
| 145 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 146 | static int acm_wb_is_avail(struct acm *acm) |
| 147 | { |
| 148 | int i, n; |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 149 | unsigned long flags; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 150 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 151 | n = ACM_NW; |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 152 | spin_lock_irqsave(&acm->write_lock, flags); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 153 | for (i = 0; i < ACM_NW; i++) { |
| 154 | n -= acm->wb[i].use; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 155 | } |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 156 | spin_unlock_irqrestore(&acm->write_lock, flags); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 157 | return n; |
| 158 | } |
| 159 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 160 | /* |
| 161 | * Finish write. |
| 162 | */ |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 163 | static void acm_write_done(struct acm *acm, struct acm_wb *wb) |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 164 | { |
| 165 | unsigned long flags; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 166 | |
| 167 | spin_lock_irqsave(&acm->write_lock, flags); |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 168 | wb->use = 0; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 169 | acm->transmitting--; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 170 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Poke write. |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 175 | * |
| 176 | * the caller is responsible for locking |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 177 | */ |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 178 | |
| 179 | static int acm_start_wb(struct acm *acm, struct acm_wb *wb) |
| 180 | { |
| 181 | int rc; |
| 182 | |
| 183 | acm->transmitting++; |
| 184 | |
| 185 | wb->urb->transfer_buffer = wb->buf; |
| 186 | wb->urb->transfer_dma = wb->dmah; |
| 187 | wb->urb->transfer_buffer_length = wb->len; |
| 188 | wb->urb->dev = acm->dev; |
| 189 | |
| 190 | if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) { |
| 191 | dbg("usb_submit_urb(write bulk) failed: %d", rc); |
| 192 | acm_write_done(acm, wb); |
| 193 | } |
| 194 | return rc; |
| 195 | } |
| 196 | |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 197 | static int acm_write_start(struct acm *acm, int wbn) |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 198 | { |
| 199 | unsigned long flags; |
David Brownell | 934da46 | 2008-08-06 18:44:12 -0700 | [diff] [blame] | 200 | struct acm_wb *wb = &acm->wb[wbn]; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 201 | int rc; |
| 202 | |
| 203 | spin_lock_irqsave(&acm->write_lock, flags); |
| 204 | if (!acm->dev) { |
David Brownell | 934da46 | 2008-08-06 18:44:12 -0700 | [diff] [blame] | 205 | wb->use = 0; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 206 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 210 | dbg("%s susp_count: %d", __func__, acm->susp_count); |
| 211 | if (acm->susp_count) { |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 212 | acm->delayed_wb = wb; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 213 | schedule_work(&acm->waker); |
| 214 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 215 | return 0; /* A white lie */ |
| 216 | } |
| 217 | usb_mark_last_busy(acm->dev); |
| 218 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 219 | rc = acm_start_wb(acm, wb); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 220 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 221 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 222 | return rc; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 223 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 224 | } |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 225 | /* |
| 226 | * attributes exported through sysfs |
| 227 | */ |
| 228 | static ssize_t show_caps |
| 229 | (struct device *dev, struct device_attribute *attr, char *buf) |
| 230 | { |
| 231 | struct usb_interface *intf = to_usb_interface(dev); |
| 232 | struct acm *acm = usb_get_intfdata(intf); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 233 | |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 234 | return sprintf(buf, "%d", acm->ctrl_caps); |
| 235 | } |
| 236 | static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL); |
| 237 | |
| 238 | static ssize_t show_country_codes |
| 239 | (struct device *dev, struct device_attribute *attr, char *buf) |
| 240 | { |
| 241 | struct usb_interface *intf = to_usb_interface(dev); |
| 242 | struct acm *acm = usb_get_intfdata(intf); |
| 243 | |
| 244 | memcpy(buf, acm->country_codes, acm->country_code_size); |
| 245 | return acm->country_code_size; |
| 246 | } |
| 247 | |
| 248 | static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL); |
| 249 | |
| 250 | static ssize_t show_country_rel_date |
| 251 | (struct device *dev, struct device_attribute *attr, char *buf) |
| 252 | { |
| 253 | struct usb_interface *intf = to_usb_interface(dev); |
| 254 | struct acm *acm = usb_get_intfdata(intf); |
| 255 | |
| 256 | return sprintf(buf, "%d", acm->country_rel_date); |
| 257 | } |
| 258 | |
| 259 | static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 260 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * Interrupt handlers for various ACM device responses |
| 262 | */ |
| 263 | |
| 264 | /* control interface reports status changes with "interrupt" transfers */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 265 | static void acm_ctrl_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | struct acm *acm = urb->context; |
| 268 | struct usb_cdc_notification *dr = urb->transfer_buffer; |
| 269 | unsigned char *data; |
| 270 | int newctrl; |
Greg Kroah-Hartman | 185d405 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 271 | int retval; |
| 272 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Greg Kroah-Hartman | 185d405 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 274 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | case 0: |
| 276 | /* success */ |
| 277 | break; |
| 278 | case -ECONNRESET: |
| 279 | case -ENOENT: |
| 280 | case -ESHUTDOWN: |
| 281 | /* this urb is terminated, clean up */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 282 | dbg("%s - urb shutting down with status: %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return; |
| 284 | default: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 285 | dbg("%s - nonzero urb status received: %d", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | goto exit; |
| 287 | } |
| 288 | |
| 289 | if (!ACM_READY(acm)) |
| 290 | goto exit; |
| 291 | |
| 292 | data = (unsigned char *)(dr + 1); |
| 293 | switch (dr->bNotificationType) { |
| 294 | |
| 295 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 296 | |
| 297 | dbg("%s network", dr->wValue ? "connected to" : "disconnected from"); |
| 298 | break; |
| 299 | |
| 300 | case USB_CDC_NOTIFY_SERIAL_STATE: |
| 301 | |
Harvey Harrison | a5abdea | 2008-04-29 01:03:40 -0700 | [diff] [blame] | 302 | newctrl = get_unaligned_le16(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { |
| 305 | dbg("calling hangup"); |
| 306 | tty_hangup(acm->tty); |
| 307 | } |
| 308 | |
| 309 | acm->ctrlin = newctrl; |
| 310 | |
| 311 | dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c", |
| 312 | acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', |
| 313 | acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-', |
| 314 | acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', |
| 315 | acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); |
| 316 | |
| 317 | break; |
| 318 | |
| 319 | default: |
| 320 | dbg("unknown notification %d received: index %d len %d data0 %d data1 %d", |
| 321 | dr->bNotificationType, dr->wIndex, |
| 322 | dr->wLength, data[0], data[1]); |
| 323 | break; |
| 324 | } |
| 325 | exit: |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 326 | usb_mark_last_busy(acm->dev); |
Greg Kroah-Hartman | 185d405 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 327 | retval = usb_submit_urb (urb, GFP_ATOMIC); |
| 328 | if (retval) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 329 | dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with " |
| 330 | "result %d", __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* data interface returns incoming bytes, or we got unthrottled */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 334 | static void acm_read_bulk(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 336 | struct acm_rb *buf; |
| 337 | struct acm_ru *rcv = urb->context; |
| 338 | struct acm *acm = rcv->instance; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 339 | int status = urb->status; |
Greg Kroah-Hartman | 185d405 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 340 | |
| 341 | dbg("Entering acm_read_bulk with status %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 343 | if (!ACM_READY(acm)) { |
| 344 | dev_dbg(&acm->data->dev, "Aborting, acm not ready"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 346 | } |
| 347 | usb_mark_last_busy(acm->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 349 | if (status) |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 350 | dev_dbg(&acm->data->dev, "bulk rx status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 352 | buf = rcv->buffer; |
| 353 | buf->size = urb->actual_length; |
| 354 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 355 | if (likely(status == 0)) { |
| 356 | spin_lock(&acm->read_lock); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 357 | acm->processing++; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 358 | list_add_tail(&rcv->list, &acm->spare_read_urbs); |
| 359 | list_add_tail(&buf->list, &acm->filled_read_bufs); |
| 360 | spin_unlock(&acm->read_lock); |
| 361 | } else { |
| 362 | /* we drop the buffer due to an error */ |
| 363 | spin_lock(&acm->read_lock); |
| 364 | list_add_tail(&rcv->list, &acm->spare_read_urbs); |
| 365 | list_add(&buf->list, &acm->spare_read_bufs); |
| 366 | spin_unlock(&acm->read_lock); |
| 367 | /* nevertheless the tasklet must be kicked unconditionally |
| 368 | so the queue cannot dry up */ |
| 369 | } |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 370 | if (likely(!acm->susp_count)) |
| 371 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void acm_rx_tasklet(unsigned long _acm) |
| 375 | { |
| 376 | struct acm *acm = (void *)_acm; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 377 | struct acm_rb *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | struct tty_struct *tty = acm->tty; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 379 | struct acm_ru *rcv; |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 380 | unsigned long flags; |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 381 | unsigned char throttled; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | dbg("Entering acm_rx_tasklet"); |
| 384 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 385 | if (!ACM_READY(acm)) |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 386 | { |
| 387 | dbg("acm_rx_tasklet: ACM not ready"); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 388 | return; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 389 | } |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 390 | |
Oliver Neukum | 834dbca | 2007-03-06 10:47:04 +0100 | [diff] [blame] | 391 | spin_lock_irqsave(&acm->throttle_lock, flags); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 392 | throttled = acm->throttle; |
Oliver Neukum | 834dbca | 2007-03-06 10:47:04 +0100 | [diff] [blame] | 393 | spin_unlock_irqrestore(&acm->throttle_lock, flags); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 394 | if (throttled) |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 395 | { |
| 396 | dbg("acm_rx_tasklet: throttled"); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 397 | return; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 398 | } |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 399 | |
| 400 | next_buffer: |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 401 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 402 | if (list_empty(&acm->filled_read_bufs)) { |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 403 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 404 | goto urbs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 406 | buf = list_entry(acm->filled_read_bufs.next, |
| 407 | struct acm_rb, list); |
| 408 | list_del(&buf->list); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 409 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 410 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 411 | dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 412 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 413 | tty_buffer_request_room(tty, buf->size); |
Oliver Neukum | 834dbca | 2007-03-06 10:47:04 +0100 | [diff] [blame] | 414 | spin_lock_irqsave(&acm->throttle_lock, flags); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 415 | throttled = acm->throttle; |
Oliver Neukum | 834dbca | 2007-03-06 10:47:04 +0100 | [diff] [blame] | 416 | spin_unlock_irqrestore(&acm->throttle_lock, flags); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 417 | if (!throttled) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 418 | tty_insert_flip_string(tty, buf->base, buf->size); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 419 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 421 | if (throttled) { |
| 422 | dbg("Throttling noticed"); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 423 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 424 | list_add(&buf->list, &acm->filled_read_bufs); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 425 | spin_unlock_irqrestore(&acm->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return; |
| 427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 429 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 430 | list_add(&buf->list, &acm->spare_read_bufs); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 431 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 432 | goto next_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 434 | urbs: |
| 435 | while (!list_empty(&acm->spare_read_bufs)) { |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 436 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 437 | if (list_empty(&acm->spare_read_urbs)) { |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 438 | acm->processing = 0; |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 439 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | rcv = list_entry(acm->spare_read_urbs.next, |
| 443 | struct acm_ru, list); |
| 444 | list_del(&rcv->list); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 445 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 446 | |
| 447 | buf = list_entry(acm->spare_read_bufs.next, |
| 448 | struct acm_rb, list); |
| 449 | list_del(&buf->list); |
| 450 | |
| 451 | rcv->buffer = buf; |
| 452 | |
| 453 | usb_fill_bulk_urb(rcv->urb, acm->dev, |
| 454 | acm->rx_endpoint, |
| 455 | buf->base, |
| 456 | acm->readsize, |
| 457 | acm_read_bulk, rcv); |
| 458 | rcv->urb->transfer_dma = buf->dma; |
| 459 | rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 460 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 461 | /* This shouldn't kill the driver as unsuccessful URBs are returned to the |
| 462 | free-urbs-pool and resubmited ASAP */ |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 463 | spin_lock_irqsave(&acm->read_lock, flags); |
| 464 | if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 465 | list_add(&buf->list, &acm->spare_read_bufs); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 466 | list_add(&rcv->list, &acm->spare_read_urbs); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 467 | acm->processing = 0; |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 468 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 469 | return; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 470 | } else { |
| 471 | spin_unlock_irqrestore(&acm->read_lock, flags); |
| 472 | dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 475 | spin_lock_irqsave(&acm->read_lock, flags); |
| 476 | acm->processing = 0; |
| 477 | spin_unlock_irqrestore(&acm->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* data interface wrote those outgoing bytes */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 481 | static void acm_write_bulk(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 483 | struct acm_wb *wb = urb->context; |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 484 | struct acm *acm = wb->instance; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 485 | |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 486 | if (verbose || urb->status |
| 487 | || (urb->actual_length != urb->transfer_buffer_length)) |
| 488 | dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n", |
| 489 | urb->actual_length, |
| 490 | urb->transfer_buffer_length, |
| 491 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 493 | acm_write_done(acm, wb); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 494 | if (ACM_READY(acm)) |
| 495 | schedule_work(&acm->work); |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 496 | else |
| 497 | wake_up_interruptible(&acm->drain_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 500 | static void acm_softint(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 502 | struct acm *acm = container_of(work, struct acm, work); |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 503 | |
| 504 | dev_vdbg(&acm->data->dev, "tx work\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (!ACM_READY(acm)) |
| 506 | return; |
| 507 | tty_wakeup(acm->tty); |
| 508 | } |
| 509 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 510 | static void acm_waker(struct work_struct *waker) |
| 511 | { |
| 512 | struct acm *acm = container_of(waker, struct acm, waker); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 513 | int rv; |
| 514 | |
| 515 | rv = usb_autopm_get_interface(acm->control); |
| 516 | if (rv < 0) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 517 | dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | if (acm->delayed_wb) { |
| 521 | acm_start_wb(acm, acm->delayed_wb); |
| 522 | acm->delayed_wb = NULL; |
| 523 | } |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 524 | usb_autopm_put_interface(acm->control); |
| 525 | } |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* |
| 528 | * TTY handlers |
| 529 | */ |
| 530 | |
| 531 | static int acm_tty_open(struct tty_struct *tty, struct file *filp) |
| 532 | { |
| 533 | struct acm *acm; |
| 534 | int rv = -EINVAL; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 535 | int i; |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 536 | dbg("Entering acm_tty_open."); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 537 | |
| 538 | mutex_lock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | acm = acm_table[tty->index]; |
| 541 | if (!acm || !acm->dev) |
| 542 | goto err_out; |
| 543 | else |
| 544 | rv = 0; |
| 545 | |
David Engraf | 28d1dfa | 2008-03-20 10:53:52 +0100 | [diff] [blame] | 546 | set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | tty->driver_data = acm; |
| 548 | acm->tty = tty; |
| 549 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 550 | /* force low_latency on so that our tty_push actually forces the data through, |
| 551 | otherwise it is scheduled, and with high data rates data can get lost. */ |
| 552 | tty->low_latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Oliver Neukum | 94409cc | 2008-02-11 15:22:29 +0100 | [diff] [blame] | 554 | if (usb_autopm_get_interface(acm->control) < 0) |
| 555 | goto early_bail; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 556 | else |
| 557 | acm->control->needs_remote_wakeup = 1; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 558 | |
| 559 | mutex_lock(&acm->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (acm->used++) { |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 561 | usb_autopm_put_interface(acm->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto done; |
| 563 | } |
| 564 | |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | acm->ctrlurb->dev = acm->dev; |
| 567 | if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { |
| 568 | dbg("usb_submit_urb(ctrl irq) failed"); |
| 569 | goto bail_out; |
| 570 | } |
| 571 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 572 | if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) && |
| 573 | (acm->ctrl_caps & USB_CDC_CAP_LINE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto full_bailout; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 575 | usb_autopm_put_interface(acm->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 577 | INIT_LIST_HEAD(&acm->spare_read_urbs); |
| 578 | INIT_LIST_HEAD(&acm->spare_read_bufs); |
| 579 | INIT_LIST_HEAD(&acm->filled_read_bufs); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 580 | for (i = 0; i < acm->rx_buflimit; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 581 | list_add(&(acm->ru[i].list), &acm->spare_read_urbs); |
| 582 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 583 | for (i = 0; i < acm->rx_buflimit; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 584 | list_add(&(acm->rb[i].list), &acm->spare_read_bufs); |
| 585 | } |
| 586 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame] | 587 | acm->throttle = 0; |
| 588 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 589 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | done: |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 592 | mutex_unlock(&acm->mutex); |
Alexey Dobriyan | 74573ee | 2008-08-20 16:56:04 -0700 | [diff] [blame] | 593 | err_out: |
Oliver Neukum | 94409cc | 2008-02-11 15:22:29 +0100 | [diff] [blame] | 594 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | return rv; |
| 596 | |
| 597 | full_bailout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | usb_kill_urb(acm->ctrlurb); |
| 599 | bail_out: |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 600 | usb_autopm_put_interface(acm->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | acm->used--; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 602 | mutex_unlock(&acm->mutex); |
Oliver Neukum | 94409cc | 2008-02-11 15:22:29 +0100 | [diff] [blame] | 603 | early_bail: |
| 604 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | return -EIO; |
| 606 | } |
| 607 | |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 608 | static void acm_tty_unregister(struct acm *acm) |
| 609 | { |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 610 | int i,nr; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 611 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 612 | nr = acm->rx_buflimit; |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 613 | tty_unregister_device(acm_tty_driver, acm->minor); |
| 614 | usb_put_intf(acm->control); |
| 615 | acm_table[acm->minor] = NULL; |
| 616 | usb_free_urb(acm->ctrlurb); |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 617 | for (i = 0; i < ACM_NW; i++) |
| 618 | usb_free_urb(acm->wb[i].urb); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 619 | for (i = 0; i < nr; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 620 | usb_free_urb(acm->ru[i].urb); |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 621 | kfree(acm->country_codes); |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 622 | kfree(acm); |
| 623 | } |
| 624 | |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 625 | static int acm_tty_chars_in_buffer(struct tty_struct *tty); |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | static void acm_tty_close(struct tty_struct *tty, struct file *filp) |
| 628 | { |
| 629 | struct acm *acm = tty->driver_data; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 630 | int i,nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | if (!acm || !acm->used) |
| 633 | return; |
| 634 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 635 | nr = acm->rx_buflimit; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 636 | mutex_lock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (!--acm->used) { |
| 638 | if (acm->dev) { |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 639 | usb_autopm_get_interface(acm->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | acm_set_control(acm, acm->ctrlout = 0); |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 641 | |
| 642 | /* try letting the last writes drain naturally */ |
| 643 | wait_event_interruptible_timeout(acm->drain_wait, |
| 644 | (ACM_NW == acm_wb_is_avail(acm)) |
| 645 | || !acm->dev, |
| 646 | ACM_CLOSE_TIMEOUT * HZ); |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | usb_kill_urb(acm->ctrlurb); |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 649 | for (i = 0; i < ACM_NW; i++) |
| 650 | usb_kill_urb(acm->wb[i].urb); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 651 | for (i = 0; i < nr; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 652 | usb_kill_urb(acm->ru[i].urb); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 653 | acm->control->needs_remote_wakeup = 0; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 654 | usb_autopm_put_interface(acm->control); |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 655 | } else |
| 656 | acm_tty_unregister(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 658 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 662 | { |
| 663 | struct acm *acm = tty->driver_data; |
| 664 | int stat; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 665 | unsigned long flags; |
| 666 | int wbn; |
| 667 | struct acm_wb *wb; |
| 668 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 669 | dbg("Entering acm_tty_write to write %d bytes,", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
| 671 | if (!ACM_READY(acm)) |
| 672 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (!count) |
| 674 | return 0; |
| 675 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 676 | spin_lock_irqsave(&acm->write_lock, flags); |
| 677 | if ((wbn = acm_wb_alloc(acm)) < 0) { |
| 678 | spin_unlock_irqrestore(&acm->write_lock, flags); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 679 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 681 | wb = &acm->wb[wbn]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 683 | count = (count > acm->writesize) ? acm->writesize : count; |
| 684 | dbg("Get %d bytes...", count); |
| 685 | memcpy(wb->buf, buf, count); |
| 686 | wb->len = count; |
| 687 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 688 | |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 689 | if ((stat = acm_write_start(acm, wbn)) < 0) |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 690 | return stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return count; |
| 692 | } |
| 693 | |
| 694 | static int acm_tty_write_room(struct tty_struct *tty) |
| 695 | { |
| 696 | struct acm *acm = tty->driver_data; |
| 697 | if (!ACM_READY(acm)) |
| 698 | return -EINVAL; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 699 | /* |
| 700 | * Do not let the line discipline to know that we have a reserve, |
| 701 | * or it might get too enthusiastic. |
| 702 | */ |
David Brownell | 934da46 | 2008-08-06 18:44:12 -0700 | [diff] [blame] | 703 | return acm_wb_is_avail(acm) ? acm->writesize : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int acm_tty_chars_in_buffer(struct tty_struct *tty) |
| 707 | { |
| 708 | struct acm *acm = tty->driver_data; |
| 709 | if (!ACM_READY(acm)) |
| 710 | return -EINVAL; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 711 | /* |
| 712 | * This is inaccurate (overcounts), but it works. |
| 713 | */ |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 714 | return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static void acm_tty_throttle(struct tty_struct *tty) |
| 718 | { |
| 719 | struct acm *acm = tty->driver_data; |
| 720 | if (!ACM_READY(acm)) |
| 721 | return; |
| 722 | spin_lock_bh(&acm->throttle_lock); |
| 723 | acm->throttle = 1; |
| 724 | spin_unlock_bh(&acm->throttle_lock); |
| 725 | } |
| 726 | |
| 727 | static void acm_tty_unthrottle(struct tty_struct *tty) |
| 728 | { |
| 729 | struct acm *acm = tty->driver_data; |
| 730 | if (!ACM_READY(acm)) |
| 731 | return; |
| 732 | spin_lock_bh(&acm->throttle_lock); |
| 733 | acm->throttle = 0; |
| 734 | spin_unlock_bh(&acm->throttle_lock); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 735 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 738 | static int acm_tty_break_ctl(struct tty_struct *tty, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | struct acm *acm = tty->driver_data; |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 741 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (!ACM_READY(acm)) |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 743 | return -EINVAL; |
| 744 | retval = acm_send_break(acm, state ? 0xffff : 0); |
| 745 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | dbg("send break failed"); |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 747 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file) |
| 751 | { |
| 752 | struct acm *acm = tty->driver_data; |
| 753 | |
| 754 | if (!ACM_READY(acm)) |
| 755 | return -EINVAL; |
| 756 | |
| 757 | return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | |
| 758 | (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | |
| 759 | (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | |
| 760 | (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | |
| 761 | (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | |
| 762 | TIOCM_CTS; |
| 763 | } |
| 764 | |
| 765 | static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file, |
| 766 | unsigned int set, unsigned int clear) |
| 767 | { |
| 768 | struct acm *acm = tty->driver_data; |
| 769 | unsigned int newctrl; |
| 770 | |
| 771 | if (!ACM_READY(acm)) |
| 772 | return -EINVAL; |
| 773 | |
| 774 | newctrl = acm->ctrlout; |
| 775 | set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); |
| 776 | clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); |
| 777 | |
| 778 | newctrl = (newctrl & ~clear) | set; |
| 779 | |
| 780 | if (acm->ctrlout == newctrl) |
| 781 | return 0; |
| 782 | return acm_set_control(acm, acm->ctrlout = newctrl); |
| 783 | } |
| 784 | |
| 785 | static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) |
| 786 | { |
| 787 | struct acm *acm = tty->driver_data; |
| 788 | |
| 789 | if (!ACM_READY(acm)) |
| 790 | return -EINVAL; |
| 791 | |
| 792 | return -ENOIOCTLCMD; |
| 793 | } |
| 794 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 795 | static const __u32 acm_tty_speed[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | 0, 50, 75, 110, 134, 150, 200, 300, 600, |
| 797 | 1200, 1800, 2400, 4800, 9600, 19200, 38400, |
| 798 | 57600, 115200, 230400, 460800, 500000, 576000, |
| 799 | 921600, 1000000, 1152000, 1500000, 2000000, |
| 800 | 2500000, 3000000, 3500000, 4000000 |
| 801 | }; |
| 802 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 803 | static const __u8 acm_tty_size[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | 5, 6, 7, 8 |
| 805 | }; |
| 806 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 807 | static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
| 809 | struct acm *acm = tty->driver_data; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 810 | struct ktermios *termios = tty->termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | struct usb_cdc_line_coding newline; |
| 812 | int newctrl = acm->ctrlout; |
| 813 | |
| 814 | if (!ACM_READY(acm)) |
| 815 | return; |
| 816 | |
| 817 | newline.dwDTERate = cpu_to_le32p(acm_tty_speed + |
| 818 | (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0)); |
| 819 | newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; |
| 820 | newline.bParityType = termios->c_cflag & PARENB ? |
| 821 | (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; |
| 822 | newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4]; |
| 823 | |
| 824 | acm->clocal = ((termios->c_cflag & CLOCAL) != 0); |
| 825 | |
| 826 | if (!newline.dwDTERate) { |
| 827 | newline.dwDTERate = acm->line.dwDTERate; |
| 828 | newctrl &= ~ACM_CTRL_DTR; |
| 829 | } else newctrl |= ACM_CTRL_DTR; |
| 830 | |
| 831 | if (newctrl != acm->ctrlout) |
| 832 | acm_set_control(acm, acm->ctrlout = newctrl); |
| 833 | |
| 834 | if (memcmp(&acm->line, &newline, sizeof newline)) { |
| 835 | memcpy(&acm->line, &newline, sizeof newline); |
| 836 | dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate), |
| 837 | newline.bCharFormat, newline.bParityType, |
| 838 | newline.bDataBits); |
| 839 | acm_set_line(acm, &acm->line); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * USB probe and disconnect routines. |
| 845 | */ |
| 846 | |
Oliver Neukum | 830f402 | 2008-06-25 14:17:16 +0200 | [diff] [blame] | 847 | /* Little helpers: write/read buffers free */ |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 848 | static void acm_write_buffers_free(struct acm *acm) |
| 849 | { |
| 850 | int i; |
| 851 | struct acm_wb *wb; |
| 852 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 853 | for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 854 | usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah); |
| 855 | } |
| 856 | } |
| 857 | |
Oliver Neukum | 830f402 | 2008-06-25 14:17:16 +0200 | [diff] [blame] | 858 | static void acm_read_buffers_free(struct acm *acm) |
| 859 | { |
| 860 | struct usb_device *usb_dev = interface_to_usbdev(acm->control); |
| 861 | int i, n = acm->rx_buflimit; |
| 862 | |
| 863 | for (i = 0; i < n; i++) |
| 864 | usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); |
| 865 | } |
| 866 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 867 | /* Little helper: write buffers allocate */ |
| 868 | static int acm_write_buffers_alloc(struct acm *acm) |
| 869 | { |
| 870 | int i; |
| 871 | struct acm_wb *wb; |
| 872 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 873 | for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 874 | wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL, |
| 875 | &wb->dmah); |
| 876 | if (!wb->buf) { |
| 877 | while (i != 0) { |
| 878 | --i; |
| 879 | --wb; |
| 880 | usb_buffer_free(acm->dev, acm->writesize, |
| 881 | wb->buf, wb->dmah); |
| 882 | } |
| 883 | return -ENOMEM; |
| 884 | } |
| 885 | } |
| 886 | return 0; |
| 887 | } |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | static int acm_probe (struct usb_interface *intf, |
| 890 | const struct usb_device_id *id) |
| 891 | { |
| 892 | struct usb_cdc_union_desc *union_header = NULL; |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 893 | struct usb_cdc_country_functional_desc *cfd = NULL; |
David Brownell | c6dbf55 | 2008-04-13 14:00:44 -0700 | [diff] [blame] | 894 | unsigned char *buffer = intf->altsetting->extra; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | int buflen = intf->altsetting->extralen; |
| 896 | struct usb_interface *control_interface; |
| 897 | struct usb_interface *data_interface; |
| 898 | struct usb_endpoint_descriptor *epctrl; |
| 899 | struct usb_endpoint_descriptor *epread; |
| 900 | struct usb_endpoint_descriptor *epwrite; |
| 901 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 902 | struct acm *acm; |
| 903 | int minor; |
| 904 | int ctrlsize,readsize; |
| 905 | u8 *buf; |
| 906 | u8 ac_management_function = 0; |
| 907 | u8 call_management_function = 0; |
| 908 | int call_interface_num = -1; |
| 909 | int data_interface_num; |
| 910 | unsigned long quirks; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 911 | int num_rx_buf; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 912 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 914 | /* normal quirks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | quirks = (unsigned long)id->driver_info; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 916 | num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR; |
| 917 | |
| 918 | /* handle quirks deadly to normal probing*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (quirks == NO_UNION_NORMAL) { |
| 920 | data_interface = usb_ifnum_to_if(usb_dev, 1); |
| 921 | control_interface = usb_ifnum_to_if(usb_dev, 0); |
| 922 | goto skip_normal_probe; |
| 923 | } |
| 924 | |
| 925 | /* normal probing*/ |
| 926 | if (!buffer) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 927 | dev_err(&intf->dev, "Weird descriptor references\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | return -EINVAL; |
| 929 | } |
| 930 | |
| 931 | if (!buflen) { |
| 932 | if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 933 | dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | buflen = intf->cur_altsetting->endpoint->extralen; |
| 935 | buffer = intf->cur_altsetting->endpoint->extra; |
| 936 | } else { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 937 | dev_err(&intf->dev, |
| 938 | "Zero length descriptor references\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | return -EINVAL; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | while (buflen > 0) { |
| 944 | if (buffer [1] != USB_DT_CS_INTERFACE) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 945 | dev_err(&intf->dev, "skipping garbage\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto next_desc; |
| 947 | } |
| 948 | |
| 949 | switch (buffer [2]) { |
| 950 | case USB_CDC_UNION_TYPE: /* we've found it */ |
| 951 | if (union_header) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 952 | dev_err(&intf->dev, "More than one " |
| 953 | "union descriptor, " |
| 954 | "skipping ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | goto next_desc; |
| 956 | } |
| 957 | union_header = (struct usb_cdc_union_desc *) |
| 958 | buffer; |
| 959 | break; |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 960 | case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/ |
| 961 | cfd = (struct usb_cdc_country_functional_desc *)buffer; |
| 962 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | case USB_CDC_HEADER_TYPE: /* maybe check version */ |
| 964 | break; /* for now we ignore it */ |
| 965 | case USB_CDC_ACM_TYPE: |
| 966 | ac_management_function = buffer[3]; |
| 967 | break; |
| 968 | case USB_CDC_CALL_MANAGEMENT_TYPE: |
| 969 | call_management_function = buffer[3]; |
| 970 | call_interface_num = buffer[4]; |
| 971 | if ((call_management_function & 3) != 3) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 972 | dev_err(&intf->dev, "This device " |
| 973 | "cannot do calls on its own. " |
| 974 | "It is no modem.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | default: |
David Brownell | c6dbf55 | 2008-04-13 14:00:44 -0700 | [diff] [blame] | 977 | /* there are LOTS more CDC descriptors that |
| 978 | * could legitimately be found here. |
| 979 | */ |
| 980 | dev_dbg(&intf->dev, "Ignoring descriptor: " |
| 981 | "type %02x, length %d\n", |
| 982 | buffer[2], buffer[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | break; |
| 984 | } |
| 985 | next_desc: |
| 986 | buflen -= buffer[0]; |
| 987 | buffer += buffer[0]; |
| 988 | } |
| 989 | |
| 990 | if (!union_header) { |
| 991 | if (call_interface_num > 0) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 992 | dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); |
| 994 | control_interface = intf; |
| 995 | } else { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 996 | dev_dbg(&intf->dev,"No union descriptor, giving up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return -ENODEV; |
| 998 | } |
| 999 | } else { |
| 1000 | control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); |
| 1001 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); |
| 1002 | if (!control_interface || !data_interface) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1003 | dev_dbg(&intf->dev,"no interfaces\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | return -ENODEV; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | if (data_interface_num != call_interface_num) |
Joe Perches | dc0d5c1 | 2007-12-17 11:40:18 -0800 | [diff] [blame] | 1009 | dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | skip_normal_probe: |
| 1012 | |
| 1013 | /*workaround for switched interfaces */ |
| 1014 | if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) { |
| 1015 | if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) { |
| 1016 | struct usb_interface *t; |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1017 | dev_dbg(&intf->dev,"Your device has switched interfaces.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | t = control_interface; |
| 1020 | control_interface = data_interface; |
| 1021 | data_interface = t; |
| 1022 | } else { |
| 1023 | return -EINVAL; |
| 1024 | } |
| 1025 | } |
Alan Stern | 74da5d6 | 2007-08-02 13:29:10 -0400 | [diff] [blame] | 1026 | |
| 1027 | /* Accept probe requests only for the control interface */ |
| 1028 | if (intf != control_interface) |
| 1029 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | if (usb_interface_claimed(data_interface)) { /* valid in this context */ |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1032 | dev_dbg(&intf->dev,"The data interface isn't available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | return -EBUSY; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | if (data_interface->cur_altsetting->desc.bNumEndpoints < 2) |
| 1038 | return -EINVAL; |
| 1039 | |
| 1040 | epctrl = &control_interface->cur_altsetting->endpoint[0].desc; |
| 1041 | epread = &data_interface->cur_altsetting->endpoint[0].desc; |
| 1042 | epwrite = &data_interface->cur_altsetting->endpoint[1].desc; |
| 1043 | |
| 1044 | |
| 1045 | /* workaround for switched endpoints */ |
Luiz Fernando N. Capitulino | 45aea70 | 2006-10-26 13:02:48 -0300 | [diff] [blame] | 1046 | if (!usb_endpoint_dir_in(epread)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* descriptors are swapped */ |
| 1048 | struct usb_endpoint_descriptor *t; |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1049 | dev_dbg(&intf->dev,"The data interface has switched endpoints\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | t = epread; |
| 1052 | epread = epwrite; |
| 1053 | epwrite = t; |
| 1054 | } |
| 1055 | dbg("interfaces are valid"); |
| 1056 | for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++); |
| 1057 | |
| 1058 | if (minor == ACM_TTY_MINORS) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame^] | 1059 | dev_err(&intf->dev, "no more free acm devices\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return -ENODEV; |
| 1061 | } |
| 1062 | |
Oliver Neukum | 46f116e | 2005-10-24 22:42:35 +0200 | [diff] [blame] | 1063 | if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1064 | dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | goto alloc_fail; |
| 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1069 | readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2); |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 1070 | acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | acm->control = control_interface; |
| 1072 | acm->data = data_interface; |
| 1073 | acm->minor = minor; |
| 1074 | acm->dev = usb_dev; |
| 1075 | acm->ctrl_caps = ac_management_function; |
| 1076 | acm->ctrlsize = ctrlsize; |
| 1077 | acm->readsize = readsize; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1078 | acm->rx_buflimit = num_rx_buf; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1079 | acm->urb_task.func = acm_rx_tasklet; |
| 1080 | acm->urb_task.data = (unsigned long) acm; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1081 | INIT_WORK(&acm->work, acm_softint); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1082 | INIT_WORK(&acm->waker, acm_waker); |
David Brownell | e5fbab5 | 2008-08-06 18:46:10 -0700 | [diff] [blame] | 1083 | init_waitqueue_head(&acm->drain_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | spin_lock_init(&acm->throttle_lock); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1085 | spin_lock_init(&acm->write_lock); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1086 | spin_lock_init(&acm->read_lock); |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1087 | mutex_init(&acm->mutex); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1088 | acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); |
| 1091 | if (!buf) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1092 | dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | goto alloc_fail2; |
| 1094 | } |
| 1095 | acm->ctrl_buffer = buf; |
| 1096 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1097 | if (acm_write_buffers_alloc(acm) < 0) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1098 | dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | goto alloc_fail4; |
| 1100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
| 1102 | acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); |
| 1103 | if (!acm->ctrlurb) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1104 | dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | goto alloc_fail5; |
| 1106 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1107 | for (i = 0; i < num_rx_buf; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1108 | struct acm_ru *rcv = &(acm->ru[i]); |
| 1109 | |
| 1110 | if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1111 | dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n"); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1112 | goto alloc_fail7; |
| 1113 | } |
| 1114 | |
| 1115 | rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1116 | rcv->instance = acm; |
| 1117 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1118 | for (i = 0; i < num_rx_buf; i++) { |
David Brownell | 672c4e1 | 2008-08-06 18:41:12 -0700 | [diff] [blame] | 1119 | struct acm_rb *rb = &(acm->rb[i]); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1120 | |
David Brownell | 672c4e1 | 2008-08-06 18:41:12 -0700 | [diff] [blame] | 1121 | rb->base = usb_buffer_alloc(acm->dev, readsize, |
| 1122 | GFP_KERNEL, &rb->dma); |
| 1123 | if (!rb->base) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1124 | dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n"); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1125 | goto alloc_fail7; |
| 1126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 1128 | for(i = 0; i < ACM_NW; i++) |
| 1129 | { |
| 1130 | struct acm_wb *snd = &(acm->wb[i]); |
| 1131 | |
| 1132 | if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) { |
| 1133 | dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)"); |
| 1134 | goto alloc_fail7; |
| 1135 | } |
| 1136 | |
| 1137 | usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), |
| 1138 | NULL, acm->writesize, acm_write_bulk, snd); |
| 1139 | snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1140 | snd->instance = acm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 1143 | usb_set_intfdata (intf, acm); |
| 1144 | |
| 1145 | i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); |
| 1146 | if (i < 0) |
| 1147 | goto alloc_fail8; |
| 1148 | |
| 1149 | if (cfd) { /* export the country data */ |
| 1150 | acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL); |
| 1151 | if (!acm->country_codes) |
| 1152 | goto skip_countries; |
| 1153 | acm->country_code_size = cfd->bLength - 4; |
| 1154 | memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4); |
| 1155 | acm->country_rel_date = cfd->iCountryCodeRelDate; |
| 1156 | |
| 1157 | i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); |
| 1158 | if (i < 0) { |
| 1159 | kfree(acm->country_codes); |
| 1160 | goto skip_countries; |
| 1161 | } |
| 1162 | |
| 1163 | i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate); |
| 1164 | if (i < 0) { |
| 1165 | kfree(acm->country_codes); |
| 1166 | goto skip_countries; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | skip_countries: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), |
| 1172 | acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); |
| 1173 | acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1174 | acm->ctrlurb->transfer_dma = acm->ctrl_dma; |
| 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); |
| 1177 | |
| 1178 | acm_set_control(acm, acm->ctrlout); |
| 1179 | |
| 1180 | acm->line.dwDTERate = cpu_to_le32(9600); |
| 1181 | acm->line.bDataBits = 8; |
| 1182 | acm_set_line(acm, &acm->line); |
| 1183 | |
| 1184 | usb_driver_claim_interface(&acm_driver, data_interface, acm); |
David Brownell | 672c4e1 | 2008-08-06 18:41:12 -0700 | [diff] [blame] | 1185 | usb_set_intfdata(data_interface, acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1187 | usb_get_intf(control_interface); |
| 1188 | tty_register_device(acm_tty_driver, minor, &control_interface->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | acm_table[minor] = acm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 1192 | return 0; |
| 1193 | alloc_fail8: |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 1194 | for (i = 0; i < ACM_NW; i++) |
| 1195 | usb_free_urb(acm->wb[i].urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | alloc_fail7: |
Oliver Neukum | 830f402 | 2008-06-25 14:17:16 +0200 | [diff] [blame] | 1197 | acm_read_buffers_free(acm); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1198 | for (i = 0; i < num_rx_buf; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1199 | usb_free_urb(acm->ru[i].urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | usb_free_urb(acm->ctrlurb); |
| 1201 | alloc_fail5: |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1202 | acm_write_buffers_free(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | alloc_fail4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); |
| 1205 | alloc_fail2: |
| 1206 | kfree(acm); |
| 1207 | alloc_fail: |
| 1208 | return -ENOMEM; |
| 1209 | } |
| 1210 | |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1211 | static void stop_data_traffic(struct acm *acm) |
| 1212 | { |
| 1213 | int i; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1214 | dbg("Entering stop_data_traffic"); |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1215 | |
| 1216 | tasklet_disable(&acm->urb_task); |
| 1217 | |
| 1218 | usb_kill_urb(acm->ctrlurb); |
David Engraf | e4cf3aa | 2008-03-20 10:01:34 +0100 | [diff] [blame] | 1219 | for(i = 0; i < ACM_NW; i++) |
| 1220 | usb_kill_urb(acm->wb[i].urb); |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1221 | for (i = 0; i < acm->rx_buflimit; i++) |
| 1222 | usb_kill_urb(acm->ru[i].urb); |
| 1223 | |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1224 | tasklet_enable(&acm->urb_task); |
| 1225 | |
| 1226 | cancel_work_sync(&acm->work); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1227 | cancel_work_sync(&acm->waker); |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | static void acm_disconnect(struct usb_interface *intf) |
| 1231 | { |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 1232 | struct acm *acm = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
David Brownell | 672c4e1 | 2008-08-06 18:41:12 -0700 | [diff] [blame] | 1235 | /* sibling interface is already cleaning up */ |
| 1236 | if (!acm) |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1237 | return; |
David Brownell | 672c4e1 | 2008-08-06 18:41:12 -0700 | [diff] [blame] | 1238 | |
| 1239 | mutex_lock(&open_mutex); |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 1240 | if (acm->country_codes){ |
Alan Stern | 74da5d6 | 2007-08-02 13:29:10 -0400 | [diff] [blame] | 1241 | device_remove_file(&acm->control->dev, |
| 1242 | &dev_attr_wCountryCodes); |
| 1243 | device_remove_file(&acm->control->dev, |
| 1244 | &dev_attr_iCountryCodeRelDate); |
Oliver Neukum | c4cabd2 | 2007-02-27 15:28:55 +0100 | [diff] [blame] | 1245 | } |
Alan Stern | 74da5d6 | 2007-08-02 13:29:10 -0400 | [diff] [blame] | 1246 | device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | acm->dev = NULL; |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1248 | usb_set_intfdata(acm->control, NULL); |
| 1249 | usb_set_intfdata(acm->data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1251 | stop_data_traffic(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1253 | acm_write_buffers_free(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); |
Oliver Neukum | 830f402 | 2008-06-25 14:17:16 +0200 | [diff] [blame] | 1255 | acm_read_buffers_free(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Oliver Neukum | 830f402 | 2008-06-25 14:17:16 +0200 | [diff] [blame] | 1257 | usb_driver_release_interface(&acm_driver, intf == acm->control ? |
| 1258 | acm->data : acm->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
| 1260 | if (!acm->used) { |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1261 | acm_tty_unregister(acm); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1262 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1266 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | if (acm->tty) |
| 1269 | tty_hangup(acm->tty); |
| 1270 | } |
| 1271 | |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1272 | #ifdef CONFIG_PM |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1273 | static int acm_suspend(struct usb_interface *intf, pm_message_t message) |
| 1274 | { |
| 1275 | struct acm *acm = usb_get_intfdata(intf); |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1276 | int cnt; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1277 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1278 | if (acm->dev->auto_pm) { |
| 1279 | int b; |
| 1280 | |
| 1281 | spin_lock_irq(&acm->read_lock); |
| 1282 | spin_lock(&acm->write_lock); |
| 1283 | b = acm->processing + acm->transmitting; |
| 1284 | spin_unlock(&acm->write_lock); |
| 1285 | spin_unlock_irq(&acm->read_lock); |
| 1286 | if (b) |
| 1287 | return -EBUSY; |
| 1288 | } |
| 1289 | |
| 1290 | spin_lock_irq(&acm->read_lock); |
| 1291 | spin_lock(&acm->write_lock); |
| 1292 | cnt = acm->susp_count++; |
| 1293 | spin_unlock(&acm->write_lock); |
| 1294 | spin_unlock_irq(&acm->read_lock); |
| 1295 | |
| 1296 | if (cnt) |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1297 | return 0; |
| 1298 | /* |
| 1299 | we treat opened interfaces differently, |
| 1300 | we must guard against open |
| 1301 | */ |
| 1302 | mutex_lock(&acm->mutex); |
| 1303 | |
| 1304 | if (acm->used) |
| 1305 | stop_data_traffic(acm); |
| 1306 | |
| 1307 | mutex_unlock(&acm->mutex); |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static int acm_resume(struct usb_interface *intf) |
| 1312 | { |
| 1313 | struct acm *acm = usb_get_intfdata(intf); |
| 1314 | int rv = 0; |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1315 | int cnt; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1316 | |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1317 | spin_lock_irq(&acm->read_lock); |
| 1318 | acm->susp_count -= 1; |
| 1319 | cnt = acm->susp_count; |
| 1320 | spin_unlock_irq(&acm->read_lock); |
| 1321 | |
| 1322 | if (cnt) |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1323 | return 0; |
| 1324 | |
| 1325 | mutex_lock(&acm->mutex); |
| 1326 | if (acm->used) { |
| 1327 | rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); |
| 1328 | if (rv < 0) |
Oliver Neukum | 11ea859 | 2008-06-20 11:25:57 +0200 | [diff] [blame] | 1329 | goto err_out; |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1330 | |
| 1331 | tasklet_schedule(&acm->urb_task); |
| 1332 | } |
| 1333 | |
| 1334 | err_out: |
| 1335 | mutex_unlock(&acm->mutex); |
| 1336 | return rv; |
| 1337 | } |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1338 | |
| 1339 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * USB driver structure. |
| 1342 | */ |
| 1343 | |
| 1344 | static struct usb_device_id acm_ids[] = { |
| 1345 | /* quirky and broken devices */ |
| 1346 | { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ |
| 1347 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1348 | }, |
Andrey Arapov | b0e2a70 | 2007-07-04 17:11:42 +0200 | [diff] [blame] | 1349 | { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */ |
| 1350 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1351 | }, |
Masahito Omote | 8753e65 | 2005-07-29 12:17:25 -0700 | [diff] [blame] | 1352 | { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */ |
| 1353 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1354 | }, |
Chris Malley | 91a9c92 | 2006-10-03 10:08:28 +0100 | [diff] [blame] | 1355 | { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */ |
| 1356 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1357 | }, |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1358 | { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */ |
| 1359 | .driver_info = SINGLE_RX_URB, /* firmware bug */ |
| 1360 | }, |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 1361 | { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */ |
| 1362 | .driver_info = SINGLE_RX_URB, /* firmware bug */ |
| 1363 | }, |
Oliver Neukum | 9be8456 | 2007-02-12 08:50:03 +0100 | [diff] [blame] | 1364 | { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */ |
| 1365 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1366 | }, |
Iain McFarlane | 6149ed5 | 2008-05-04 00:13:49 +0100 | [diff] [blame] | 1367 | { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */ |
| 1368 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1369 | }, |
Eric Sandeen | c8fd2c3 | 2008-08-14 08:25:40 -0500 | [diff] [blame] | 1370 | { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */ |
| 1371 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1372 | }, |
Oliver Neukum | 9be8456 | 2007-02-12 08:50:03 +0100 | [diff] [blame] | 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | /* control interfaces with various AT-command sets */ |
| 1375 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1376 | USB_CDC_ACM_PROTO_AT_V25TER) }, |
| 1377 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1378 | USB_CDC_ACM_PROTO_AT_PCCA101) }, |
| 1379 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1380 | USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, |
| 1381 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1382 | USB_CDC_ACM_PROTO_AT_GSM) }, |
| 1383 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1384 | USB_CDC_ACM_PROTO_AT_3G ) }, |
| 1385 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1386 | USB_CDC_ACM_PROTO_AT_CDMA) }, |
| 1387 | |
| 1388 | /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */ |
| 1389 | { } |
| 1390 | }; |
| 1391 | |
| 1392 | MODULE_DEVICE_TABLE (usb, acm_ids); |
| 1393 | |
| 1394 | static struct usb_driver acm_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | .name = "cdc_acm", |
| 1396 | .probe = acm_probe, |
| 1397 | .disconnect = acm_disconnect, |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1398 | #ifdef CONFIG_PM |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1399 | .suspend = acm_suspend, |
| 1400 | .resume = acm_resume, |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1401 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | .id_table = acm_ids, |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1403 | #ifdef CONFIG_PM |
Oliver Neukum | 1365baf | 2007-10-12 17:24:28 +0200 | [diff] [blame] | 1404 | .supports_autosuspend = 1, |
Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 1405 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | }; |
| 1407 | |
| 1408 | /* |
| 1409 | * TTY driver structures. |
| 1410 | */ |
| 1411 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1412 | static const struct tty_operations acm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | .open = acm_tty_open, |
| 1414 | .close = acm_tty_close, |
| 1415 | .write = acm_tty_write, |
| 1416 | .write_room = acm_tty_write_room, |
| 1417 | .ioctl = acm_tty_ioctl, |
| 1418 | .throttle = acm_tty_throttle, |
| 1419 | .unthrottle = acm_tty_unthrottle, |
| 1420 | .chars_in_buffer = acm_tty_chars_in_buffer, |
| 1421 | .break_ctl = acm_tty_break_ctl, |
| 1422 | .set_termios = acm_tty_set_termios, |
| 1423 | .tiocmget = acm_tty_tiocmget, |
| 1424 | .tiocmset = acm_tty_tiocmset, |
| 1425 | }; |
| 1426 | |
| 1427 | /* |
| 1428 | * Init / exit. |
| 1429 | */ |
| 1430 | |
| 1431 | static int __init acm_init(void) |
| 1432 | { |
| 1433 | int retval; |
| 1434 | acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); |
| 1435 | if (!acm_tty_driver) |
| 1436 | return -ENOMEM; |
| 1437 | acm_tty_driver->owner = THIS_MODULE, |
| 1438 | acm_tty_driver->driver_name = "acm", |
| 1439 | acm_tty_driver->name = "ttyACM", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | acm_tty_driver->major = ACM_TTY_MAJOR, |
| 1441 | acm_tty_driver->minor_start = 0, |
| 1442 | acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, |
| 1443 | acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1444 | acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | acm_tty_driver->init_termios = tty_std_termios; |
| 1446 | acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1447 | tty_set_operations(acm_tty_driver, &acm_ops); |
| 1448 | |
| 1449 | retval = tty_register_driver(acm_tty_driver); |
| 1450 | if (retval) { |
| 1451 | put_tty_driver(acm_tty_driver); |
| 1452 | return retval; |
| 1453 | } |
| 1454 | |
| 1455 | retval = usb_register(&acm_driver); |
| 1456 | if (retval) { |
| 1457 | tty_unregister_driver(acm_tty_driver); |
| 1458 | put_tty_driver(acm_tty_driver); |
| 1459 | return retval; |
| 1460 | } |
| 1461 | |
Greg Kroah-Hartman | 5909f6e | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 1462 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 1463 | DRIVER_DESC "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | static void __exit acm_exit(void) |
| 1469 | { |
| 1470 | usb_deregister(&acm_driver); |
| 1471 | tty_unregister_driver(acm_tty_driver); |
| 1472 | put_tty_driver(acm_tty_driver); |
| 1473 | } |
| 1474 | |
| 1475 | module_init(acm_init); |
| 1476 | module_exit(acm_exit); |
| 1477 | |
| 1478 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 1479 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 1480 | MODULE_LICENSE("GPL"); |
| 1481 | |