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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * This program is free software; you can redistribute it and/or modify |
| 38 | * it under the terms of the GNU General Public License as published by |
| 39 | * the Free Software Foundation; either version 2 of the License, or |
| 40 | * (at your option) any later version. |
| 41 | * |
| 42 | * This program is distributed in the hope that it will be useful, |
| 43 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 | * GNU General Public License for more details. |
| 46 | * |
| 47 | * You should have received a copy of the GNU General Public License |
| 48 | * along with this program; if not, write to the Free Software |
| 49 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 50 | */ |
| 51 | |
| 52 | #undef DEBUG |
| 53 | |
| 54 | #include <linux/kernel.h> |
| 55 | #include <linux/errno.h> |
| 56 | #include <linux/init.h> |
| 57 | #include <linux/slab.h> |
| 58 | #include <linux/tty.h> |
| 59 | #include <linux/tty_driver.h> |
| 60 | #include <linux/tty_flip.h> |
| 61 | #include <linux/module.h> |
| 62 | #include <linux/smp_lock.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 Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 76 | #define DRIVER_VERSION "v0.25" |
| 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 | |
| 122 | wbn = acm->write_current; |
| 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 | |
| 136 | static void acm_wb_free(struct acm *acm, int wbn) |
| 137 | { |
| 138 | acm->wb[wbn].use = 0; |
| 139 | } |
| 140 | |
| 141 | static int acm_wb_is_avail(struct acm *acm) |
| 142 | { |
| 143 | int i, n; |
| 144 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 145 | n = ACM_NW; |
| 146 | for (i = 0; i < ACM_NW; i++) { |
| 147 | n -= acm->wb[i].use; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 148 | } |
| 149 | return n; |
| 150 | } |
| 151 | |
| 152 | static inline int acm_wb_is_used(struct acm *acm, int wbn) |
| 153 | { |
| 154 | return acm->wb[wbn].use; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Finish write. |
| 159 | */ |
| 160 | static void acm_write_done(struct acm *acm) |
| 161 | { |
| 162 | unsigned long flags; |
| 163 | int wbn; |
| 164 | |
| 165 | spin_lock_irqsave(&acm->write_lock, flags); |
| 166 | acm->write_ready = 1; |
| 167 | wbn = acm->write_current; |
| 168 | acm_wb_free(acm, wbn); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 169 | acm->write_current = (wbn + 1) % ACM_NW; |
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. |
| 175 | */ |
| 176 | static int acm_write_start(struct acm *acm) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | int wbn; |
| 180 | struct acm_wb *wb; |
| 181 | int rc; |
| 182 | |
| 183 | spin_lock_irqsave(&acm->write_lock, flags); |
| 184 | if (!acm->dev) { |
| 185 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | |
| 189 | if (!acm->write_ready) { |
| 190 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 191 | return 0; /* A white lie */ |
| 192 | } |
| 193 | |
| 194 | wbn = acm->write_current; |
| 195 | if (!acm_wb_is_used(acm, wbn)) { |
| 196 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 197 | return 0; |
| 198 | } |
| 199 | wb = &acm->wb[wbn]; |
| 200 | |
| 201 | acm->write_ready = 0; |
| 202 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 203 | |
| 204 | acm->writeurb->transfer_buffer = wb->buf; |
| 205 | acm->writeurb->transfer_dma = wb->dmah; |
| 206 | acm->writeurb->transfer_buffer_length = wb->len; |
| 207 | acm->writeurb->dev = acm->dev; |
| 208 | |
| 209 | if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) { |
| 210 | dbg("usb_submit_urb(write bulk) failed: %d", rc); |
| 211 | acm_write_done(acm); |
| 212 | } |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | * Interrupt handlers for various ACM device responses |
| 218 | */ |
| 219 | |
| 220 | /* control interface reports status changes with "interrupt" transfers */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 221 | static void acm_ctrl_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | struct acm *acm = urb->context; |
| 224 | struct usb_cdc_notification *dr = urb->transfer_buffer; |
| 225 | unsigned char *data; |
| 226 | int newctrl; |
| 227 | int status; |
| 228 | |
| 229 | switch (urb->status) { |
| 230 | case 0: |
| 231 | /* success */ |
| 232 | break; |
| 233 | case -ECONNRESET: |
| 234 | case -ENOENT: |
| 235 | case -ESHUTDOWN: |
| 236 | /* this urb is terminated, clean up */ |
| 237 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); |
| 238 | return; |
| 239 | default: |
| 240 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); |
| 241 | goto exit; |
| 242 | } |
| 243 | |
| 244 | if (!ACM_READY(acm)) |
| 245 | goto exit; |
| 246 | |
| 247 | data = (unsigned char *)(dr + 1); |
| 248 | switch (dr->bNotificationType) { |
| 249 | |
| 250 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 251 | |
| 252 | dbg("%s network", dr->wValue ? "connected to" : "disconnected from"); |
| 253 | break; |
| 254 | |
| 255 | case USB_CDC_NOTIFY_SERIAL_STATE: |
| 256 | |
| 257 | newctrl = le16_to_cpu(get_unaligned((__le16 *) data)); |
| 258 | |
| 259 | if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { |
| 260 | dbg("calling hangup"); |
| 261 | tty_hangup(acm->tty); |
| 262 | } |
| 263 | |
| 264 | acm->ctrlin = newctrl; |
| 265 | |
| 266 | dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c", |
| 267 | acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', |
| 268 | acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-', |
| 269 | acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', |
| 270 | acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); |
| 271 | |
| 272 | break; |
| 273 | |
| 274 | default: |
| 275 | dbg("unknown notification %d received: index %d len %d data0 %d data1 %d", |
| 276 | dr->bNotificationType, dr->wIndex, |
| 277 | dr->wLength, data[0], data[1]); |
| 278 | break; |
| 279 | } |
| 280 | exit: |
| 281 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 282 | if (status) |
| 283 | err ("%s - usb_submit_urb failed with result %d", |
| 284 | __FUNCTION__, status); |
| 285 | } |
| 286 | |
| 287 | /* data interface returns incoming bytes, or we got unthrottled */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 288 | static void acm_read_bulk(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 290 | struct acm_rb *buf; |
| 291 | struct acm_ru *rcv = urb->context; |
| 292 | struct acm *acm = rcv->instance; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 293 | int status = urb->status; |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 294 | dbg("Entering acm_read_bulk with status %d", urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | if (!ACM_READY(acm)) |
| 297 | return; |
| 298 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 299 | if (status) |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 300 | dev_dbg(&acm->data->dev, "bulk rx status %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 302 | buf = rcv->buffer; |
| 303 | buf->size = urb->actual_length; |
| 304 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 305 | if (likely(status == 0)) { |
| 306 | spin_lock(&acm->read_lock); |
| 307 | list_add_tail(&rcv->list, &acm->spare_read_urbs); |
| 308 | list_add_tail(&buf->list, &acm->filled_read_bufs); |
| 309 | spin_unlock(&acm->read_lock); |
| 310 | } else { |
| 311 | /* we drop the buffer due to an error */ |
| 312 | spin_lock(&acm->read_lock); |
| 313 | list_add_tail(&rcv->list, &acm->spare_read_urbs); |
| 314 | list_add(&buf->list, &acm->spare_read_bufs); |
| 315 | spin_unlock(&acm->read_lock); |
| 316 | /* nevertheless the tasklet must be kicked unconditionally |
| 317 | so the queue cannot dry up */ |
| 318 | } |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 319 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void acm_rx_tasklet(unsigned long _acm) |
| 323 | { |
| 324 | struct acm *acm = (void *)_acm; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 325 | struct acm_rb *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | struct tty_struct *tty = acm->tty; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 327 | struct acm_ru *rcv; |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 328 | unsigned long flags; |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 329 | unsigned char throttled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | dbg("Entering acm_rx_tasklet"); |
| 331 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 332 | if (!ACM_READY(acm)) |
| 333 | return; |
| 334 | |
| 335 | spin_lock(&acm->throttle_lock); |
| 336 | throttled = acm->throttle; |
| 337 | spin_unlock(&acm->throttle_lock); |
| 338 | if (throttled) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 339 | return; |
| 340 | |
| 341 | next_buffer: |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 342 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 343 | if (list_empty(&acm->filled_read_bufs)) { |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 344 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 345 | goto urbs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 347 | buf = list_entry(acm->filled_read_bufs.next, |
| 348 | struct acm_rb, list); |
| 349 | list_del(&buf->list); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 350 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 351 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 352 | 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] | 353 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 354 | tty_buffer_request_room(tty, buf->size); |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 355 | spin_lock(&acm->throttle_lock); |
| 356 | throttled = acm->throttle; |
| 357 | spin_unlock(&acm->throttle_lock); |
| 358 | if (!throttled) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 359 | tty_insert_flip_string(tty, buf->base, buf->size); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 360 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 362 | if (throttled) { |
| 363 | dbg("Throttling noticed"); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 364 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 365 | list_add(&buf->list, &acm->filled_read_bufs); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 366 | spin_unlock_irqrestore(&acm->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 370 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 371 | list_add(&buf->list, &acm->spare_read_bufs); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 372 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 373 | goto next_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 375 | urbs: |
| 376 | while (!list_empty(&acm->spare_read_bufs)) { |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 377 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 378 | if (list_empty(&acm->spare_read_urbs)) { |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 379 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | rcv = list_entry(acm->spare_read_urbs.next, |
| 383 | struct acm_ru, list); |
| 384 | list_del(&rcv->list); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 385 | spin_unlock_irqrestore(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 386 | |
| 387 | buf = list_entry(acm->spare_read_bufs.next, |
| 388 | struct acm_rb, list); |
| 389 | list_del(&buf->list); |
| 390 | |
| 391 | rcv->buffer = buf; |
| 392 | |
| 393 | usb_fill_bulk_urb(rcv->urb, acm->dev, |
| 394 | acm->rx_endpoint, |
| 395 | buf->base, |
| 396 | acm->readsize, |
| 397 | acm_read_bulk, rcv); |
| 398 | rcv->urb->transfer_dma = buf->dma; |
| 399 | rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 400 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 401 | 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] | 402 | |
| 403 | /* This shouldn't kill the driver as unsuccessful URBs are returned to the |
| 404 | free-urbs-pool and resubmited ASAP */ |
| 405 | if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) { |
| 406 | list_add(&buf->list, &acm->spare_read_bufs); |
Jarek Poplawski | 762f007 | 2006-10-06 07:23:11 +0200 | [diff] [blame] | 407 | spin_lock_irqsave(&acm->read_lock, flags); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 408 | list_add(&rcv->list, &acm->spare_read_urbs); |
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 | return; |
| 411 | } |
| 412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* data interface wrote those outgoing bytes */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 416 | static void acm_write_bulk(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
| 418 | struct acm *acm = (struct acm *)urb->context; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 419 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 420 | dbg("Entering acm_write_bulk with status %d", urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 422 | acm_write_done(acm); |
| 423 | acm_write_start(acm); |
| 424 | if (ACM_READY(acm)) |
| 425 | schedule_work(&acm->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 428 | static void acm_softint(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 430 | struct acm *acm = container_of(work, struct acm, work); |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 431 | dbg("Entering acm_softint."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | if (!ACM_READY(acm)) |
| 434 | return; |
| 435 | tty_wakeup(acm->tty); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * TTY handlers |
| 440 | */ |
| 441 | |
| 442 | static int acm_tty_open(struct tty_struct *tty, struct file *filp) |
| 443 | { |
| 444 | struct acm *acm; |
| 445 | int rv = -EINVAL; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 446 | int i; |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 447 | dbg("Entering acm_tty_open."); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 448 | |
| 449 | mutex_lock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | acm = acm_table[tty->index]; |
| 452 | if (!acm || !acm->dev) |
| 453 | goto err_out; |
| 454 | else |
| 455 | rv = 0; |
| 456 | |
| 457 | tty->driver_data = acm; |
| 458 | acm->tty = tty; |
| 459 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 460 | /* force low_latency on so that our tty_push actually forces the data through, |
| 461 | otherwise it is scheduled, and with high data rates data can get lost. */ |
| 462 | tty->low_latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | if (acm->used++) { |
| 465 | goto done; |
| 466 | } |
| 467 | |
| 468 | acm->ctrlurb->dev = acm->dev; |
| 469 | if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { |
| 470 | dbg("usb_submit_urb(ctrl irq) failed"); |
| 471 | goto bail_out; |
| 472 | } |
| 473 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 474 | if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) && |
| 475 | (acm->ctrl_caps & USB_CDC_CAP_LINE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | goto full_bailout; |
| 477 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 478 | INIT_LIST_HEAD(&acm->spare_read_urbs); |
| 479 | INIT_LIST_HEAD(&acm->spare_read_bufs); |
| 480 | INIT_LIST_HEAD(&acm->filled_read_bufs); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 481 | for (i = 0; i < acm->rx_buflimit; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 482 | list_add(&(acm->ru[i].list), &acm->spare_read_urbs); |
| 483 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 484 | for (i = 0; i < acm->rx_buflimit; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 485 | list_add(&(acm->rb[i].list), &acm->spare_read_bufs); |
| 486 | } |
| 487 | |
Oliver Neukum | ca79b7b | 2007-02-12 08:41:35 +0100 | [diff] [blame^] | 488 | acm->throttle = 0; |
| 489 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 490 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | done: |
| 493 | err_out: |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 494 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return rv; |
| 496 | |
| 497 | full_bailout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | usb_kill_urb(acm->ctrlurb); |
| 499 | bail_out: |
| 500 | acm->used--; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 501 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return -EIO; |
| 503 | } |
| 504 | |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 505 | static void acm_tty_unregister(struct acm *acm) |
| 506 | { |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 507 | int i,nr; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 508 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 509 | nr = acm->rx_buflimit; |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 510 | tty_unregister_device(acm_tty_driver, acm->minor); |
| 511 | usb_put_intf(acm->control); |
| 512 | acm_table[acm->minor] = NULL; |
| 513 | usb_free_urb(acm->ctrlurb); |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 514 | usb_free_urb(acm->writeurb); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 515 | for (i = 0; i < nr; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 516 | usb_free_urb(acm->ru[i].urb); |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 517 | kfree(acm); |
| 518 | } |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | static void acm_tty_close(struct tty_struct *tty, struct file *filp) |
| 521 | { |
| 522 | struct acm *acm = tty->driver_data; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 523 | int i,nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | if (!acm || !acm->used) |
| 526 | return; |
| 527 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 528 | nr = acm->rx_buflimit; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 529 | mutex_lock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (!--acm->used) { |
| 531 | if (acm->dev) { |
| 532 | acm_set_control(acm, acm->ctrlout = 0); |
| 533 | usb_kill_urb(acm->ctrlurb); |
| 534 | usb_kill_urb(acm->writeurb); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 535 | for (i = 0; i < nr; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 536 | usb_kill_urb(acm->ru[i].urb); |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 537 | } else |
| 538 | acm_tty_unregister(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 540 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 544 | { |
| 545 | struct acm *acm = tty->driver_data; |
| 546 | int stat; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 547 | unsigned long flags; |
| 548 | int wbn; |
| 549 | struct acm_wb *wb; |
| 550 | |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 551 | dbg("Entering acm_tty_write to write %d bytes,", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | if (!ACM_READY(acm)) |
| 554 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (!count) |
| 556 | return 0; |
| 557 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 558 | spin_lock_irqsave(&acm->write_lock, flags); |
| 559 | if ((wbn = acm_wb_alloc(acm)) < 0) { |
| 560 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 561 | acm_write_start(acm); |
| 562 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 564 | wb = &acm->wb[wbn]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 566 | count = (count > acm->writesize) ? acm->writesize : count; |
| 567 | dbg("Get %d bytes...", count); |
| 568 | memcpy(wb->buf, buf, count); |
| 569 | wb->len = count; |
| 570 | spin_unlock_irqrestore(&acm->write_lock, flags); |
| 571 | |
| 572 | if ((stat = acm_write_start(acm)) < 0) |
| 573 | return stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return count; |
| 575 | } |
| 576 | |
| 577 | static int acm_tty_write_room(struct tty_struct *tty) |
| 578 | { |
| 579 | struct acm *acm = tty->driver_data; |
| 580 | if (!ACM_READY(acm)) |
| 581 | return -EINVAL; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 582 | /* |
| 583 | * Do not let the line discipline to know that we have a reserve, |
| 584 | * or it might get too enthusiastic. |
| 585 | */ |
| 586 | return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static int acm_tty_chars_in_buffer(struct tty_struct *tty) |
| 590 | { |
| 591 | struct acm *acm = tty->driver_data; |
| 592 | if (!ACM_READY(acm)) |
| 593 | return -EINVAL; |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 594 | /* |
| 595 | * This is inaccurate (overcounts), but it works. |
| 596 | */ |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 597 | return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void acm_tty_throttle(struct tty_struct *tty) |
| 601 | { |
| 602 | struct acm *acm = tty->driver_data; |
| 603 | if (!ACM_READY(acm)) |
| 604 | return; |
| 605 | spin_lock_bh(&acm->throttle_lock); |
| 606 | acm->throttle = 1; |
| 607 | spin_unlock_bh(&acm->throttle_lock); |
| 608 | } |
| 609 | |
| 610 | static void acm_tty_unthrottle(struct tty_struct *tty) |
| 611 | { |
| 612 | struct acm *acm = tty->driver_data; |
| 613 | if (!ACM_READY(acm)) |
| 614 | return; |
| 615 | spin_lock_bh(&acm->throttle_lock); |
| 616 | acm->throttle = 0; |
| 617 | spin_unlock_bh(&acm->throttle_lock); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 618 | tasklet_schedule(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static void acm_tty_break_ctl(struct tty_struct *tty, int state) |
| 622 | { |
| 623 | struct acm *acm = tty->driver_data; |
| 624 | if (!ACM_READY(acm)) |
| 625 | return; |
| 626 | if (acm_send_break(acm, state ? 0xffff : 0)) |
| 627 | dbg("send break failed"); |
| 628 | } |
| 629 | |
| 630 | static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file) |
| 631 | { |
| 632 | struct acm *acm = tty->driver_data; |
| 633 | |
| 634 | if (!ACM_READY(acm)) |
| 635 | return -EINVAL; |
| 636 | |
| 637 | return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | |
| 638 | (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | |
| 639 | (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | |
| 640 | (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | |
| 641 | (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | |
| 642 | TIOCM_CTS; |
| 643 | } |
| 644 | |
| 645 | static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file, |
| 646 | unsigned int set, unsigned int clear) |
| 647 | { |
| 648 | struct acm *acm = tty->driver_data; |
| 649 | unsigned int newctrl; |
| 650 | |
| 651 | if (!ACM_READY(acm)) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | newctrl = acm->ctrlout; |
| 655 | set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); |
| 656 | clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); |
| 657 | |
| 658 | newctrl = (newctrl & ~clear) | set; |
| 659 | |
| 660 | if (acm->ctrlout == newctrl) |
| 661 | return 0; |
| 662 | return acm_set_control(acm, acm->ctrlout = newctrl); |
| 663 | } |
| 664 | |
| 665 | static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) |
| 666 | { |
| 667 | struct acm *acm = tty->driver_data; |
| 668 | |
| 669 | if (!ACM_READY(acm)) |
| 670 | return -EINVAL; |
| 671 | |
| 672 | return -ENOIOCTLCMD; |
| 673 | } |
| 674 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 675 | static const __u32 acm_tty_speed[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 0, 50, 75, 110, 134, 150, 200, 300, 600, |
| 677 | 1200, 1800, 2400, 4800, 9600, 19200, 38400, |
| 678 | 57600, 115200, 230400, 460800, 500000, 576000, |
| 679 | 921600, 1000000, 1152000, 1500000, 2000000, |
| 680 | 2500000, 3000000, 3500000, 4000000 |
| 681 | }; |
| 682 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 683 | static const __u8 acm_tty_size[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | 5, 6, 7, 8 |
| 685 | }; |
| 686 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 687 | 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] | 688 | { |
| 689 | struct acm *acm = tty->driver_data; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 690 | struct ktermios *termios = tty->termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | struct usb_cdc_line_coding newline; |
| 692 | int newctrl = acm->ctrlout; |
| 693 | |
| 694 | if (!ACM_READY(acm)) |
| 695 | return; |
| 696 | |
| 697 | newline.dwDTERate = cpu_to_le32p(acm_tty_speed + |
| 698 | (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0)); |
| 699 | newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; |
| 700 | newline.bParityType = termios->c_cflag & PARENB ? |
| 701 | (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; |
| 702 | newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4]; |
| 703 | |
| 704 | acm->clocal = ((termios->c_cflag & CLOCAL) != 0); |
| 705 | |
| 706 | if (!newline.dwDTERate) { |
| 707 | newline.dwDTERate = acm->line.dwDTERate; |
| 708 | newctrl &= ~ACM_CTRL_DTR; |
| 709 | } else newctrl |= ACM_CTRL_DTR; |
| 710 | |
| 711 | if (newctrl != acm->ctrlout) |
| 712 | acm_set_control(acm, acm->ctrlout = newctrl); |
| 713 | |
| 714 | if (memcmp(&acm->line, &newline, sizeof newline)) { |
| 715 | memcpy(&acm->line, &newline, sizeof newline); |
| 716 | dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate), |
| 717 | newline.bCharFormat, newline.bParityType, |
| 718 | newline.bDataBits); |
| 719 | acm_set_line(acm, &acm->line); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * USB probe and disconnect routines. |
| 725 | */ |
| 726 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 727 | /* Little helper: write buffers free */ |
| 728 | static void acm_write_buffers_free(struct acm *acm) |
| 729 | { |
| 730 | int i; |
| 731 | struct acm_wb *wb; |
| 732 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 733 | for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 734 | usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* Little helper: write buffers allocate */ |
| 739 | static int acm_write_buffers_alloc(struct acm *acm) |
| 740 | { |
| 741 | int i; |
| 742 | struct acm_wb *wb; |
| 743 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 744 | for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 745 | wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL, |
| 746 | &wb->dmah); |
| 747 | if (!wb->buf) { |
| 748 | while (i != 0) { |
| 749 | --i; |
| 750 | --wb; |
| 751 | usb_buffer_free(acm->dev, acm->writesize, |
| 752 | wb->buf, wb->dmah); |
| 753 | } |
| 754 | return -ENOMEM; |
| 755 | } |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | static int acm_probe (struct usb_interface *intf, |
| 761 | const struct usb_device_id *id) |
| 762 | { |
| 763 | struct usb_cdc_union_desc *union_header = NULL; |
| 764 | char *buffer = intf->altsetting->extra; |
| 765 | int buflen = intf->altsetting->extralen; |
| 766 | struct usb_interface *control_interface; |
| 767 | struct usb_interface *data_interface; |
| 768 | struct usb_endpoint_descriptor *epctrl; |
| 769 | struct usb_endpoint_descriptor *epread; |
| 770 | struct usb_endpoint_descriptor *epwrite; |
| 771 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 772 | struct acm *acm; |
| 773 | int minor; |
| 774 | int ctrlsize,readsize; |
| 775 | u8 *buf; |
| 776 | u8 ac_management_function = 0; |
| 777 | u8 call_management_function = 0; |
| 778 | int call_interface_num = -1; |
| 779 | int data_interface_num; |
| 780 | unsigned long quirks; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 781 | int num_rx_buf; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 782 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 784 | /* normal quirks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | quirks = (unsigned long)id->driver_info; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 786 | num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR; |
| 787 | |
| 788 | /* handle quirks deadly to normal probing*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | if (quirks == NO_UNION_NORMAL) { |
| 790 | data_interface = usb_ifnum_to_if(usb_dev, 1); |
| 791 | control_interface = usb_ifnum_to_if(usb_dev, 0); |
| 792 | goto skip_normal_probe; |
| 793 | } |
| 794 | |
| 795 | /* normal probing*/ |
| 796 | if (!buffer) { |
| 797 | err("Wierd descriptor references\n"); |
| 798 | return -EINVAL; |
| 799 | } |
| 800 | |
| 801 | if (!buflen) { |
| 802 | if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 803 | dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | buflen = intf->cur_altsetting->endpoint->extralen; |
| 805 | buffer = intf->cur_altsetting->endpoint->extra; |
| 806 | } else { |
| 807 | err("Zero length descriptor references\n"); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | while (buflen > 0) { |
| 813 | if (buffer [1] != USB_DT_CS_INTERFACE) { |
| 814 | err("skipping garbage\n"); |
| 815 | goto next_desc; |
| 816 | } |
| 817 | |
| 818 | switch (buffer [2]) { |
| 819 | case USB_CDC_UNION_TYPE: /* we've found it */ |
| 820 | if (union_header) { |
| 821 | err("More than one union descriptor, skipping ..."); |
| 822 | goto next_desc; |
| 823 | } |
| 824 | union_header = (struct usb_cdc_union_desc *) |
| 825 | buffer; |
| 826 | break; |
| 827 | case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */ |
| 828 | break; /* for now we ignore it */ |
| 829 | case USB_CDC_HEADER_TYPE: /* maybe check version */ |
| 830 | break; /* for now we ignore it */ |
| 831 | case USB_CDC_ACM_TYPE: |
| 832 | ac_management_function = buffer[3]; |
| 833 | break; |
| 834 | case USB_CDC_CALL_MANAGEMENT_TYPE: |
| 835 | call_management_function = buffer[3]; |
| 836 | call_interface_num = buffer[4]; |
| 837 | if ((call_management_function & 3) != 3) |
| 838 | err("This device cannot do calls on its own. It is no modem."); |
| 839 | break; |
| 840 | |
| 841 | default: |
| 842 | err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]); |
| 843 | break; |
| 844 | } |
| 845 | next_desc: |
| 846 | buflen -= buffer[0]; |
| 847 | buffer += buffer[0]; |
| 848 | } |
| 849 | |
| 850 | if (!union_header) { |
| 851 | if (call_interface_num > 0) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 852 | dev_dbg(&intf->dev,"No union descriptor, using call management descriptor"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); |
| 854 | control_interface = intf; |
| 855 | } else { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 856 | dev_dbg(&intf->dev,"No union descriptor, giving up"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | return -ENODEV; |
| 858 | } |
| 859 | } else { |
| 860 | control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); |
| 861 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); |
| 862 | if (!control_interface || !data_interface) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 863 | dev_dbg(&intf->dev,"no interfaces"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return -ENODEV; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if (data_interface_num != call_interface_num) |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 869 | dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | skip_normal_probe: |
| 872 | |
| 873 | /*workaround for switched interfaces */ |
| 874 | if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) { |
| 875 | if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) { |
| 876 | struct usb_interface *t; |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 877 | dev_dbg(&intf->dev,"Your device has switched interfaces."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | t = control_interface; |
| 880 | control_interface = data_interface; |
| 881 | data_interface = t; |
| 882 | } else { |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (usb_interface_claimed(data_interface)) { /* valid in this context */ |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 888 | dev_dbg(&intf->dev,"The data interface isn't available"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return -EBUSY; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | if (data_interface->cur_altsetting->desc.bNumEndpoints < 2) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | epctrl = &control_interface->cur_altsetting->endpoint[0].desc; |
| 897 | epread = &data_interface->cur_altsetting->endpoint[0].desc; |
| 898 | epwrite = &data_interface->cur_altsetting->endpoint[1].desc; |
| 899 | |
| 900 | |
| 901 | /* workaround for switched endpoints */ |
Luiz Fernando N. Capitulino | 45aea70 | 2006-10-26 13:02:48 -0300 | [diff] [blame] | 902 | if (!usb_endpoint_dir_in(epread)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /* descriptors are swapped */ |
| 904 | struct usb_endpoint_descriptor *t; |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 905 | dev_dbg(&intf->dev,"The data interface has switched endpoints"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | t = epread; |
| 908 | epread = epwrite; |
| 909 | epwrite = t; |
| 910 | } |
| 911 | dbg("interfaces are valid"); |
| 912 | for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++); |
| 913 | |
| 914 | if (minor == ACM_TTY_MINORS) { |
| 915 | err("no more free acm devices"); |
| 916 | return -ENODEV; |
| 917 | } |
| 918 | |
Oliver Neukum | 46f116e | 2005-10-24 22:42:35 +0200 | [diff] [blame] | 919 | if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 920 | dev_dbg(&intf->dev, "out of memory (acm kzalloc)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | goto alloc_fail; |
| 922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 925 | readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize); |
| 927 | acm->control = control_interface; |
| 928 | acm->data = data_interface; |
| 929 | acm->minor = minor; |
| 930 | acm->dev = usb_dev; |
| 931 | acm->ctrl_caps = ac_management_function; |
| 932 | acm->ctrlsize = ctrlsize; |
| 933 | acm->readsize = readsize; |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 934 | acm->rx_buflimit = num_rx_buf; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 935 | acm->urb_task.func = acm_rx_tasklet; |
| 936 | acm->urb_task.data = (unsigned long) acm; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 937 | INIT_WORK(&acm->work, acm_softint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | spin_lock_init(&acm->throttle_lock); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 939 | spin_lock_init(&acm->write_lock); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 940 | spin_lock_init(&acm->read_lock); |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 941 | acm->write_ready = 1; |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 942 | acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); |
| 945 | if (!buf) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 946 | dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | goto alloc_fail2; |
| 948 | } |
| 949 | acm->ctrl_buffer = buf; |
| 950 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 951 | if (acm_write_buffers_alloc(acm) < 0) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 952 | dev_dbg(&intf->dev, "out of memory (write buffer alloc)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | goto alloc_fail4; |
| 954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
| 956 | acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); |
| 957 | if (!acm->ctrlurb) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 958 | dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | goto alloc_fail5; |
| 960 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 961 | for (i = 0; i < num_rx_buf; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 962 | struct acm_ru *rcv = &(acm->ru[i]); |
| 963 | |
| 964 | if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 965 | dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)"); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 966 | goto alloc_fail7; |
| 967 | } |
| 968 | |
| 969 | rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 970 | rcv->instance = acm; |
| 971 | } |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 972 | for (i = 0; i < num_rx_buf; i++) { |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 973 | struct acm_rb *buf = &(acm->rb[i]); |
| 974 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 975 | if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 976 | dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)"); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 977 | goto alloc_fail7; |
| 978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | acm->writeurb = usb_alloc_urb(0, GFP_KERNEL); |
| 981 | if (!acm->writeurb) { |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 982 | dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | goto alloc_fail7; |
| 984 | } |
| 985 | |
| 986 | usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), |
| 987 | acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); |
| 988 | acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 989 | acm->ctrlurb->transfer_dma = acm->ctrl_dma; |
| 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 992 | NULL, acm->writesize, acm_write_bulk, acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); |
| 996 | |
| 997 | acm_set_control(acm, acm->ctrlout); |
| 998 | |
| 999 | acm->line.dwDTERate = cpu_to_le32(9600); |
| 1000 | acm->line.bDataBits = 8; |
| 1001 | acm_set_line(acm, &acm->line); |
| 1002 | |
| 1003 | usb_driver_claim_interface(&acm_driver, data_interface, acm); |
| 1004 | |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1005 | usb_get_intf(control_interface); |
| 1006 | tty_register_device(acm_tty_driver, minor, &control_interface->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | acm_table[minor] = acm; |
| 1009 | usb_set_intfdata (intf, acm); |
| 1010 | return 0; |
| 1011 | |
| 1012 | alloc_fail7: |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1013 | for (i = 0; i < num_rx_buf; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1014 | usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1015 | for (i = 0; i < num_rx_buf; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1016 | usb_free_urb(acm->ru[i].urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | usb_free_urb(acm->ctrlurb); |
| 1018 | alloc_fail5: |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1019 | acm_write_buffers_free(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | alloc_fail4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); |
| 1022 | alloc_fail2: |
| 1023 | kfree(acm); |
| 1024 | alloc_fail: |
| 1025 | return -ENOMEM; |
| 1026 | } |
| 1027 | |
| 1028 | static void acm_disconnect(struct usb_interface *intf) |
| 1029 | { |
| 1030 | struct acm *acm = usb_get_intfdata (intf); |
| 1031 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1032 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (!acm || !acm->dev) { |
| 1035 | dbg("disconnect on nonexisting interface"); |
| 1036 | return; |
| 1037 | } |
| 1038 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1039 | mutex_lock(&open_mutex); |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1040 | if (!usb_get_intfdata(intf)) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1041 | mutex_unlock(&open_mutex); |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1042 | return; |
| 1043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | acm->dev = NULL; |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1045 | usb_set_intfdata(acm->control, NULL); |
| 1046 | usb_set_intfdata(acm->data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1048 | tasklet_disable(&acm->urb_task); |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | usb_kill_urb(acm->ctrlurb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | usb_kill_urb(acm->writeurb); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1052 | for (i = 0; i < acm->rx_buflimit; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1053 | usb_kill_urb(acm->ru[i].urb); |
| 1054 | |
| 1055 | INIT_LIST_HEAD(&acm->filled_read_bufs); |
| 1056 | INIT_LIST_HEAD(&acm->spare_read_bufs); |
| 1057 | |
| 1058 | tasklet_enable(&acm->urb_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | flush_scheduled_work(); /* wait for acm_softint */ |
| 1061 | |
Oliver Neukum | 884b600 | 2005-04-21 21:28:02 +0200 | [diff] [blame] | 1062 | acm_write_buffers_free(acm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1064 | for (i = 0; i < acm->rx_buflimit; i++) |
David Kubicek | 61a87ad | 2005-11-01 18:51:34 +0100 | [diff] [blame] | 1065 | usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Oliver Neukum | 86067eea | 2006-01-08 12:39:13 +0100 | [diff] [blame] | 1067 | usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| 1069 | if (!acm->used) { |
brian@murphy.dk | 83ef344 | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1070 | acm_tty_unregister(acm); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1071 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 1075 | mutex_unlock(&open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | if (acm->tty) |
| 1078 | tty_hangup(acm->tty); |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * USB driver structure. |
| 1083 | */ |
| 1084 | |
| 1085 | static struct usb_device_id acm_ids[] = { |
| 1086 | /* quirky and broken devices */ |
| 1087 | { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ |
| 1088 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1089 | }, |
Masahito Omote | 8753e65 | 2005-07-29 12:17:25 -0700 | [diff] [blame] | 1090 | { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */ |
| 1091 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1092 | }, |
Chris Malley | 91a9c92 | 2006-10-03 10:08:28 +0100 | [diff] [blame] | 1093 | { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */ |
| 1094 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ |
| 1095 | }, |
Oliver Neukum | 8647894 | 2006-05-13 22:50:47 +0200 | [diff] [blame] | 1096 | { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */ |
| 1097 | .driver_info = SINGLE_RX_URB, /* firmware bug */ |
| 1098 | }, |
Oliver Neukum | 3dd2ae8 | 2006-06-23 09:14:17 +0200 | [diff] [blame] | 1099 | { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */ |
| 1100 | .driver_info = SINGLE_RX_URB, /* firmware bug */ |
| 1101 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | /* control interfaces with various AT-command sets */ |
| 1103 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1104 | USB_CDC_ACM_PROTO_AT_V25TER) }, |
| 1105 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1106 | USB_CDC_ACM_PROTO_AT_PCCA101) }, |
| 1107 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1108 | USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, |
| 1109 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1110 | USB_CDC_ACM_PROTO_AT_GSM) }, |
| 1111 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1112 | USB_CDC_ACM_PROTO_AT_3G ) }, |
| 1113 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, |
| 1114 | USB_CDC_ACM_PROTO_AT_CDMA) }, |
| 1115 | |
| 1116 | /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */ |
| 1117 | { } |
| 1118 | }; |
| 1119 | |
| 1120 | MODULE_DEVICE_TABLE (usb, acm_ids); |
| 1121 | |
| 1122 | static struct usb_driver acm_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | .name = "cdc_acm", |
| 1124 | .probe = acm_probe, |
| 1125 | .disconnect = acm_disconnect, |
| 1126 | .id_table = acm_ids, |
| 1127 | }; |
| 1128 | |
| 1129 | /* |
| 1130 | * TTY driver structures. |
| 1131 | */ |
| 1132 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1133 | static const struct tty_operations acm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | .open = acm_tty_open, |
| 1135 | .close = acm_tty_close, |
| 1136 | .write = acm_tty_write, |
| 1137 | .write_room = acm_tty_write_room, |
| 1138 | .ioctl = acm_tty_ioctl, |
| 1139 | .throttle = acm_tty_throttle, |
| 1140 | .unthrottle = acm_tty_unthrottle, |
| 1141 | .chars_in_buffer = acm_tty_chars_in_buffer, |
| 1142 | .break_ctl = acm_tty_break_ctl, |
| 1143 | .set_termios = acm_tty_set_termios, |
| 1144 | .tiocmget = acm_tty_tiocmget, |
| 1145 | .tiocmset = acm_tty_tiocmset, |
| 1146 | }; |
| 1147 | |
| 1148 | /* |
| 1149 | * Init / exit. |
| 1150 | */ |
| 1151 | |
| 1152 | static int __init acm_init(void) |
| 1153 | { |
| 1154 | int retval; |
| 1155 | acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); |
| 1156 | if (!acm_tty_driver) |
| 1157 | return -ENOMEM; |
| 1158 | acm_tty_driver->owner = THIS_MODULE, |
| 1159 | acm_tty_driver->driver_name = "acm", |
| 1160 | acm_tty_driver->name = "ttyACM", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | acm_tty_driver->major = ACM_TTY_MAJOR, |
| 1162 | acm_tty_driver->minor_start = 0, |
| 1163 | acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, |
| 1164 | acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1165 | acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | acm_tty_driver->init_termios = tty_std_termios; |
| 1167 | acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1168 | tty_set_operations(acm_tty_driver, &acm_ops); |
| 1169 | |
| 1170 | retval = tty_register_driver(acm_tty_driver); |
| 1171 | if (retval) { |
| 1172 | put_tty_driver(acm_tty_driver); |
| 1173 | return retval; |
| 1174 | } |
| 1175 | |
| 1176 | retval = usb_register(&acm_driver); |
| 1177 | if (retval) { |
| 1178 | tty_unregister_driver(acm_tty_driver); |
| 1179 | put_tty_driver(acm_tty_driver); |
| 1180 | return retval; |
| 1181 | } |
| 1182 | |
| 1183 | info(DRIVER_VERSION ":" DRIVER_DESC); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static void __exit acm_exit(void) |
| 1189 | { |
| 1190 | usb_deregister(&acm_driver); |
| 1191 | tty_unregister_driver(acm_tty_driver); |
| 1192 | put_tty_driver(acm_tty_driver); |
| 1193 | } |
| 1194 | |
| 1195 | module_init(acm_init); |
| 1196 | module_exit(acm_exit); |
| 1197 | |
| 1198 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 1199 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 1200 | MODULE_LICENSE("GPL"); |
| 1201 | |