File Index
Files and directories
Sign in
FILE

cdc-acm.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/class/cdc-acm.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * cdc-acm.c
   4 *
   5 * Copyright (c) 1999 Armin Fuerst	<fuerst@in.tum.de>
   6 * Copyright (c) 1999 Pavel Machek	<pavel@ucw.cz>
   7 * Copyright (c) 1999 Johannes Erdfelt	<johannes@erdfelt.com>
   8 * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
   9 * Copyright (c) 2004 Oliver Neukum	<oliver@neukum.name>
  10 * Copyright (c) 2005 David Kubicek	<dave@awk.cz>
  11 * Copyright (c) 2011 Johan Hovold	<jhovold@gmail.com>
  12 *
  13 * USB Abstract Control Model driver for USB modems and ISDN adapters
  14 *
  15 * Sponsored by SuSE
  16 */
  17
  18#undef DEBUG
  19#undef VERBOSE_DEBUG
  20
  21#include <linux/kernel.h>
  22#include <linux/sched/signal.h>
  23#include <linux/errno.h>
  24#include <linux/init.h>
  25#include <linux/slab.h>
  26#include <linux/log2.h>
  27#include <linux/tty.h>
  28#include <linux/serial.h>
  29#include <linux/tty_driver.h>
  30#include <linux/tty_flip.h>
  31#include <linux/tty_ldisc.h>
  32#include <linux/module.h>
  33#include <linux/mutex.h>
  34#include <linux/uaccess.h>
  35#include <linux/usb.h>
  36#include <linux/usb/cdc.h>
  37#include <asm/byteorder.h>
  38#include <linux/unaligned.h>
  39#include <linux/idr.h>
  40#include <linux/list.h>
  41
  42#include "cdc-acm.h"
  43
  44
  45#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  46#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  47
  48static struct usb_driver acm_driver;
  49static struct tty_driver *acm_tty_driver;
  50
  51static DEFINE_IDR(acm_minors);
  52static DEFINE_MUTEX(acm_minors_lock);
  53
  54static void acm_tty_set_termios(struct tty_struct *tty,
  55				const struct ktermios *termios_old);
  56
  57/*
  58 * acm_minors accessors
  59 */
  60
  61/*
  62 * Look up an ACM structure by minor. If found and not disconnected, increment
  63 * its refcount and return it with its mutex held.
  64 */
  65static struct acm *acm_get_by_minor(unsigned int minor)
  66{
  67	struct acm *acm;
  68
  69	mutex_lock(&acm_minors_lock);
  70	acm = idr_find(&acm_minors, minor);
  71	if (acm) {
  72		mutex_lock(&acm->mutex);
  73		if (acm->disconnected) {
  74			mutex_unlock(&acm->mutex);
  75			acm = NULL;
  76		} else {
  77			tty_port_get(&acm->port);
  78			mutex_unlock(&acm->mutex);
  79		}
  80	}
  81	mutex_unlock(&acm_minors_lock);
  82	return acm;
  83}
  84
  85/*
  86 * Try to find an available minor number and if found, associate it with 'acm'.
  87 */
  88static int acm_alloc_minor(struct acm *acm)
  89{
  90	int minor;
  91
  92	mutex_lock(&acm_minors_lock);
  93	minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
  94	mutex_unlock(&acm_minors_lock);
  95
  96	return minor;
  97}
  98
  99/* Release the minor number associated with 'acm'.  */
 100static void acm_release_minor(struct acm *acm)
 101{
 102	mutex_lock(&acm_minors_lock);
 103	idr_remove(&acm_minors, acm->minor);
 104	mutex_unlock(&acm_minors_lock);
 105}
 106
 107/*
 108 * Functions for ACM control messages.
 109 */
 110
 111static int acm_ctrl_msg(struct acm *acm, int request, int value,
 112							void *buf, int len)
 113{
 114	int retval;
 115
 116	retval = usb_autopm_get_interface(acm->control);
 117#define VENDOR_CLASS_DATA_IFACE		BIT(9)  /* data interface uses vendor-specific class */
 118#define ALWAYS_POLL_CTRL		BIT(10) /* keep ctrl URB active even without an open TTY */
 119	if (retval)
 120		return retval;
 121
 122	retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
 123		request, USB_RT_ACM, value,
 124		acm->control->altsetting[0].desc.bInterfaceNumber,
 125		buf, len, USB_CTRL_SET_TIMEOUT);
 126
 127	dev_dbg(&acm->control->dev,
 128		"%s - rq 0x%02x, val %#x, len %#x, result %d\n",
 129		__func__, request, value, len, retval);
 130
 131	usb_autopm_put_interface(acm->control);
 132
 133	return retval < 0 ? retval : 0;
 134}
 135
 136/* devices aren't required to support these requests.
 137 * the cdc acm descriptor tells whether they do...
 138 */
 139static inline int acm_set_control(struct acm *acm, int control)
 140{
 141	if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
 142		return -EOPNOTSUPP;
 143
 144	return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
 145			control, NULL, 0);
 146}
 147
 148#define acm_set_line(acm, line) \
 149	acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
 150#define acm_send_break(acm, ms) \
 151	acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
 152
 153static void acm_poison_urbs(struct acm *acm)
 154{
 155	int i;
 156
 157	usb_poison_urb(acm->ctrlurb);
 158	for (i = 0; i < ACM_NW; i++)
 159		usb_poison_urb(acm->wb[i].urb);
 160	for (i = 0; i < acm->rx_buflimit; i++)
 161		usb_poison_urb(acm->read_urbs[i]);
 162}
 163
 164static void acm_unpoison_urbs(struct acm *acm)
 165{
 166	int i;
 167
 168	for (i = 0; i < acm->rx_buflimit; i++)
 169		usb_unpoison_urb(acm->read_urbs[i]);
 170	for (i = 0; i < ACM_NW; i++)
 171		usb_unpoison_urb(acm->wb[i].urb);
 172	usb_unpoison_urb(acm->ctrlurb);
 173}
 174
 175
 176/*
 177 * Write buffer management.
 178 * All of these assume proper locks taken by the caller.
 179 */
 180
 181static int acm_wb_alloc(struct acm *acm)
 182{
 183	int i, wbn;
 184	struct acm_wb *wb;
 185
 186	wbn = 0;
 187	i = 0;
 188	for (;;) {
 189		wb = &acm->wb[wbn];
 190		if (!wb->use) {
 191			wb->use = true;
 192			wb->len = 0;
 193			return wbn;
 194		}
 195		wbn = (wbn + 1) % ACM_NW;
 196		if (++i >= ACM_NW)
 197			return -1;
 198	}
 199}
 200
 201static int acm_wb_is_avail(struct acm *acm)
 202{
 203	int i, n;
 204	unsigned long flags;
 205
 206	n = ACM_NW;
 207	spin_lock_irqsave(&acm->write_lock, flags);
 208	for (i = 0; i < ACM_NW; i++)
 209		if(acm->wb[i].use)
 210			n--;
 211	spin_unlock_irqrestore(&acm->write_lock, flags);
 212	return n;
 213}
 214
 215/*
 216 * Finish write. Caller must hold acm->write_lock
 217 */
 218static void acm_write_done(struct acm *acm, struct acm_wb *wb)
 219{
 220	wb->use = false;
 221	acm->transmitting--;
 222	usb_autopm_put_interface_async(acm->control);
 223}
 224
 225/*
 226 * Poke write.
 227 *
 228 * the caller is responsible for locking
 229 */
 230
 231static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
 232{
 233	int rc;
 234
 235	acm->transmitting++;
 236
 237	wb->urb->transfer_buffer = wb->buf;
 238	wb->urb->transfer_dma = wb->dmah;
 239	wb->urb->transfer_buffer_length = wb->len;
 240	wb->urb->dev = acm->dev;
 241
 242	rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
 243	if (rc < 0) {
 244		if (rc != -EPERM)
 245			dev_err(&acm->data->dev,
 246				"%s - usb_submit_urb(write bulk) failed: %d\n",
 247				__func__, rc);
 248		acm_write_done(acm, wb);
 249	}
 250	return rc;
 251}
 252
 253/*
 254 * attributes exported through sysfs
 255 */
 256static ssize_t bmCapabilities_show
 257(struct device *dev, struct device_attribute *attr, char *buf)
 258{
 259	struct usb_interface *intf = to_usb_interface(dev);
 260	struct acm *acm = usb_get_intfdata(intf);
 261
 262	return sprintf(buf, "%d", acm->ctrl_caps);
 263}
 264static DEVICE_ATTR_RO(bmCapabilities);
 265
 266static ssize_t wCountryCodes_show
 267(struct device *dev, struct device_attribute *attr, char *buf)
 268{
 269	struct usb_interface *intf = to_usb_interface(dev);
 270	struct acm *acm = usb_get_intfdata(intf);
 271
 272	memcpy(buf, acm->country_codes, acm->country_code_size);
 273	return acm->country_code_size;
 274}
 275
 276static DEVICE_ATTR_RO(wCountryCodes);
 277
 278static ssize_t iCountryCodeRelDate_show
 279(struct device *dev, struct device_attribute *attr, char *buf)
 280{
 281	struct usb_interface *intf = to_usb_interface(dev);
 282	struct acm *acm = usb_get_intfdata(intf);
 283
 284	return sprintf(buf, "%d", acm->country_rel_date);
 285}
 286
 287static DEVICE_ATTR_RO(iCountryCodeRelDate);
 288/*
 289 * Interrupt handlers for various ACM device responses
 290 */
 291
 292static void acm_process_notification(struct acm *acm, unsigned char *buf)
 293{
 294	int newctrl;
 295	int difference;
 296	unsigned long flags;
 297	struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf;
 298	unsigned char *data = buf + sizeof(struct usb_cdc_notification);
 299
 300	switch (dr->bNotificationType) {
 301	case USB_CDC_NOTIFY_NETWORK_CONNECTION:
 302		dev_dbg(&acm->control->dev,
 303			"%s - network connection: %d\n", __func__, dr->wValue);
 304		break;
 305
 306	case USB_CDC_NOTIFY_SERIAL_STATE:
 307		if (le16_to_cpu(dr->wLength) != 2) {
 308			dev_dbg(&acm->control->dev,
 309				"%s - malformed serial state\n", __func__);
 310			break;
 311		}
 312
 313		newctrl = get_unaligned_le16(data);
 314		dev_dbg(&acm->control->dev,
 315			"%s - serial state: 0x%x\n", __func__, newctrl);
 316
 317		if (!acm->clocal && (acm->ctrlin & ~newctrl & USB_CDC_SERIAL_STATE_DCD)) {
 318			dev_dbg(&acm->control->dev,
 319				"%s - calling hangup\n", __func__);
 320			tty_port_tty_hangup(&acm->port, false);
 321		}
 322
 323		difference = acm->ctrlin ^ newctrl;
 324
 325		if ((difference & USB_CDC_SERIAL_STATE_DCD) && acm->port.tty) {
 326			struct tty_ldisc *ld = tty_ldisc_ref(acm->port.tty);
 327			if (ld) {
 328				if (ld->ops->dcd_change)
 329					ld->ops->dcd_change(acm->port.tty, newctrl & USB_CDC_SERIAL_STATE_DCD);
 330				tty_ldisc_deref(ld);
 331			}
 332		}
 333
 334		spin_lock_irqsave(&acm->read_lock, flags);
 335		acm->ctrlin = newctrl;
 336		acm->oldcount = acm->iocount;
 337
 338		if (difference & USB_CDC_SERIAL_STATE_DSR)
 339			acm->iocount.dsr++;
 340		if (difference & USB_CDC_SERIAL_STATE_DCD)
 341			acm->iocount.dcd++;
 342		if (newctrl & USB_CDC_SERIAL_STATE_BREAK) {
 343			acm->iocount.brk++;
 344			tty_insert_flip_char(&acm->port, 0, TTY_BREAK);
 345		}
 346		if (newctrl & USB_CDC_SERIAL_STATE_RING_SIGNAL)
 347			acm->iocount.rng++;
 348		if (newctrl & USB_CDC_SERIAL_STATE_FRAMING)
 349			acm->iocount.frame++;
 350		if (newctrl & USB_CDC_SERIAL_STATE_PARITY)
 351			acm->iocount.parity++;
 352		if (newctrl & USB_CDC_SERIAL_STATE_OVERRUN)
 353			acm->iocount.overrun++;
 354		spin_unlock_irqrestore(&acm->read_lock, flags);
 355
 356		if (newctrl & USB_CDC_SERIAL_STATE_BREAK)
 357			tty_flip_buffer_push(&acm->port);
 358
 359		if (difference)
 360			wake_up_all(&acm->wioctl);
 361
 362		break;
 363
 364	default:
 365		dev_dbg(&acm->control->dev,
 366			"%s - unknown notification %d received: index %d len %d\n",
 367			__func__,
 368			dr->bNotificationType, dr->wIndex, dr->wLength);
 369	}
 370}
 371
 372/* control interface reports status changes with "interrupt" transfers */
 373static void acm_ctrl_irq(struct urb *urb)
 374{
 375	struct acm *acm = urb->context;
 376	struct usb_cdc_notification *dr;
 377	unsigned int current_size = urb->actual_length;
 378	unsigned int expected_size, copy_size, alloc_size;
 379	int retval;
 380	int status = urb->status;
 381
 382	switch (status) {
 383	case 0:
 384		/* success */
 385		break;
 386	case -ECONNRESET:
 387	case -ENOENT:
 388	case -ESHUTDOWN:
 389		/* this urb is terminated, clean up */
 390		dev_dbg(&acm->control->dev,
 391			"%s - urb shutting down with status: %d\n",
 392			__func__, status);
 393		return;
 394	default:
 395		dev_dbg(&acm->control->dev,
 396			"%s - nonzero urb status received: %d\n",
 397			__func__, status);
 398		goto exit;
 399	}
 400
 401	usb_mark_last_busy(acm->dev);
 402
 403	if (acm->nb_index == 0) {
 404		/*
 405		 * The first chunk of a message must contain at least the
 406		 * notification header with the length field, otherwise we
 407		 * can't get an expected_size.
 408		 */
 409		if (current_size < sizeof(struct usb_cdc_notification)) {
 410			dev_dbg(&acm->control->dev, "urb too short\n");
 411			goto exit;
 412		}
 413		dr = urb->transfer_buffer;
 414	} else {
 415		dr = (struct usb_cdc_notification *)acm->notification_buffer;
 416	}
 417	/* size = notification-header + (optional) data */
 418	expected_size = sizeof(struct usb_cdc_notification) +
 419					le16_to_cpu(dr->wLength);
 420
 421	if (acm->nb_index != 0 || current_size < expected_size) {
 422		/* notification is transmitted fragmented, reassemble */
 423		if (acm->nb_size < expected_size) {
 424			u8 *new_buffer;
 425			alloc_size = roundup_pow_of_two(expected_size);
 426			/* Final freeing is done on disconnect. */
 427			new_buffer = krealloc(acm->notification_buffer,
 428					      alloc_size, GFP_ATOMIC);
 429			if (!new_buffer) {
 430				acm->nb_index = 0;
 431				goto exit;
 432			}
 433
 434			acm->notification_buffer = new_buffer;
 435			acm->nb_size = alloc_size;
 436			dr = (struct usb_cdc_notification *)acm->notification_buffer;
 437		}
 438
 439		copy_size = min(current_size,
 440				expected_size - acm->nb_index);
 441
 442		memcpy(&acm->notification_buffer[acm->nb_index],
 443		       urb->transfer_buffer, copy_size);
 444		acm->nb_index += copy_size;
 445		current_size = acm->nb_index;
 446	}
 447
 448	if (current_size >= expected_size) {
 449		/* notification complete */
 450		acm_process_notification(acm, (unsigned char *)dr);
 451		acm->nb_index = 0;
 452	}
 453
 454exit:
 455	retval = usb_submit_urb(urb, GFP_ATOMIC);
 456	if (retval && retval != -EPERM && retval != -ENODEV)
 457		dev_err(&acm->control->dev,
 458			"%s - usb_submit_urb failed: %d\n", __func__, retval);
 459	else
 460		dev_vdbg(&acm->control->dev,
 461			"control resubmission terminated %d\n", retval);
 462}
 463
 464static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
 465{
 466	int res;
 467
 468	if (!test_and_clear_bit(index, &acm->read_urbs_free))
 469		return 0;
 470
 471	res = usb_submit_urb(acm->read_urbs[index], mem_flags);
 472	if (res) {
 473		if (res != -EPERM && res != -ENODEV) {
 474			dev_err(&acm->data->dev,
 475				"urb %d failed submission with %d\n",
 476				index, res);
 477		} else {
 478			dev_vdbg(&acm->data->dev, "intended failure %d\n", res);
 479		}
 480		set_bit(index, &acm->read_urbs_free);
 481		return res;
 482	} else {
 483		dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
 484	}
 485
 486	return 0;
 487}
 488
 489static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
 490{
 491	int res;
 492	int i;
 493
 494	for (i = 0; i < acm->rx_buflimit; ++i) {
 495		res = acm_submit_read_urb(acm, i, mem_flags);
 496		if (res)
 497			return res;
 498	}
 499
 500	return 0;
 501}
 502
 503static void acm_process_read_urb(struct acm *acm, struct urb *urb)
 504{
 505	unsigned long flags;
 506
 507	if (!urb->actual_length)
 508		return;
 509
 510	spin_lock_irqsave(&acm->read_lock, flags);
 511	tty_insert_flip_string(&acm->port, urb->transfer_buffer,
 512			urb->actual_length);
 513	spin_unlock_irqrestore(&acm->read_lock, flags);
 514
 515	tty_flip_buffer_push(&acm->port);
 516}
 517
 518static void acm_read_bulk_callback(struct urb *urb)
 519{
 520	struct acm_rb *rb = urb->context;
 521	struct acm *acm = rb->instance;
 522	int status = urb->status;
 523	bool stopped = false;
 524	bool stalled = false;
 525	bool cooldown = false;
 526
 527	dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
 528		rb->index, urb->actual_length, status);
 529
 530	switch (status) {
 531	case 0:
 532		usb_mark_last_busy(acm->dev);
 533		acm_process_read_urb(acm, urb);
 534		break;
 535	case -EPIPE:
 536		set_bit(EVENT_RX_STALL, &acm->flags);
 537		stalled = true;
 538		break;
 539	case -ENOENT:
 540	case -ECONNRESET:
 541	case -ESHUTDOWN:
 542		dev_dbg(&acm->data->dev,
 543			"%s - urb shutting down with status: %d\n",
 544			__func__, status);
 545		stopped = true;
 546		break;
 547	case -EOVERFLOW:
 548	case -EPROTO:
 549		dev_dbg(&acm->data->dev,
 550			"%s - cooling babbling device\n", __func__);
 551		usb_mark_last_busy(acm->dev);
 552		set_bit(rb->index, &acm->urbs_in_error_delay);
 553		set_bit(ACM_ERROR_DELAY, &acm->flags);
 554		cooldown = true;
 555		break;
 556	default:
 557		dev_dbg(&acm->data->dev,
 558			"%s - nonzero urb status received: %d\n",
 559			__func__, status);
 560		break;
 561	}
 562
 563	/*
 564	 * Make sure URB processing is done before marking as free to avoid
 565	 * racing with unthrottle() on another CPU. Matches the barriers
 566	 * implied by the test_and_clear_bit() in acm_submit_read_urb().
 567	 */
 568	smp_mb__before_atomic();
 569	set_bit(rb->index, &acm->read_urbs_free);
 570	/*
 571	 * Make sure URB is marked as free before checking the throttled flag
 572	 * to avoid racing with unthrottle() on another CPU. Matches the
 573	 * smp_mb() in unthrottle().
 574	 */
 575	smp_mb__after_atomic();
 576
 577	if (stopped || stalled || cooldown) {
 578		if (stalled)
 579			schedule_delayed_work(&acm->dwork, 0);
 580		else if (cooldown)
 581			schedule_delayed_work(&acm->dwork, HZ / 2);
 582		return;
 583	}
 584
 585	if (test_bit(ACM_THROTTLED, &acm->flags))
 586		return;
 587
 588	acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
 589}
 590
 591/* data interface wrote those outgoing bytes */
 592static void acm_write_bulk(struct urb *urb)
 593{
 594	struct acm_wb *wb = urb->context;
 595	struct acm *acm = wb->instance;
 596	unsigned long flags;
 597	int status = urb->status;
 598
 599	if (status || (urb->actual_length != urb->transfer_buffer_length))
 600		dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
 601			urb->actual_length,
 602			urb->transfer_buffer_length,
 603			status);
 604
 605	spin_lock_irqsave(&acm->write_lock, flags);
 606	acm_write_done(acm, wb);
 607	spin_unlock_irqrestore(&acm->write_lock, flags);
 608	set_bit(EVENT_TTY_WAKEUP, &acm->flags);
 609	schedule_delayed_work(&acm->dwork, 0);
 610}
 611
 612static void acm_softint(struct work_struct *work)
 613{
 614	int i;
 615	struct acm *acm = container_of(work, struct acm, dwork.work);
 616
 617	if (test_bit(EVENT_RX_STALL, &acm->flags)) {
 618		smp_mb(); /* against acm_suspend() */
 619		if (!acm->susp_count) {
 620			for (i = 0; i < acm->rx_buflimit; i++)
 621				usb_kill_urb(acm->read_urbs[i]);
 622			usb_clear_halt(acm->dev, acm->in);
 623			acm_submit_read_urbs(acm, GFP_KERNEL);
 624			clear_bit(EVENT_RX_STALL, &acm->flags);
 625		}
 626	}
 627
 628	if (test_and_clear_bit(ACM_ERROR_DELAY, &acm->flags)) {
 629		for (i = 0; i < acm->rx_buflimit; i++)
 630			if (test_and_clear_bit(i, &acm->urbs_in_error_delay))
 631				acm_submit_read_urb(acm, i, GFP_KERNEL);
 632	}
 633
 634	if (test_and_clear_bit(EVENT_TTY_WAKEUP, &acm->flags))
 635		tty_port_tty_wakeup(&acm->port);
 636}
 637
 638/*
 639 * TTY handlers
 640 */
 641
 642static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
 643{
 644	struct acm *acm;
 645	int retval;
 646
 647	acm = acm_get_by_minor(tty->index);
 648	if (!acm)
 649		return -ENODEV;
 650
 651	retval = tty_standard_install(driver, tty);
 652	if (retval)
 653		goto error_init_termios;
 654
 655	/*
 656	 * Suppress initial echoing for some devices which might send data
 657	 * immediately after acm driver has been installed.
 658	 */
 659	if (acm->quirks & DISABLE_ECHO)
 660		tty->termios.c_lflag &= ~ECHO;
 661
 662	tty->driver_data = acm;
 663
 664	return 0;
 665
 666error_init_termios:
 667	tty_port_put(&acm->port);
 668	return retval;
 669}
 670
 671static int acm_tty_open(struct tty_struct *tty, struct file *filp)
 672{
 673	struct acm *acm = tty->driver_data;
 674
 675	return tty_port_open(&acm->port, tty, filp);
 676}
 677
 678static void acm_port_dtr_rts(struct tty_port *port, bool active)
 679{
 680	struct acm *acm = container_of(port, struct acm, port);
 681	int val;
 682	int res;
 683
 684	if (active)
 685		val = USB_CDC_CTRL_DTR | USB_CDC_CTRL_RTS;
 686	else
 687		val = 0;
 688
 689	/* FIXME: add missing ctrlout locking throughout driver */
 690	acm->ctrlout = val;
 691
 692	res = acm_set_control(acm, val);
 693	if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
 694		/* This is broken in too many devices to spam the logs */
 695		dev_dbg(&acm->control->dev, "failed to set dtr/rts\n");
 696}
 697
 698static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
 699{
 700	struct acm *acm = container_of(port, struct acm, port);
 701	int retval = -ENODEV;
 702	int i;
 703
 704	mutex_lock(&acm->mutex);
 705	if (acm->disconnected)
 706		goto disconnected;
 707
 708	retval = usb_autopm_get_interface(acm->control);
 709	if (retval)
 710		goto error_get_interface;
 711
 712	set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
 713	acm->control->needs_remote_wakeup = 1;
 714
 715	if (!(acm->quirks & ALWAYS_POLL_CTRL)) {
 716		acm->ctrlurb->dev = acm->dev;
 717		retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
 718		if (retval) {
 719			dev_err(&acm->control->dev,
 720				"%s - usb_submit_urb(ctrl irq) failed\n", __func__);
 721			goto error_submit_urb;
 722		}
 723	}
 724
 725	acm_tty_set_termios(tty, NULL);
 726
 727	/*
 728	 * Unthrottle device in case the TTY was closed while throttled.
 729	 */
 730	clear_bit(ACM_THROTTLED, &acm->flags);
 731
 732	retval = acm_submit_read_urbs(acm, GFP_KERNEL);
 733	if (retval)
 734		goto error_submit_read_urbs;
 735
 736	usb_autopm_put_interface(acm->control);
 737
 738	mutex_unlock(&acm->mutex);
 739
 740	return 0;
 741
 742error_submit_read_urbs:
 743	for (i = 0; i < acm->rx_buflimit; i++)
 744		usb_kill_urb(acm->read_urbs[i]);
 745	usb_kill_urb(acm->ctrlurb);
 746error_submit_urb:
 747	usb_autopm_put_interface(acm->control);
 748error_get_interface:
 749disconnected:
 750	mutex_unlock(&acm->mutex);
 751
 752	return usb_translate_errors(retval);
 753}
 754
 755static void acm_port_destruct(struct tty_port *port)
 756{
 757	struct acm *acm = container_of(port, struct acm, port);
 758
 759	if (acm->minor != ACM_MINOR_INVALID)
 760		acm_release_minor(acm);
 761	usb_put_intf(acm->control);
 762	kfree(acm->country_codes);
 763	kfree(acm);
 764}
 765
 766static void acm_port_shutdown(struct tty_port *port)
 767{
 768	struct acm *acm = container_of(port, struct acm, port);
 769	struct urb *urb;
 770	struct acm_wb *wb;
 771
 772	/*
 773	 * Need to grab write_lock to prevent race with resume, but no need to
 774	 * hold it due to the tty-port initialised flag.
 775	 */
 776	acm_poison_urbs(acm);
 777	spin_lock_irq(&acm->write_lock);
 778	spin_unlock_irq(&acm->write_lock);
 779
 780	usb_autopm_get_interface_no_resume(acm->control);
 781	acm->control->needs_remote_wakeup = 0;
 782	usb_autopm_put_interface(acm->control);
 783
 784	for (;;) {
 785		urb = usb_get_from_anchor(&acm->delayed);
 786		if (!urb)
 787			break;
 788		wb = urb->context;
 789		wb->use = false;
 790		usb_autopm_put_interface_async(acm->control);
 791	}
 792
 793	acm_unpoison_urbs(acm);
 794
 795	if (acm->quirks & ALWAYS_POLL_CTRL) {
 796		acm->ctrlurb->dev = acm->dev;
 797		if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL))
 798			dev_dbg(&acm->control->dev,
 799				"ctrl polling restart failed after port close\n");
 800		/* port_shutdown() cleared DTR/RTS; restore them */
 801		acm_set_control(acm, USB_CDC_CTRL_DTR | USB_CDC_CTRL_RTS);
 802	}
 803}
 804
 805static void acm_tty_cleanup(struct tty_struct *tty)
 806{
 807	struct acm *acm = tty->driver_data;
 808
 809	tty_port_put(&acm->port);
 810}
 811
 812static void acm_tty_hangup(struct tty_struct *tty)
 813{
 814	struct acm *acm = tty->driver_data;
 815
 816	tty_port_hangup(&acm->port);
 817}
 818
 819static void acm_tty_close(struct tty_struct *tty, struct file *filp)
 820{
 821	struct acm *acm = tty->driver_data;
 822
 823	tty_port_close(&acm->port, tty, filp);
 824}
 825
 826static ssize_t acm_tty_write(struct tty_struct *tty, const u8 *buf,
 827			     size_t count)
 828{
 829	struct acm *acm = tty->driver_data;
 830	int stat;
 831	unsigned long flags;
 832	int wbn;
 833	struct acm_wb *wb;
 834
 835	if (!count)
 836		return 0;
 837
 838	dev_vdbg(&acm->data->dev, "%zu bytes from tty layer\n", count);
 839
 840	spin_lock_irqsave(&acm->write_lock, flags);
 841	wbn = acm_wb_alloc(acm);
 842	if (wbn < 0) {
 843		spin_unlock_irqrestore(&acm->write_lock, flags);
 844		return 0;
 845	}
 846	wb = &acm->wb[wbn];
 847
 848	if (!acm->dev) {
 849		wb->use = false;
 850		spin_unlock_irqrestore(&acm->write_lock, flags);
 851		return -ENODEV;
 852	}
 853
 854	count = (count > acm->writesize) ? acm->writesize : count;
 855	dev_vdbg(&acm->data->dev, "writing %zu bytes\n", count);
 856	memcpy(wb->buf, buf, count);
 857	wb->len = count;
 858
 859	stat = usb_autopm_get_interface_async(acm->control);
 860	if (stat) {
 861		wb->use = false;
 862		spin_unlock_irqrestore(&acm->write_lock, flags);
 863		return stat;
 864	}
 865
 866	if (acm->susp_count) {
 867		usb_anchor_urb(wb->urb, &acm->delayed);
 868		spin_unlock_irqrestore(&acm->write_lock, flags);
 869		return count;
 870	}
 871
 872	stat = acm_start_wb(acm, wb);
 873	spin_unlock_irqrestore(&acm->write_lock, flags);
 874
 875	if (stat < 0)
 876		return stat;
 877	return count;
 878}
 879
 880static unsigned int acm_tty_write_room(struct tty_struct *tty)
 881{
 882	struct acm *acm = tty->driver_data;
 883	/*
 884	 * Do not let the line discipline to know that we have a reserve,
 885	 * or it might get too enthusiastic.
 886	 */
 887	return acm_wb_is_avail(acm) ? acm->writesize : 0;
 888}
 889
 890static void acm_tty_flush_buffer(struct tty_struct *tty)
 891{
 892	struct acm *acm = tty->driver_data;
 893	unsigned long flags;
 894	int i;
 895
 896	spin_lock_irqsave(&acm->write_lock, flags);
 897	for (i = 0; i < ACM_NW; i++)
 898		if (acm->wb[i].use)
 899			usb_unlink_urb(acm->wb[i].urb);
 900	spin_unlock_irqrestore(&acm->write_lock, flags);
 901}
 902
 903static unsigned int acm_tty_chars_in_buffer(struct tty_struct *tty)
 904{
 905	struct acm *acm = tty->driver_data;
 906	/*
 907	 * if the device was unplugged then any remaining characters fell out
 908	 * of the connector ;)
 909	 */
 910	if (acm->disconnected)
 911		return 0;
 912	/*
 913	 * This is inaccurate (overcounts), but it works.
 914	 */
 915	return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
 916}
 917
 918static void acm_tty_throttle(struct tty_struct *tty)
 919{
 920	struct acm *acm = tty->driver_data;
 921
 922	set_bit(ACM_THROTTLED, &acm->flags);
 923}
 924
 925static void acm_tty_unthrottle(struct tty_struct *tty)
 926{
 927	struct acm *acm = tty->driver_data;
 928
 929	clear_bit(ACM_THROTTLED, &acm->flags);
 930
 931	/* Matches the smp_mb__after_atomic() in acm_read_bulk_callback(). */
 932	smp_mb();
 933
 934	acm_submit_read_urbs(acm, GFP_KERNEL);
 935}
 936
 937static int acm_tty_break_ctl(struct tty_struct *tty, int state)
 938{
 939	struct acm *acm = tty->driver_data;
 940	int retval;
 941
 942	if (!(acm->ctrl_caps & USB_CDC_CAP_BRK))
 943		return -EOPNOTSUPP;
 944
 945	retval = acm_send_break(acm, state ? 0xffff : 0);
 946	if (retval < 0)
 947		dev_dbg(&acm->control->dev,
 948			"%s - send break failed\n", __func__);
 949	return retval;
 950}
 951
 952static int acm_tty_tiocmget(struct tty_struct *tty)
 953{
 954	struct acm *acm = tty->driver_data;
 955
 956	return (acm->ctrlout & USB_CDC_CTRL_DTR ? TIOCM_DTR : 0) |
 957	       (acm->ctrlout & USB_CDC_CTRL_RTS ? TIOCM_RTS : 0) |
 958	       (acm->ctrlin  & USB_CDC_SERIAL_STATE_DSR ? TIOCM_DSR : 0) |
 959	       (acm->ctrlin  & USB_CDC_SERIAL_STATE_RING_SIGNAL ? TIOCM_RI : 0) |
 960	       (acm->ctrlin  & USB_CDC_SERIAL_STATE_DCD ? TIOCM_CD : 0) |
 961	       TIOCM_CTS;
 962}
 963
 964static int acm_tty_tiocmset(struct tty_struct *tty,
 965			    unsigned int set, unsigned int clear)
 966{
 967	struct acm *acm = tty->driver_data;
 968	unsigned int newctrl;
 969
 970	newctrl = acm->ctrlout;
 971	set = (set & TIOCM_DTR ? USB_CDC_CTRL_DTR : 0) |
 972	      (set & TIOCM_RTS ? USB_CDC_CTRL_RTS : 0);
 973	clear = (clear & TIOCM_DTR ? USB_CDC_CTRL_DTR : 0) |
 974		(clear & TIOCM_RTS ? USB_CDC_CTRL_RTS : 0);
 975
 976	newctrl = (newctrl & ~clear) | set;
 977
 978	if (acm->ctrlout == newctrl)
 979		return 0;
 980	return acm_set_control(acm, acm->ctrlout = newctrl);
 981}
 982
 983static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 984{
 985	struct acm *acm = tty->driver_data;
 986
 987	ss->line = acm->minor;
 988	mutex_lock(&acm->port.mutex);
 989	ss->close_delay	= jiffies_to_msecs(acm->port.close_delay) / 10;
 990	ss->closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
 991				ASYNC_CLOSING_WAIT_NONE :
 992				jiffies_to_msecs(acm->port.closing_wait) / 10;
 993	mutex_unlock(&acm->port.mutex);
 994	return 0;
 995}
 996
 997static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 998{
 999	struct acm *acm = tty->driver_data;
1000	unsigned int closing_wait, close_delay;
1001	int retval = 0;
1002
1003	close_delay = msecs_to_jiffies(ss->close_delay * 10);
1004	closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
1005			ASYNC_CLOSING_WAIT_NONE :
1006			msecs_to_jiffies(ss->closing_wait * 10);
1007
1008	mutex_lock(&acm->port.mutex);
1009
1010	if (!capable(CAP_SYS_ADMIN)) {
1011		if ((close_delay != acm->port.close_delay) ||
1012		    (closing_wait != acm->port.closing_wait))
1013			retval = -EPERM;
1014	} else {
1015		acm->port.close_delay  = close_delay;
1016		acm->port.closing_wait = closing_wait;
1017	}
1018
1019	mutex_unlock(&acm->port.mutex);
1020	return retval;
1021}
1022
1023static int wait_serial_change(struct acm *acm, unsigned long arg)
1024{
1025	int rv = 0;
1026	DECLARE_WAITQUEUE(wait, current);
1027	struct async_icount old, new;
1028
1029	do {
1030		spin_lock_irq(&acm->read_lock);
1031		old = acm->oldcount;
1032		new = acm->iocount;
1033		acm->oldcount = new;
1034		spin_unlock_irq(&acm->read_lock);
1035
1036		if ((arg & TIOCM_DSR) &&
1037			old.dsr != new.dsr)
1038			break;
1039		if ((arg & TIOCM_CD)  &&
1040			old.dcd != new.dcd)
1041			break;
1042		if ((arg & TIOCM_RI) &&
1043			old.rng != new.rng)
1044			break;
1045
1046		add_wait_queue(&acm->wioctl, &wait);
1047		set_current_state(TASK_INTERRUPTIBLE);
1048		schedule();
1049		remove_wait_queue(&acm->wioctl, &wait);
1050		if (acm->disconnected) {
1051			if (arg & TIOCM_CD)
1052				break;
1053			else
1054				rv = -ENODEV;
1055		} else {
1056			if (signal_pending(current))
1057				rv = -ERESTARTSYS;
1058		}
1059	} while (!rv);
1060
1061	
1062
1063	return rv;
1064}
1065
1066static int acm_tty_get_icount(struct tty_struct *tty,
1067					struct serial_icounter_struct *icount)
1068{
1069	struct acm *acm = tty->driver_data;
1070
1071	icount->dsr = acm->iocount.dsr;
1072	icount->rng = acm->iocount.rng;
1073	icount->dcd = acm->iocount.dcd;
1074	icount->frame = acm->iocount.frame;
1075	icount->overrun = acm->iocount.overrun;
1076	icount->parity = acm->iocount.parity;
1077	icount->brk = acm->iocount.brk;
1078
1079	return 0;
1080}
1081
1082static int acm_tty_ioctl(struct tty_struct *tty,
1083					unsigned int cmd, unsigned long arg)
1084{
1085	struct acm *acm = tty->driver_data;
1086	int rv = -ENOIOCTLCMD;
1087
1088	switch (cmd) {
1089	case TIOCMIWAIT:
1090		rv = usb_autopm_get_interface(acm->control);
1091		if (rv < 0) {
1092			rv = -EIO;
1093			break;
1094		}
1095		rv = wait_serial_change(acm, arg);
1096		usb_autopm_put_interface(acm->control);
1097		break;
1098	}
1099
1100	return rv;
1101}
1102
1103static void acm_tty_set_termios(struct tty_struct *tty,
1104				const struct ktermios *termios_old)
1105{
1106	struct acm *acm = tty->driver_data;
1107	struct ktermios *termios = &tty->termios;
1108	struct usb_cdc_line_coding newline;
1109	int newctrl = acm->ctrlout;
1110
1111	newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
1112	newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
1113	newline.bParityType = termios->c_cflag & PARENB ?
1114				(termios->c_cflag & PARODD ? 1 : 2) +
1115				(termios->c_cflag & CMSPAR ? 2 : 0) : 0;
1116	newline.bDataBits = tty_get_char_size(termios->c_cflag);
1117
1118	/* FIXME: Needs to clear unsupported bits in the termios */
1119	acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
1120
1121	if (C_BAUD(tty) == B0) {
1122		newline.dwDTERate = acm->line.dwDTERate;
1123		newctrl &= ~USB_CDC_CTRL_DTR;
1124	} else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
1125		newctrl |=  USB_CDC_CTRL_DTR;
1126	}
1127
1128	if (newctrl != acm->ctrlout)
1129		acm_set_control(acm, acm->ctrlout = newctrl);
1130
1131	if (memcmp(&acm->line, &newline, sizeof newline)) {
1132		memcpy(&acm->line, &newline, sizeof newline);
1133		dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
1134			__func__,
1135			le32_to_cpu(newline.dwDTERate),
1136			newline.bCharFormat, newline.bParityType,
1137			newline.bDataBits);
1138		acm_set_line(acm, &acm->line);
1139	}
1140}
1141
1142static const struct tty_port_operations acm_port_ops = {
1143	.dtr_rts = acm_port_dtr_rts,
1144	.shutdown = acm_port_shutdown,
1145	.activate = acm_port_activate,
1146	.destruct = acm_port_destruct,
1147};
1148
1149/*
1150 * USB probe and disconnect routines.
1151 */
1152
1153/* Little helpers: write/read buffers free */
1154static void acm_write_buffers_free(struct acm *acm)
1155{
1156	int i;
1157	struct acm_wb *wb;
1158
1159	for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
1160		usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
1161}
1162
1163static void acm_read_buffers_free(struct acm *acm)
1164{
1165	int i;
1166
1167	for (i = 0; i < acm->rx_buflimit; i++)
1168		usb_free_coherent(acm->dev, acm->readsize,
1169			  acm->read_buffers[i].base, acm->read_buffers[i].dma);
1170}
1171
1172/* Little helper: write buffers allocate */
1173static int acm_write_buffers_alloc(struct acm *acm)
1174{
1175	int i;
1176	struct acm_wb *wb;
1177
1178	for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
1179		wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
1180		    &wb->dmah);
1181		if (!wb->buf) {
1182			while (i != 0) {
1183				--i;
1184				--wb;
1185				usb_free_coherent(acm->dev, acm->writesize,
1186				    wb->buf, wb->dmah);
1187			}
1188			return -ENOMEM;
1189		}
1190	}
1191	return 0;
1192}
1193
1194static int acm_probe(struct usb_interface *intf,
1195		     const struct usb_device_id *id)
1196{
1197	struct usb_cdc_union_desc *union_header = NULL;
1198	struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
1199	unsigned char *buffer = intf->altsetting->extra;
1200	int buflen = intf->altsetting->extralen;
1201	struct usb_interface *control_interface;
1202	struct usb_interface *data_interface;
1203	struct usb_endpoint_descriptor *epctrl = NULL;
1204	struct usb_endpoint_descriptor *epread = NULL;
1205	struct usb_endpoint_descriptor *epwrite = NULL;
1206	struct usb_device *usb_dev = interface_to_usbdev(intf);
1207	struct usb_cdc_parsed_header h;
1208	struct acm *acm;
1209	int minor;
1210	int ctrlsize, readsize;
1211	u8 *buf;
1212	int call_intf_num = -1;
1213	int data_intf_num = -1;
1214	unsigned long quirks;
1215	int num_rx_buf;
1216	int i;
1217	int combined_interfaces = 0;
1218	struct device *tty_dev;
1219	int rv = -ENOMEM;
1220	int res;
1221
1222	/* normal quirks */
1223	quirks = (unsigned long)id->driver_info;
1224
1225	if (quirks == IGNORE_DEVICE)
1226		return -ENODEV;
1227
1228	memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
1229
1230	num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
1231
1232	/* handle quirks deadly to normal probing*/
1233	if (quirks == NO_UNION_NORMAL) {
1234		data_interface = usb_ifnum_to_if(usb_dev, 1);
1235		control_interface = usb_ifnum_to_if(usb_dev, 0);
1236		/* we would crash */
1237		if (!data_interface || !control_interface)
1238			return -ENODEV;
1239		goto skip_normal_probe;
1240	} else if (quirks == NO_UNION_12) {
1241		data_interface = usb_ifnum_to_if(usb_dev, 2);
1242		control_interface = usb_ifnum_to_if(usb_dev, 1);
1243		if (!data_interface || !control_interface)
1244			 return -ENODEV;
1245		goto skip_normal_probe;
1246	}
1247
1248	/* normal probing*/
1249	if (!buffer) {
1250		dev_err(&intf->dev, "Weird descriptor references\n");
1251		return -EINVAL;
1252	}
1253
1254	if (!buflen) {
1255		if (intf->cur_altsetting->endpoint &&
1256				intf->cur_altsetting->endpoint->extralen &&
1257				intf->cur_altsetting->endpoint->extra) {
1258			dev_dbg(&intf->dev,
1259				"Seeking extra descriptors on endpoint\n");
1260			buflen = intf->cur_altsetting->endpoint->extralen;
1261			buffer = intf->cur_altsetting->endpoint->extra;
1262		} else {
1263			dev_err(&intf->dev,
1264				"Zero length descriptor references\n");
1265			return -EINVAL;
1266		}
1267	}
1268
1269	cdc_parse_cdc_header(&h, intf, buffer, buflen);
1270	union_header = h.usb_cdc_union_desc;
1271	cmgmd = h.usb_cdc_call_mgmt_descriptor;
1272	if (cmgmd)
1273		call_intf_num = cmgmd->bDataInterface;
1274
1275	if (!union_header) {
1276		if (intf->cur_altsetting->desc.bNumEndpoints == 3) {
1277			dev_dbg(&intf->dev, "No union descriptor, assuming single interface\n");
1278			combined_interfaces = 1;
1279			control_interface = data_interface = intf;
1280			goto look_for_collapsed_interface;
1281		} else if (call_intf_num > 0) {
1282			dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1283			data_intf_num = call_intf_num;
1284			data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
1285			control_interface = intf;
1286		} else {
1287			dev_dbg(&intf->dev, "No union descriptor, giving up\n");
1288			return -ENODEV;
1289		}
1290	} else {
1291		int class = -1;
1292
1293		data_intf_num = union_header->bSlaveInterface0;
1294		control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1295		data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
1296
1297		if (control_interface)
1298			class = control_interface->cur_altsetting->desc.bInterfaceClass;
1299
1300		if (class != USB_CLASS_COMM && class != USB_CLASS_CDC_DATA) {
1301			dev_dbg(&intf->dev, "Broken union descriptor, assuming single interface\n");
1302			combined_interfaces = 1;
1303			control_interface = data_interface = intf;
1304			goto look_for_collapsed_interface;
1305		}
1306	}
1307
1308	if (!control_interface || !data_interface) {
1309		dev_dbg(&intf->dev, "no interfaces\n");
1310		return -ENODEV;
1311	}
1312
1313	if (data_intf_num != call_intf_num)
1314		dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1315
1316	if (control_interface == data_interface) {
1317		/* some broken devices designed for windows work this way */
1318		dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1319		combined_interfaces = 1;
1320		/* a popular other OS doesn't use it */
1321		quirks |= NO_CAP_LINE;
1322		if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1323			dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1324			return -EINVAL;
1325		}
1326look_for_collapsed_interface:
1327		res = usb_find_common_endpoints(data_interface->cur_altsetting,
1328				&epread, &epwrite, &epctrl, NULL);
1329		if (res)
1330			return res;
1331
1332		goto made_compressed_probe;
1333	}
1334
1335skip_normal_probe:
1336
1337	/*workaround for switched interfaces */
1338	if (data_interface->cur_altsetting->desc.bInterfaceClass != USB_CLASS_CDC_DATA) {
1339		if (control_interface->cur_altsetting->desc.bInterfaceClass == USB_CLASS_CDC_DATA) {
1340			dev_dbg(&intf->dev,
1341				"Your device has switched interfaces.\n");
1342			swap(control_interface, data_interface);
1343		} else if (quirks & VENDOR_CLASS_DATA_IFACE) {
1344			dev_dbg(&intf->dev,
1345				"Vendor-specific data interface class, continuing.\n");
1346		} else {
1347			return -EINVAL;
1348		}
1349	}
1350
1351	/* Accept probe requests only for the control interface */
1352	if (!combined_interfaces && intf != control_interface)
1353		return -ENODEV;
1354
1355	if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1356	    control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1357		return -EINVAL;
1358
1359	epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1360	epread = &data_interface->cur_altsetting->endpoint[0].desc;
1361	epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1362
1363
1364	/* workaround for switched endpoints */
1365	if (!usb_endpoint_dir_in(epread)) {
1366		/* descriptors are swapped */
1367		dev_dbg(&intf->dev,
1368			"The data interface has switched endpoints\n");
1369		swap(epread, epwrite);
1370	}
1371made_compressed_probe:
1372	dev_dbg(&intf->dev, "interfaces are valid\n");
1373
1374	acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1375	if (!acm)
1376		return -ENOMEM;
1377
1378	tty_port_init(&acm->port);
1379	acm->port.ops = &acm_port_ops;
1380
1381	ctrlsize = usb_endpoint_maxp(epctrl);
1382	readsize = usb_endpoint_maxp(epread) *
1383				(quirks == SINGLE_RX_URB ? 1 : 2);
1384	acm->combined_interfaces = combined_interfaces;
1385	acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1386	acm->control = control_interface;
1387	acm->data = data_interface;
1388
1389	usb_get_intf(acm->control); /* undone in destruct() */
1390
1391	minor = acm_alloc_minor(acm);
1392	if (minor < 0) {
1393		acm->minor = ACM_MINOR_INVALID;
1394		goto err_put_port;
1395	}
1396
1397	acm->minor = minor;
1398	acm->dev = usb_dev;
1399	if (h.usb_cdc_acm_descriptor)
1400		acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
1401	if (quirks & NO_CAP_LINE)
1402		acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1403	if (quirks & MISSING_CAP_BRK)
1404		acm->ctrl_caps |= USB_CDC_CAP_BRK;
1405	acm->ctrlsize = ctrlsize;
1406	acm->readsize = readsize;
1407	acm->rx_buflimit = num_rx_buf;
1408	INIT_DELAYED_WORK(&acm->dwork, acm_softint);
1409	init_waitqueue_head(&acm->wioctl);
1410	spin_lock_init(&acm->write_lock);
1411	spin_lock_init(&acm->read_lock);
1412	mutex_init(&acm->mutex);
1413	if (usb_endpoint_xfer_int(epread)) {
1414		acm->bInterval = epread->bInterval;
1415		acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
1416	} else {
1417		acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1418	}
1419	if (usb_endpoint_xfer_int(epwrite))
1420		acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
1421	else
1422		acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
1423	init_usb_anchor(&acm->delayed);
1424	acm->quirks = quirks;
1425
1426	buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1427	if (!buf)
1428		goto err_put_port;
1429	acm->ctrl_buffer = buf;
1430
1431	if (acm_write_buffers_alloc(acm) < 0)
1432		goto err_free_ctrl_buffer;
1433
1434	acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1435	if (!acm->ctrlurb)
1436		goto err_free_write_buffers;
1437
1438	for (i = 0; i < num_rx_buf; i++) {
1439		struct acm_rb *rb = &(acm->read_buffers[i]);
1440		struct urb *urb;
1441
1442		rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1443								&rb->dma);
1444		if (!rb->base)
1445			goto err_free_read_urbs;
1446		rb->index = i;
1447		rb->instance = acm;
1448
1449		urb = usb_alloc_urb(0, GFP_KERNEL);
1450		if (!urb)
1451			goto err_free_read_urbs;
1452
1453		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1454		urb->transfer_dma = rb->dma;
1455		if (usb_endpoint_xfer_int(epread))
1456			usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
1457					 acm->readsize,
1458					 acm_read_bulk_callback, rb,
1459					 acm->bInterval);
1460		else
1461			usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
1462					  acm->readsize,
1463					  acm_read_bulk_callback, rb);
1464
1465		acm->read_urbs[i] = urb;
1466		__set_bit(i, &acm->read_urbs_free);
1467	}
1468	for (i = 0; i < ACM_NW; i++) {
1469		struct acm_wb *snd = &(acm->wb[i]);
1470
1471		snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1472		if (!snd->urb)
1473			goto err_free_write_urbs;
1474
1475		if (usb_endpoint_xfer_int(epwrite))
1476			usb_fill_int_urb(snd->urb, usb_dev, acm->out,
1477				NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1478		else
1479			usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
1480				NULL, acm->writesize, acm_write_bulk, snd);
1481		snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1482		if (quirks & SEND_ZERO_PACKET)
1483			snd->urb->transfer_flags |= URB_ZERO_PACKET;
1484		snd->instance = acm;
1485	}
1486
1487	usb_set_intfdata(intf, acm);
1488
1489	i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1490	if (i < 0)
1491		goto err_free_write_urbs;
1492
1493	if (h.usb_cdc_country_functional_desc) { /* export the country data */
1494		struct usb_cdc_country_functional_desc * cfd =
1495					h.usb_cdc_country_functional_desc;
1496
1497		acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1498		if (!acm->country_codes)
1499			goto skip_countries;
1500		acm->country_code_size = cfd->bLength - 4;
1501		memcpy(acm->country_codes, cfd->wCountryCodes,
1502							cfd->bLength - 4);
1503		acm->country_rel_date = cfd->iCountryCodeRelDate;
1504
1505		i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1506		if (i < 0) {
1507			kfree(acm->country_codes);
1508			acm->country_codes = NULL;
1509			acm->country_code_size = 0;
1510			goto skip_countries;
1511		}
1512
1513		i = device_create_file(&intf->dev,
1514						&dev_attr_iCountryCodeRelDate);
1515		if (i < 0) {
1516			device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1517			kfree(acm->country_codes);
1518			acm->country_codes = NULL;
1519			acm->country_code_size = 0;
1520			goto skip_countries;
1521		}
1522	}
1523
1524skip_countries:
1525	usb_fill_int_urb(acm->ctrlurb, usb_dev,
1526			 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1527			 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1528			 /* works around buggy devices */
1529			 epctrl->bInterval ? epctrl->bInterval : 16);
1530	acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1531	acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1532	acm->notification_buffer = NULL;
1533	acm->nb_index = 0;
1534	acm->nb_size = 0;
1535
1536	acm->line.dwDTERate = cpu_to_le32(9600);
1537	acm->line.bDataBits = 8;
1538	acm_set_line(acm, &acm->line);
1539
1540	if (quirks & ALWAYS_POLL_CTRL)
1541		acm_set_control(acm, USB_CDC_CTRL_DTR | USB_CDC_CTRL_RTS);
1542
1543	if (!acm->combined_interfaces) {
1544		rv = usb_driver_claim_interface(&acm_driver, data_interface, acm);
1545		if (rv)
1546			goto err_remove_files;
1547	}
1548
1549	if (quirks & CLEAR_HALT_CONDITIONS) {
1550		/* errors intentionally ignored */
1551		usb_clear_halt(usb_dev, acm->in);
1552		usb_clear_halt(usb_dev, acm->out);
1553	}
1554
1555	tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1556			&control_interface->dev);
1557	if (IS_ERR(tty_dev)) {
1558		rv = PTR_ERR(tty_dev);
1559		goto err_release_data_interface;
1560	}
1561
1562	dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1563
1564	if (acm->quirks & ALWAYS_POLL_CTRL) {
1565		acm->ctrlurb->dev = acm->dev;
1566		if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL))
1567			dev_warn(&intf->dev,
1568				 "failed to start persistent ctrl polling\n");
1569	}
1570
1571	return 0;
1572
1573err_release_data_interface:
1574	if (!acm->combined_interfaces) {
1575		/* Clear driver data so that disconnect() returns early. */
1576		usb_set_intfdata(data_interface, NULL);
1577		usb_driver_release_interface(&acm_driver, data_interface);
1578	}
1579err_remove_files:
1580	if (acm->country_codes) {
1581		device_remove_file(&acm->control->dev,
1582				&dev_attr_wCountryCodes);
1583		device_remove_file(&acm->control->dev,
1584				&dev_attr_iCountryCodeRelDate);
1585	}
1586	device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1587err_free_write_urbs:
1588	for (i = 0; i < ACM_NW; i++)
1589		usb_free_urb(acm->wb[i].urb);
1590err_free_read_urbs:
1591	for (i = 0; i < num_rx_buf; i++)
1592		usb_free_urb(acm->read_urbs[i]);
1593	acm_read_buffers_free(acm);
1594	usb_free_urb(acm->ctrlurb);
1595err_free_write_buffers:
1596	acm_write_buffers_free(acm);
1597err_free_ctrl_buffer:
1598	usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1599err_put_port:
1600	tty_port_put(&acm->port);
1601
1602	return rv;
1603}
1604
1605static void acm_disconnect(struct usb_interface *intf)
1606{
1607	struct acm *acm = usb_get_intfdata(intf);
1608	int i;
1609
1610	/* sibling interface is already cleaning up */
1611	if (!acm)
1612		return;
1613
1614	acm->disconnected = true;
1615	/*
1616	 * there is a circular dependency. acm_softint() can resubmit
1617	 * the URBs in error handling so we need to block any
1618	 * submission right away
1619	 */
1620	acm_poison_urbs(acm);
1621	mutex_lock(&acm->mutex);
1622	if (acm->country_codes) {
1623		device_remove_file(&acm->control->dev,
1624				&dev_attr_wCountryCodes);
1625		device_remove_file(&acm->control->dev,
1626				&dev_attr_iCountryCodeRelDate);
1627	}
1628	wake_up_all(&acm->wioctl);
1629	device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1630	usb_set_intfdata(acm->control, NULL);
1631	usb_set_intfdata(acm->data, NULL);
1632	mutex_unlock(&acm->mutex);
1633
1634	tty_port_tty_vhangup(&acm->port);
1635
1636	cancel_delayed_work_sync(&acm->dwork);
1637
1638	tty_unregister_device(acm_tty_driver, acm->minor);
1639
1640	usb_free_urb(acm->ctrlurb);
1641	for (i = 0; i < ACM_NW; i++)
1642		usb_free_urb(acm->wb[i].urb);
1643	for (i = 0; i < acm->rx_buflimit; i++)
1644		usb_free_urb(acm->read_urbs[i]);
1645	acm_write_buffers_free(acm);
1646	usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1647	acm_read_buffers_free(acm);
1648
1649	kfree(acm->notification_buffer);
1650
1651	if (!acm->combined_interfaces)
1652		usb_driver_release_interface(&acm_driver, intf == acm->control ?
1653					acm->data : acm->control);
1654
1655	tty_port_put(&acm->port);
1656}
1657
1658#ifdef CONFIG_PM
1659static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1660{
1661	struct acm *acm = usb_get_intfdata(intf);
1662	int cnt;
1663
1664	spin_lock_irq(&acm->write_lock);
1665	if (PMSG_IS_AUTO(message)) {
1666		if (acm->transmitting) {
1667			spin_unlock_irq(&acm->write_lock);
1668			return -EBUSY;
1669		}
1670	}
1671	cnt = acm->susp_count++;
1672	spin_unlock_irq(&acm->write_lock);
1673
1674	if (cnt)
1675		return 0;
1676
1677	acm_poison_urbs(acm);
1678	cancel_delayed_work_sync(&acm->dwork);
1679	acm->urbs_in_error_delay = 0;
1680
1681	return 0;
1682}
1683
1684static int acm_resume(struct usb_interface *intf)
1685{
1686	struct acm *acm = usb_get_intfdata(intf);
1687	struct urb *urb;
1688	int rv = 0;
1689
1690	spin_lock_irq(&acm->write_lock);
1691
1692	if (--acm->susp_count)
1693		goto out;
1694
1695	acm_unpoison_urbs(acm);
1696
1697	if (tty_port_initialized(&acm->port) || (acm->quirks & ALWAYS_POLL_CTRL)) {
1698		rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
1699
1700		for (;;) {
1701			urb = usb_get_from_anchor(&acm->delayed);
1702			if (!urb)
1703				break;
1704
1705			acm_start_wb(acm, urb->context);
1706		}
1707
1708		/*
1709		 * delayed error checking because we must
1710		 * do the write path at all cost
1711		 */
1712		if (rv < 0)
1713			goto out;
1714
1715		rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
1716	}
1717out:
1718	spin_unlock_irq(&acm->write_lock);
1719
1720	return rv;
1721}
1722
1723static int acm_reset_resume(struct usb_interface *intf)
1724{
1725	struct acm *acm = usb_get_intfdata(intf);
1726
1727	if (tty_port_initialized(&acm->port))
1728		tty_port_tty_hangup(&acm->port, false);
1729
1730	return acm_resume(intf);
1731}
1732
1733#endif /* CONFIG_PM */
1734
1735static int acm_pre_reset(struct usb_interface *intf)
1736{
1737	struct acm *acm = usb_get_intfdata(intf);
1738
1739	clear_bit(EVENT_RX_STALL, &acm->flags);
1740	acm->nb_index = 0; /* pending control transfers are lost */
1741
1742	return 0;
1743}
1744
1745#define NOKIA_PCSUITE_ACM_INFO(x) \
1746		USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1747		USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1748		USB_CDC_ACM_PROTO_VENDOR)
1749
1750#define SAMSUNG_PCSUITE_ACM_INFO(x) \
1751		USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1752		USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1753		USB_CDC_ACM_PROTO_VENDOR)
1754
1755/*
1756 * USB driver structure.
1757 */
1758
1759static const struct usb_device_id acm_ids[] = {
1760	/* quirky and broken devices */
1761	{ USB_DEVICE(0x0424, 0x274e), /* Microchip Technology, Inc. (formerly SMSC) */
1762	  .driver_info = DISABLE_ECHO, }, /* DISABLE ECHO in termios flag */
1763	{ USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
1764	.driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
1765	{ USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
1766	.driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
1767	{ USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1768	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1769	},
1770	{ USB_DEVICE(0x045b, 0x023c),	/* Renesas R-Car H3 USB Download mode */
1771	.driver_info = DISABLE_ECHO,	/* Don't echo banner */
1772	},
1773	{ USB_DEVICE(0x045b, 0x0247),	/* Renesas R-Car D3 USB Download mode */
1774	.driver_info = DISABLE_ECHO,	/* Don't echo banner */
1775	},
1776	{ USB_DEVICE(0x045b, 0x0248),	/* Renesas R-Car M3-N USB Download mode */
1777	.driver_info = DISABLE_ECHO,	/* Don't echo banner */
1778	},
1779	{ USB_DEVICE(0x045b, 0x024D),	/* Renesas R-Car E3 USB Download mode */
1780	.driver_info = DISABLE_ECHO,	/* Don't echo banner */
1781	},
1782	{ USB_DEVICE(0x04b8, 0x0d12),	/* EPSON HMD Com&Sens */
1783	.driver_info = NO_UNION_12,	/* union descriptor is garbage */
1784	},
1785	{ USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1786	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1787	},
1788	{ USB_DEVICE(0x0e8d, 0x2000), /* MediaTek Inc Preloader */
1789	.driver_info = DISABLE_ECHO, /* DISABLE ECHO in termios flag */
1790	},
1791	{ USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1792	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1793	},
1794	{ USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1795	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1796	},
1797	{ USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1798	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1799	},
1800	{ USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1801	.driver_info = SINGLE_RX_URB,
1802	},
1803	{ USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1804	.driver_info = SINGLE_RX_URB, /* firmware bug */
1805	},
1806	{ USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1807	.driver_info = SINGLE_RX_URB, /* firmware bug */
1808	},
1809	{ USB_DEVICE(0x11ca, 0x0201), /* VeriFone Mx870 Gadget Serial */
1810	.driver_info = SINGLE_RX_URB,
1811	},
1812	{ USB_DEVICE(0x1901, 0x0006), /* GE Healthcare Patient Monitor UI Controller */
1813	.driver_info = DISABLE_ECHO, /* DISABLE ECHO in termios flag */
1814	},
1815	{ USB_DEVICE(0x1965, 0x0018), /* Uniden UBC125XLT */
1816	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1817	},
1818	{ USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1819	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1820	},
1821	{ USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1822	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1823	},
1824	{ USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1825	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1826	},
1827	{ USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1828	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1829	},
1830	{ USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1831	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1832	},
1833	{ USB_DEVICE(0x0572, 0x1349), /* Hiro (Conexant) USB MODEM H50228 */
1834	.driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1835	},
1836	{ USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
1837	.driver_info = QUIRK_CONTROL_LINE_STATE, },
1838	{ USB_DEVICE(0x2184, 0x001c) },	/* GW Instek AFG-2225 */
1839	{ USB_DEVICE(0x2184, 0x0036) },	/* GW Instek AFG-125 */
1840	{ USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1841	},
1842	/* Motorola H24 HSPA module: */
1843	{ USB_DEVICE(0x22b8, 0x2d91) }, /* modem                                */
1844	{ USB_DEVICE(0x22b8, 0x2d92),   /* modem           + diagnostics        */
1845	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1846	},
1847	{ USB_DEVICE(0x22b8, 0x2d93),   /* modem + AT port                      */
1848	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1849	},
1850	{ USB_DEVICE(0x22b8, 0x2d95),   /* modem + AT port + diagnostics        */
1851	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1852	},
1853	{ USB_DEVICE(0x22b8, 0x2d96),   /* modem                         + NMEA */
1854	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1855	},
1856	{ USB_DEVICE(0x22b8, 0x2d97),   /* modem           + diagnostics + NMEA */
1857	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1858	},
1859	{ USB_DEVICE(0x22b8, 0x2d99),   /* modem + AT port               + NMEA */
1860	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1861	},
1862	{ USB_DEVICE(0x22b8, 0x2d9a),   /* modem + AT port + diagnostics + NMEA */
1863	.driver_info = NO_UNION_NORMAL, /* handle only modem interface          */
1864	},
1865
1866	{ USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1867	.driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1868					   data interface instead of
1869					   communications interface.
1870					   Maybe we should define a new
1871					   quirk for this. */
1872	},
1873	{ USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1874	.driver_info = NO_UNION_NORMAL,
1875	},
1876	{ USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1877	.driver_info = NO_UNION_NORMAL,
1878	},
1879	{ USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1880	.driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1881	},
1882	{ USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1883	.driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1884	},
1885	{ USB_DEVICE(0xfff0, 0x0100), /* DATECS FP-2000 */
1886	.driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1887	},
1888	{ USB_DEVICE(0x09d8, 0x0320), /* Elatec GmbH TWN3 */
1889	.driver_info = NO_UNION_NORMAL, /* has misplaced union descriptor */
1890	},
1891	{ USB_DEVICE(0x0c26, 0x0020), /* Icom ICF3400 Serie */
1892	.driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1893	},
1894	{ USB_DEVICE(0x0ca6, 0xa050), /* Castles VEGA3000 */
1895	.driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1896	},
1897
1898	{ USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
1899	.driver_info = CLEAR_HALT_CONDITIONS,
1900	},
1901
1902	/* Nokia S60 phones expose two ACM channels. The first is
1903	 * a modem and is picked up by the standard AT-command
1904	 * information below. The second is 'vendor-specific' but
1905	 * is treated as a serial device at the S60 end, so we want
1906	 * to expose it on Linux too. */
1907	{ NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1908	{ NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1909	{ NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1910	{ NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1911	{ NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1912	{ NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1913	{ NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1914	{ NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1915	{ NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1916	{ NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1917	{ NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1918	{ NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1919	{ NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1920	{ NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1921	{ NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1922	{ NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1923	{ NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1924	{ NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i  */
1925	{ NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1926	{ NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1927	{ NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1928	{ NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic &  */
1929	{ NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1930	{ NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1931	{ NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1932	{ NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1933	{ NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1934	{ NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1935	{ NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1936	{ NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1937	{ NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1938	{ NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB  */
1939	{ NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1940	{ NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1941	{ NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1942	{ NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1943	{ NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1944	{ NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1945	{ NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3  */
1946	{ NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1947	{ NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1948	{ NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1949	{ NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1950	{ NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1951	{ NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1952	{ NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1953	{ NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1954	{ NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1955	{ NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1956	{ NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1957	{ NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1958	{ NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1959	{ NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1960	{ NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1961	{ NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1962	{ NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1963	{ SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1964
1965	/* Support for Owen devices */
1966	{ USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1967
1968	/* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1969
1970#if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1971	{ USB_DEVICE(0x04d8, 0x0082),	/* Application mode */
1972	.driver_info = IGNORE_DEVICE,
1973	},
1974	{ USB_DEVICE(0x04d8, 0x0083),	/* Bootloader mode */
1975	.driver_info = IGNORE_DEVICE,
1976	},
1977#endif
1978
1979#if IS_ENABLED(CONFIG_IR_TOY)
1980	{ USB_DEVICE(0x04d8, 0xfd08),
1981	.driver_info = IGNORE_DEVICE,
1982	},
1983
1984	{ USB_DEVICE(0x04d8, 0xf58b),
1985	.driver_info = IGNORE_DEVICE,
1986	},
1987#endif
1988
1989#if IS_ENABLED(CONFIG_USB_SERIAL_XR)
1990	{ USB_DEVICE(0x04e2, 0x1400), .driver_info = IGNORE_DEVICE },
1991	{ USB_DEVICE(0x04e2, 0x1401), .driver_info = IGNORE_DEVICE },
1992	{ USB_DEVICE(0x04e2, 0x1402), .driver_info = IGNORE_DEVICE },
1993	{ USB_DEVICE(0x04e2, 0x1403), .driver_info = IGNORE_DEVICE },
1994	{ USB_DEVICE(0x04e2, 0x1410), .driver_info = IGNORE_DEVICE },
1995	{ USB_DEVICE(0x04e2, 0x1411), .driver_info = IGNORE_DEVICE },
1996	{ USB_DEVICE(0x04e2, 0x1412), .driver_info = IGNORE_DEVICE },
1997	{ USB_DEVICE(0x04e2, 0x1414), .driver_info = IGNORE_DEVICE },
1998	{ USB_DEVICE(0x04e2, 0x1420), .driver_info = IGNORE_DEVICE },
1999	{ USB_DEVICE(0x04e2, 0x1422), .driver_info = IGNORE_DEVICE },
2000	{ USB_DEVICE(0x04e2, 0x1424), .driver_info = IGNORE_DEVICE },
2001#endif
2002
2003	/*Samsung phone in firmware update mode */
2004	{ USB_DEVICE(0x04e8, 0x685d),
2005	.driver_info = IGNORE_DEVICE,
2006	},
2007
2008	/* Exclude Infineon Flash Loader utility */
2009	{ USB_DEVICE(0x058b, 0x0041),
2010	.driver_info = IGNORE_DEVICE,
2011	},
2012
2013	/* Exclude ETAS ES58x */
2014	{ USB_DEVICE(0x108c, 0x0159), /* ES581.4 */
2015	.driver_info = IGNORE_DEVICE,
2016	},
2017	{ USB_DEVICE(0x108c, 0x0168), /* ES582.1 */
2018	.driver_info = IGNORE_DEVICE,
2019	},
2020	{ USB_DEVICE(0x108c, 0x0169), /* ES584.1 */
2021	.driver_info = IGNORE_DEVICE,
2022	},
2023
2024	{ USB_DEVICE(0x1bc7, 0x0021), /* Telit 3G ACM only composition */
2025	.driver_info = SEND_ZERO_PACKET,
2026	},
2027	{ USB_DEVICE(0x1bc7, 0x0023), /* Telit 3G ACM + ECM composition */
2028	.driver_info = SEND_ZERO_PACKET,
2029	},
2030
2031	/* Exclude Goodix Fingerprint Reader */
2032	{ USB_DEVICE(0x27c6, 0x5395),
2033	.driver_info = IGNORE_DEVICE,
2034	},
2035
2036	/* Exclude Heimann Sensor GmbH USB appset demo */
2037	{ USB_DEVICE(0x32a7, 0x0000),
2038	.driver_info = IGNORE_DEVICE,
2039	},
2040
2041	/* CH343 supports CAP_BRK, but doesn't advertise it */
2042	{ USB_DEVICE(0x1a86, 0x55d3), .driver_info = MISSING_CAP_BRK, },
2043
2044	/*
2045	 * Lenovo Yoga Book 9 14IAH10 (83KJ) — INGENIC 17EF:6161 touchscreen
2046	 * composite device.  The CDC ACM control interface (0) uses a standard
2047	 * Union descriptor, but the data interface (1) is declared as vendor-
2048	 * specific class (0xff) with no CDC data descriptors, so cdc-acm would
2049	 * normally reject it.  The firmware also requires continuous polling of
2050	 * the notification endpoint (EP 0x82) to suppress a 20-second watchdog
2051	 * reset; ALWAYS_POLL_CTRL keeps the ctrlurb active even when no TTY is
2052	 * open.  Match only the control interface by class to avoid probing the
2053	 * vendor-specific data interface.
2054	 */
2055	{ USB_DEVICE_INTERFACE_CLASS(0x17ef, 0x6161, USB_CLASS_COMM),
2056	  .driver_info = VENDOR_CLASS_DATA_IFACE | ALWAYS_POLL_CTRL },
2057
2058	/* control interfaces without any protocol set */
2059	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2060		USB_CDC_PROTO_NONE) },
2061
2062	/* control interfaces with various AT-command sets */
2063	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2064		USB_CDC_ACM_PROTO_AT_V25TER) },
2065	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2066		USB_CDC_ACM_PROTO_AT_PCCA101) },
2067	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2068		USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
2069	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2070		USB_CDC_ACM_PROTO_AT_GSM) },
2071	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2072		USB_CDC_ACM_PROTO_AT_3G) },
2073	{ USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
2074		USB_CDC_ACM_PROTO_AT_CDMA) },
2075
2076	{ USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
2077	.driver_info = SEND_ZERO_PACKET,
2078	},
2079
2080	{ }
2081};
2082
2083MODULE_DEVICE_TABLE(usb, acm_ids);
2084
2085static struct usb_driver acm_driver = {
2086	.name =		"cdc_acm",
2087	.probe =	acm_probe,
2088	.disconnect =	acm_disconnect,
2089#ifdef CONFIG_PM
2090	.suspend =	acm_suspend,
2091	.resume =	acm_resume,
2092	.reset_resume =	acm_reset_resume,
2093#endif
2094	.pre_reset =	acm_pre_reset,
2095	.id_table =	acm_ids,
2096#ifdef CONFIG_PM
2097	.supports_autosuspend = 1,
2098#endif
2099	.disable_hub_initiated_lpm = 1,
2100};
2101
2102/*
2103 * TTY driver structures.
2104 */
2105
2106static const struct tty_operations acm_ops = {
2107	.install =		acm_tty_install,
2108	.open =			acm_tty_open,
2109	.close =		acm_tty_close,
2110	.cleanup =		acm_tty_cleanup,
2111	.hangup =		acm_tty_hangup,
2112	.write =		acm_tty_write,
2113	.write_room =		acm_tty_write_room,
2114	.flush_buffer =		acm_tty_flush_buffer,
2115	.ioctl =		acm_tty_ioctl,
2116	.throttle =		acm_tty_throttle,
2117	.unthrottle =		acm_tty_unthrottle,
2118	.chars_in_buffer =	acm_tty_chars_in_buffer,
2119	.break_ctl =		acm_tty_break_ctl,
2120	.set_termios =		acm_tty_set_termios,
2121	.tiocmget =		acm_tty_tiocmget,
2122	.tiocmset =		acm_tty_tiocmset,
2123	.get_serial =		get_serial_info,
2124	.set_serial =		set_serial_info,
2125	.get_icount =		acm_tty_get_icount,
2126};
2127
2128/*
2129 * Init / exit.
2130 */
2131
2132static int __init acm_init(void)
2133{
2134	int retval;
2135	acm_tty_driver = tty_alloc_driver(ACM_TTY_MINORS, TTY_DRIVER_REAL_RAW |
2136			TTY_DRIVER_DYNAMIC_DEV);
2137	if (IS_ERR(acm_tty_driver))
2138		return PTR_ERR(acm_tty_driver);
2139	acm_tty_driver->driver_name = "acm",
2140	acm_tty_driver->name = "ttyACM",
2141	acm_tty_driver->major = ACM_TTY_MAJOR,
2142	acm_tty_driver->minor_start = 0,
2143	acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
2144	acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
2145	acm_tty_driver->init_termios = tty_std_termios;
2146	acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
2147								HUPCL | CLOCAL;
2148	tty_set_operations(acm_tty_driver, &acm_ops);
2149
2150	retval = tty_register_driver(acm_tty_driver);
2151	if (retval) {
2152		tty_driver_kref_put(acm_tty_driver);
2153		return retval;
2154	}
2155
2156	retval = usb_register(&acm_driver);
2157	if (retval) {
2158		tty_unregister_driver(acm_tty_driver);
2159		tty_driver_kref_put(acm_tty_driver);
2160		return retval;
2161	}
2162
2163	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
2164
2165	return 0;
2166}
2167
2168static void __exit acm_exit(void)
2169{
2170	usb_deregister(&acm_driver);
2171	tty_unregister_driver(acm_tty_driver);
2172	tty_driver_kref_put(acm_tty_driver);
2173	idr_destroy(&acm_minors);
2174}
2175
2176module_init(acm_init);
2177module_exit(acm_exit);
2178
2179MODULE_AUTHOR(DRIVER_AUTHOR);
2180MODULE_DESCRIPTION(DRIVER_DESC);
2181MODULE_LICENSE("GPL");
2182MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);
~/ Read-only file preview.