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