blob: cdabe76960bcb1133f83766406899ef87d84838e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cdc-acm.c
3 *
4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 Kubicek61a87ad2005-11-01 18:51:34 +01009 * Copyright (c) 2005 David Kubicek <dave@awk.cz>
Johan Hovold088c64f2011-03-25 11:06:02 +010010 * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * USB Abstract Control Model driver for USB modems and ISDN adapters
13 *
14 * Sponsored by SuSE
15 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31#undef DEBUG
David Brownelle5fbab52008-08-06 18:46:10 -070032#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010035#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/errno.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/tty.h>
Oliver Neukum7af25b42009-09-08 23:51:28 +020040#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tty_driver.h>
42#include <linux/tty_flip.h>
43#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010044#include <linux/mutex.h>
Alan Cox10077d42009-06-11 12:36:09 +010045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070047#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/byteorder.h>
49#include <asm/unaligned.h>
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020050#include <linux/idr.h>
David Kubicek61a87ad2005-11-01 18:51:34 +010051#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "cdc-acm.h"
54
David Brownelle5fbab52008-08-06 18:46:10 -070055
Johan Hovold088c64f2011-03-25 11:06:02 +010056#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
58
59static struct usb_driver acm_driver;
60static struct tty_driver *acm_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020062static DEFINE_IDR(acm_minors);
63static DEFINE_MUTEX(acm_minors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jim Paris24cb4502014-10-30 11:04:47 -040065static void acm_tty_set_termios(struct tty_struct *tty,
66 struct ktermios *termios_old);
67
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080068/*
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020069 * acm_minors accessors
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080072/*
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020073 * Look up an ACM structure by minor. If found and not disconnected, increment
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080074 * its refcount and return it with its mutex held.
75 */
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020076static struct acm *acm_get_by_minor(unsigned int minor)
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080077{
78 struct acm *acm;
79
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020080 mutex_lock(&acm_minors_lock);
81 acm = idr_find(&acm_minors, minor);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080082 if (acm) {
83 mutex_lock(&acm->mutex);
84 if (acm->disconnected) {
85 mutex_unlock(&acm->mutex);
86 acm = NULL;
87 } else {
88 tty_port_get(&acm->port);
89 mutex_unlock(&acm->mutex);
90 }
91 }
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020092 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080093 return acm;
94}
95
96/*
97 * Try to find an available minor number and if found, associate it with 'acm'.
98 */
99static int acm_alloc_minor(struct acm *acm)
100{
101 int minor;
102
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200103 mutex_lock(&acm_minors_lock);
104 minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
105 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800106
107 return minor;
108}
109
110/* Release the minor number associated with 'acm'. */
111static void acm_release_minor(struct acm *acm)
112{
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200113 mutex_lock(&acm_minors_lock);
114 idr_remove(&acm_minors, acm->minor);
115 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800116}
Alan Cox739e0282009-06-11 12:27:50 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Functions for ACM control messages.
120 */
121
Alan Cox6e47e062009-06-11 12:37:06 +0100122static int acm_ctrl_msg(struct acm *acm, int request, int value,
123 void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200125 int retval;
126
127 retval = usb_autopm_get_interface(acm->control);
128 if (retval)
129 return retval;
130
131 retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 request, USB_RT_ACM, value,
133 acm->control->altsetting[0].desc.bInterfaceNumber,
134 buf, len, 5000);
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200135
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100136 dev_dbg(&acm->control->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100137 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
138 __func__, request, value, len, retval);
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200139
140 usb_autopm_put_interface(acm->control);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return retval < 0 ? retval : 0;
143}
144
145/* devices aren't required to support these requests.
146 * the cdc acm descriptor tells whether they do...
147 */
Johan Hovold2a8cdfd2014-11-06 18:08:33 +0100148static inline int acm_set_control(struct acm *acm, int control)
149{
150 if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
151 return -EOPNOTSUPP;
152
153 return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
154 control, NULL, 0);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define acm_set_line(acm, line) \
158 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
159#define acm_send_break(acm, ms) \
160 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
161
Ladislav Michlba8c9312016-11-18 19:07:08 +0100162static void acm_kill_urbs(struct acm *acm)
163{
164 int i;
165
166 usb_kill_urb(acm->ctrlurb);
167 for (i = 0; i < ACM_NW; i++)
168 usb_kill_urb(acm->wb[i].urb);
169 for (i = 0; i < acm->rx_buflimit; i++)
170 usb_kill_urb(acm->read_urbs[i]);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Oliver Neukum884b6002005-04-21 21:28:02 +0200174 * Write buffer management.
175 * All of these assume proper locks taken by the caller.
176 */
177
178static int acm_wb_alloc(struct acm *acm)
179{
180 int i, wbn;
181 struct acm_wb *wb;
182
David Engrafe4cf3aa2008-03-20 10:01:34 +0100183 wbn = 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200184 i = 0;
185 for (;;) {
186 wb = &acm->wb[wbn];
187 if (!wb->use) {
188 wb->use = 1;
189 return wbn;
190 }
Oliver Neukum86478942006-05-13 22:50:47 +0200191 wbn = (wbn + 1) % ACM_NW;
192 if (++i >= ACM_NW)
Oliver Neukum884b6002005-04-21 21:28:02 +0200193 return -1;
194 }
195}
196
Oliver Neukum884b6002005-04-21 21:28:02 +0200197static int acm_wb_is_avail(struct acm *acm)
198{
199 int i, n;
David Brownelle5fbab52008-08-06 18:46:10 -0700200 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200201
Oliver Neukum86478942006-05-13 22:50:47 +0200202 n = ACM_NW;
David Brownelle5fbab52008-08-06 18:46:10 -0700203 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100204 for (i = 0; i < ACM_NW; i++)
Oliver Neukum86478942006-05-13 22:50:47 +0200205 n -= acm->wb[i].use;
David Brownelle5fbab52008-08-06 18:46:10 -0700206 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200207 return n;
208}
209
Oliver Neukum884b6002005-04-21 21:28:02 +0200210/*
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800211 * Finish write. Caller must hold acm->write_lock
Oliver Neukum884b6002005-04-21 21:28:02 +0200212 */
David Engrafe4cf3aa2008-03-20 10:01:34 +0100213static void acm_write_done(struct acm *acm, struct acm_wb *wb)
Oliver Neukum884b6002005-04-21 21:28:02 +0200214{
David Engrafe4cf3aa2008-03-20 10:01:34 +0100215 wb->use = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200216 acm->transmitting--;
Oliver Neukum97d35f92009-12-16 17:05:57 +0100217 usb_autopm_put_interface_async(acm->control);
Oliver Neukum884b6002005-04-21 21:28:02 +0200218}
219
220/*
221 * Poke write.
Oliver Neukum11ea8592008-06-20 11:25:57 +0200222 *
223 * the caller is responsible for locking
Oliver Neukum884b6002005-04-21 21:28:02 +0200224 */
Oliver Neukum11ea8592008-06-20 11:25:57 +0200225
226static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
227{
228 int rc;
229
230 acm->transmitting++;
231
232 wb->urb->transfer_buffer = wb->buf;
233 wb->urb->transfer_dma = wb->dmah;
234 wb->urb->transfer_buffer_length = wb->len;
235 wb->urb->dev = acm->dev;
236
Alan Cox6e47e062009-06-11 12:37:06 +0100237 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
238 if (rc < 0) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100239 dev_err(&acm->data->dev,
240 "%s - usb_submit_urb(write bulk) failed: %d\n",
241 __func__, rc);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200242 acm_write_done(acm, wb);
243 }
244 return rc;
245}
246
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100247/*
248 * attributes exported through sysfs
249 */
250static ssize_t show_caps
251(struct device *dev, struct device_attribute *attr, char *buf)
252{
253 struct usb_interface *intf = to_usb_interface(dev);
254 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum884b6002005-04-21 21:28:02 +0200255
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100256 return sprintf(buf, "%d", acm->ctrl_caps);
257}
258static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
259
260static ssize_t show_country_codes
261(struct device *dev, struct device_attribute *attr, char *buf)
262{
263 struct usb_interface *intf = to_usb_interface(dev);
264 struct acm *acm = usb_get_intfdata(intf);
265
266 memcpy(buf, acm->country_codes, acm->country_code_size);
267 return acm->country_code_size;
268}
269
270static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
271
272static ssize_t show_country_rel_date
273(struct device *dev, struct device_attribute *attr, char *buf)
274{
275 struct usb_interface *intf = to_usb_interface(dev);
276 struct acm *acm = usb_get_intfdata(intf);
277
278 return sprintf(buf, "%d", acm->country_rel_date);
279}
280
281static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
Oliver Neukum884b6002005-04-21 21:28:02 +0200282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * Interrupt handlers for various ACM device responses
284 */
285
286/* control interface reports status changes with "interrupt" transfers */
David Howells7d12e782006-10-05 14:55:46 +0100287static void acm_ctrl_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct acm *acm = urb->context;
290 struct usb_cdc_notification *dr = urb->transfer_buffer;
291 unsigned char *data;
292 int newctrl;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100293 int difference;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700294 int retval;
295 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700297 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case 0:
299 /* success */
300 break;
301 case -ECONNRESET:
302 case -ENOENT:
303 case -ESHUTDOWN:
304 /* this urb is terminated, clean up */
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100305 dev_dbg(&acm->control->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100306 "%s - urb shutting down with status: %d\n",
307 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return;
309 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100310 dev_dbg(&acm->control->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100311 "%s - nonzero urb status received: %d\n",
312 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto exit;
314 }
315
Johan Hovold7e7797e2011-03-22 11:12:11 +0100316 usb_mark_last_busy(acm->dev);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 data = (unsigned char *)(dr + 1);
319 switch (dr->bNotificationType) {
Alan Cox6e47e062009-06-11 12:37:06 +0100320 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
Ladislav Michl90744af82016-11-18 19:01:22 +0100321 dev_dbg(&acm->control->dev,
322 "%s - network connection: %d\n", __func__, dr->wValue);
Alan Cox6e47e062009-06-11 12:37:06 +0100323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Alan Cox6e47e062009-06-11 12:37:06 +0100325 case USB_CDC_NOTIFY_SERIAL_STATE:
Tobias Herzog1bb99142017-03-30 22:15:10 +0200326 if (le16_to_cpu(dr->wLength) != 2) {
327 dev_dbg(&acm->control->dev,
328 "%s - malformed serial state\n", __func__);
329 break;
330 }
331
Alan Cox6e47e062009-06-11 12:37:06 +0100332 newctrl = get_unaligned_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jiri Slabyaa27a092013-03-07 13:12:30 +0100334 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
Ladislav Michl90744af82016-11-18 19:01:22 +0100335 dev_dbg(&acm->control->dev,
336 "%s - calling hangup\n", __func__);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100337 tty_port_tty_hangup(&acm->port, false);
Alan Cox6e47e062009-06-11 12:37:06 +0100338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100340 difference = acm->ctrlin ^ newctrl;
341 spin_lock(&acm->read_lock);
Alan Cox6e47e062009-06-11 12:37:06 +0100342 acm->ctrlin = newctrl;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100343 acm->oldcount = acm->iocount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100345 if (difference & ACM_CTRL_DSR)
346 acm->iocount.dsr++;
347 if (difference & ACM_CTRL_BRK)
348 acm->iocount.brk++;
349 if (difference & ACM_CTRL_RI)
350 acm->iocount.rng++;
351 if (difference & ACM_CTRL_DCD)
352 acm->iocount.dcd++;
353 if (difference & ACM_CTRL_FRAMING)
354 acm->iocount.frame++;
355 if (difference & ACM_CTRL_PARITY)
356 acm->iocount.parity++;
357 if (difference & ACM_CTRL_OVERRUN)
358 acm->iocount.overrun++;
359 spin_unlock(&acm->read_lock);
360
361 if (difference)
362 wake_up_all(&acm->wioctl);
363
364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Alan Cox6e47e062009-06-11 12:37:06 +0100366 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100367 dev_dbg(&acm->control->dev,
Tobias Herzog1bb99142017-03-30 22:15:10 +0200368 "%s - unknown notification %d received: index %d len %d\n",
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100369 __func__,
Tobias Herzog1bb99142017-03-30 22:15:10 +0200370 dr->bNotificationType, dr->wIndex, dr->wLength);
371
Alan Cox6e47e062009-06-11 12:37:06 +0100372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374exit:
Alan Cox6e47e062009-06-11 12:37:06 +0100375 retval = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum6c8074e2015-03-20 11:25:17 +0100376 if (retval && retval != -EPERM)
Ladislav Michl90744af82016-11-18 19:01:22 +0100377 dev_err(&acm->control->dev,
378 "%s - usb_submit_urb failed: %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Johan Hovold088c64f2011-03-25 11:06:02 +0100381static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Johan Hovold088c64f2011-03-25 11:06:02 +0100383 int res;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700384
Johan Hovold088c64f2011-03-25 11:06:02 +0100385 if (!test_and_clear_bit(index, &acm->read_urbs_free))
386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Johan Hovold088c64f2011-03-25 11:06:02 +0100388 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
389 if (res) {
390 if (res != -EPERM) {
391 dev_err(&acm->data->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100392 "urb %d failed submission with %d\n",
393 index, res);
Johan Hovold088c64f2011-03-25 11:06:02 +0100394 }
395 set_bit(index, &acm->read_urbs_free);
396 return res;
Oliver Neukum46e75072016-09-13 16:44:36 +0200397 } else {
398 dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
Johan Hovold088c64f2011-03-25 11:06:02 +0100399 }
400
401 return 0;
402}
403
404static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
405{
406 int res;
407 int i;
408
409 for (i = 0; i < acm->rx_buflimit; ++i) {
410 res = acm_submit_read_urb(acm, i, mem_flags);
411 if (res)
412 return res;
413 }
414
415 return 0;
416}
417
418static void acm_process_read_urb(struct acm *acm, struct urb *urb)
419{
Johan Hovold088c64f2011-03-25 11:06:02 +0100420 if (!urb->actual_length)
421 return;
422
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100423 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
424 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100425 tty_flip_buffer_push(&acm->port);
Johan Hovold088c64f2011-03-25 11:06:02 +0100426}
427
428static void acm_read_bulk_callback(struct urb *urb)
429{
430 struct acm_rb *rb = urb->context;
431 struct acm *acm = rb->instance;
432 unsigned long flags;
Oliver Neukum36e59e02015-03-20 09:24:24 +0100433 int status = urb->status;
Johan Hovold088c64f2011-03-25 11:06:02 +0100434
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200435 dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
Ladislav Michl90744af82016-11-18 19:01:22 +0100436 rb->index, urb->actual_length, status);
Johan Hovold088c64f2011-03-25 11:06:02 +0100437
Ladislav Michl1aba5792016-11-18 19:11:26 +0100438 set_bit(rb->index, &acm->read_urbs_free);
439
Johan Hovold088c64f2011-03-25 11:06:02 +0100440 if (!acm->dev) {
441 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Ladislav Michl1aba5792016-11-18 19:11:26 +0100445 switch (status) {
446 case 0:
447 usb_mark_last_busy(acm->dev);
448 acm_process_read_urb(acm, urb);
449 break;
450 case -EPIPE:
451 set_bit(EVENT_RX_STALL, &acm->flags);
452 schedule_work(&acm->work);
453 return;
454 case -ENOENT:
455 case -ECONNRESET:
456 case -ESHUTDOWN:
457 dev_dbg(&acm->data->dev,
458 "%s - urb shutting down with status: %d\n",
459 __func__, status);
460 return;
461 default:
462 dev_dbg(&acm->data->dev,
463 "%s - nonzero urb status received: %d\n",
464 __func__, status);
465 break;
Johan Hovold088c64f2011-03-25 11:06:02 +0100466 }
Johan Hovold4a8ee502014-05-26 19:23:49 +0200467
Oliver Neukum36e59e02015-03-20 09:24:24 +0100468 /*
469 * Unthrottle may run on another CPU which needs to see events
470 * in the same order. Submission has an implict barrier
471 */
472 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Johan Hovold088c64f2011-03-25 11:06:02 +0100474 /* throttle device if requested by tty */
475 spin_lock_irqsave(&acm->read_lock, flags);
476 acm->throttled = acm->throttle_req;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +0200477 if (!acm->throttled) {
Johan Hovold088c64f2011-03-25 11:06:02 +0100478 spin_unlock_irqrestore(&acm->read_lock, flags);
479 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
Oliver Neukum86478942006-05-13 22:50:47 +0200480 } else {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200481 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100486static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Ming Leicdc97792008-02-24 18:41:47 +0800488 struct acm_wb *wb = urb->context;
David Brownelle5fbab52008-08-06 18:46:10 -0700489 struct acm *acm = wb->instance;
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800490 unsigned long flags;
Oliver Neukum4132cd02015-03-20 11:41:06 +0100491 int status = urb->status;
Oliver Neukum884b6002005-04-21 21:28:02 +0200492
Oliver Neukum4132cd02015-03-20 11:41:06 +0100493 if (status || (urb->actual_length != urb->transfer_buffer_length))
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200494 dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
David Brownelle5fbab52008-08-06 18:46:10 -0700495 urb->actual_length,
496 urb->transfer_buffer_length,
Oliver Neukum4132cd02015-03-20 11:41:06 +0100497 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800499 spin_lock_irqsave(&acm->write_lock, flags);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100500 acm_write_done(acm, wb);
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800501 spin_unlock_irqrestore(&acm->write_lock, flags);
Ladislav Michl1aba5792016-11-18 19:11:26 +0100502 set_bit(EVENT_TTY_WAKEUP, &acm->flags);
Havard Skinnemoen99823f42011-12-09 16:51:54 -0800503 schedule_work(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
David Howellsc4028952006-11-22 14:57:56 +0000506static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Ladislav Michl1aba5792016-11-18 19:11:26 +0100508 int i;
David Howellsc4028952006-11-22 14:57:56 +0000509 struct acm *acm = container_of(work, struct acm, work);
David Brownelle5fbab52008-08-06 18:46:10 -0700510
Ladislav Michl1aba5792016-11-18 19:11:26 +0100511 if (test_bit(EVENT_RX_STALL, &acm->flags)) {
512 if (!(usb_autopm_get_interface(acm->data))) {
513 for (i = 0; i < acm->rx_buflimit; i++)
514 usb_kill_urb(acm->read_urbs[i]);
515 usb_clear_halt(acm->dev, acm->in);
516 acm_submit_read_urbs(acm, GFP_KERNEL);
517 usb_autopm_put_interface(acm->data);
518 }
519 clear_bit(EVENT_RX_STALL, &acm->flags);
520 }
521
522 if (test_bit(EVENT_TTY_WAKEUP, &acm->flags)) {
523 tty_port_tty_wakeup(&acm->port);
524 clear_bit(EVENT_TTY_WAKEUP, &acm->flags);
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/*
529 * TTY handlers
530 */
531
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800532static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct acm *acm;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800535 int retval;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100536
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200537 acm = acm_get_by_minor(tty->index);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800538 if (!acm)
539 return -ENODEV;
540
Jiri Slabyf8a8c102012-03-05 14:51:48 +0100541 retval = tty_standard_install(driver, tty);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800542 if (retval)
543 goto error_init_termios;
544
545 tty->driver_data = acm;
546
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800547 return 0;
548
549error_init_termios:
550 tty_port_put(&acm->port);
551 return retval;
552}
553
554static int acm_tty_open(struct tty_struct *tty, struct file *filp)
555{
556 struct acm *acm = tty->driver_data;
557
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800558 return tty_port_open(&acm->port, tty, filp);
559}
560
Johan Hovold0943d8e2014-05-26 19:23:51 +0200561static void acm_port_dtr_rts(struct tty_port *port, int raise)
562{
563 struct acm *acm = container_of(port, struct acm, port);
564 int val;
565 int res;
566
567 if (raise)
568 val = ACM_CTRL_DTR | ACM_CTRL_RTS;
569 else
570 val = 0;
571
572 /* FIXME: add missing ctrlout locking throughout driver */
573 acm->ctrlout = val;
574
575 res = acm_set_control(acm, val);
576 if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
577 dev_err(&acm->control->dev, "failed to set dtr/rts\n");
578}
579
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800580static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
581{
582 struct acm *acm = container_of(port, struct acm, port);
583 int retval = -ENODEV;
Johan Hovolde4c36072014-05-26 19:23:44 +0200584 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Oliver Neukum1365baf2007-10-12 17:24:28 +0200586 mutex_lock(&acm->mutex);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800587 if (acm->disconnected)
588 goto disconnected;
589
590 retval = usb_autopm_get_interface(acm->control);
591 if (retval)
592 goto error_get_interface;
593
594 /*
595 * FIXME: Why do we need this? Allocating 64K of physically contiguous
596 * memory is really nasty...
597 */
598 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
599 acm->control->needs_remote_wakeup = 1;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 acm->ctrlurb->dev = acm->dev;
Johan Hovold8727bf62014-05-26 19:23:43 +0200602 retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
603 if (retval) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100604 dev_err(&acm->control->dev,
605 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800606 goto error_submit_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Jim Paris24cb4502014-10-30 11:04:47 -0400609 acm_tty_set_termios(tty, NULL);
610
Otto Meta6c4707f2012-06-06 18:46:21 +0200611 /*
612 * Unthrottle device in case the TTY was closed while throttled.
613 */
614 spin_lock_irq(&acm->read_lock);
615 acm->throttled = 0;
616 acm->throttle_req = 0;
617 spin_unlock_irq(&acm->read_lock);
618
Johan Hovold8727bf62014-05-26 19:23:43 +0200619 retval = acm_submit_read_urbs(acm, GFP_KERNEL);
620 if (retval)
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800621 goto error_submit_read_urbs;
Oliver Neukum2b626dc2010-02-03 17:10:22 +0100622
Johan Hovold703df322014-05-26 19:23:42 +0200623 usb_autopm_put_interface(acm->control);
624
Oliver Neukum1365baf2007-10-12 17:24:28 +0200625 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800627 return 0;
628
629error_submit_read_urbs:
Johan Hovolde4c36072014-05-26 19:23:44 +0200630 for (i = 0; i < acm->rx_buflimit; i++)
631 usb_kill_urb(acm->read_urbs[i]);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800632 usb_kill_urb(acm->ctrlurb);
633error_submit_urb:
Johan Hovold703df322014-05-26 19:23:42 +0200634 usb_autopm_put_interface(acm->control);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800635error_get_interface:
636disconnected:
637 mutex_unlock(&acm->mutex);
Johan Hovold8727bf62014-05-26 19:23:43 +0200638
639 return usb_translate_errors(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800642static void acm_port_destruct(struct tty_port *port)
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700643{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800644 struct acm *acm = container_of(port, struct acm, port);
645
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800646 acm_release_minor(acm);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700647 usb_put_intf(acm->control);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100648 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700649 kfree(acm);
650}
651
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800652static void acm_port_shutdown(struct tty_port *port)
Alan Cox10077d42009-06-11 12:36:09 +0100653{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800654 struct acm *acm = container_of(port, struct acm, port);
Johan Hovold140cb812014-05-26 19:23:38 +0200655 struct urb *urb;
656 struct acm_wb *wb;
Johan Hovolddab54c92011-03-22 11:12:21 +0100657
Johan Hovoldb1d42ef2014-05-26 19:23:48 +0200658 /*
659 * Need to grab write_lock to prevent race with resume, but no need to
660 * hold it due to the tty-port initialised flag.
661 */
662 spin_lock_irq(&acm->write_lock);
663 spin_unlock_irq(&acm->write_lock);
664
665 usb_autopm_get_interface_no_resume(acm->control);
666 acm->control->needs_remote_wakeup = 0;
667 usb_autopm_put_interface(acm->control);
668
Johan Hovold89e54e42014-05-26 19:23:47 +0200669 for (;;) {
670 urb = usb_get_from_anchor(&acm->delayed);
671 if (!urb)
672 break;
673 wb = urb->context;
674 wb->use = 0;
675 usb_autopm_put_interface_async(acm->control);
Alan Cox10077d42009-06-11 12:36:09 +0100676 }
Johan Hovold89e54e42014-05-26 19:23:47 +0200677
Ladislav Michlba8c9312016-11-18 19:07:08 +0100678 acm_kill_urbs(acm);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800679}
680
681static void acm_tty_cleanup(struct tty_struct *tty)
682{
683 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200684
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800685 tty_port_put(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100686}
687
688static void acm_tty_hangup(struct tty_struct *tty)
689{
690 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200691
Alan Cox10077d42009-06-11 12:36:09 +0100692 tty_port_hangup(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static void acm_tty_close(struct tty_struct *tty, struct file *filp)
696{
697 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200698
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800699 tty_port_close(&acm->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Alan Cox6e47e062009-06-11 12:37:06 +0100702static int acm_tty_write(struct tty_struct *tty,
703 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct acm *acm = tty->driver_data;
706 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200707 unsigned long flags;
708 int wbn;
709 struct acm_wb *wb;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!count)
712 return 0;
713
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200714 dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100715
Oliver Neukum884b6002005-04-21 21:28:02 +0200716 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100717 wbn = acm_wb_alloc(acm);
718 if (wbn < 0) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200719 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200722 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700724 if (!acm->dev) {
725 wb->use = 0;
726 spin_unlock_irqrestore(&acm->write_lock, flags);
727 return -ENODEV;
728 }
729
Oliver Neukum884b6002005-04-21 21:28:02 +0200730 count = (count > acm->writesize) ? acm->writesize : count;
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200731 dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
Oliver Neukum884b6002005-04-21 21:28:02 +0200732 memcpy(wb->buf, buf, count);
733 wb->len = count;
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700734
Johan Hovold183a4502014-05-26 19:23:41 +0200735 stat = usb_autopm_get_interface_async(acm->control);
736 if (stat) {
737 wb->use = 0;
738 spin_unlock_irqrestore(&acm->write_lock, flags);
739 return stat;
740 }
741
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700742 if (acm->susp_count) {
Oliver Neukuma81cf972016-02-10 10:39:49 +0100743 if (acm->putbuffer) {
744 /* now to preserve order */
745 usb_anchor_urb(acm->putbuffer->urb, &acm->delayed);
746 acm->putbuffer = NULL;
747 }
Johan Hovold140cb812014-05-26 19:23:38 +0200748 usb_anchor_urb(wb->urb, &acm->delayed);
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700749 spin_unlock_irqrestore(&acm->write_lock, flags);
Johan Hovold140cb812014-05-26 19:23:38 +0200750 return count;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100751 } else {
752 if (acm->putbuffer) {
753 /* at this point there is no good way to handle errors */
754 acm_start_wb(acm, acm->putbuffer);
755 acm->putbuffer = NULL;
756 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700757 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700758
759 stat = acm_start_wb(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200760 spin_unlock_irqrestore(&acm->write_lock, flags);
761
Alan Cox6e47e062009-06-11 12:37:06 +0100762 if (stat < 0)
Oliver Neukum884b6002005-04-21 21:28:02 +0200763 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return count;
765}
766
Oliver Neukuma81cf972016-02-10 10:39:49 +0100767static void acm_tty_flush_chars(struct tty_struct *tty)
768{
769 struct acm *acm = tty->driver_data;
770 struct acm_wb *cur = acm->putbuffer;
771 int err;
772 unsigned long flags;
773
Oliver Neukum2a147592016-04-04 14:30:53 +0200774 if (!cur) /* nothing to do */
775 return;
776
Oliver Neukuma81cf972016-02-10 10:39:49 +0100777 acm->putbuffer = NULL;
778 err = usb_autopm_get_interface_async(acm->control);
779 spin_lock_irqsave(&acm->write_lock, flags);
780 if (err < 0) {
781 cur->use = 0;
Oliver Neukum2a147592016-04-04 14:30:53 +0200782 acm->putbuffer = cur;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100783 goto out;
784 }
785
786 if (acm->susp_count)
787 usb_anchor_urb(cur->urb, &acm->delayed);
788 else
789 acm_start_wb(acm, cur);
790out:
791 spin_unlock_irqrestore(&acm->write_lock, flags);
792 return;
793}
794
795static int acm_tty_put_char(struct tty_struct *tty, unsigned char ch)
796{
797 struct acm *acm = tty->driver_data;
798 struct acm_wb *cur;
799 int wbn;
800 unsigned long flags;
801
802overflow:
803 cur = acm->putbuffer;
804 if (!cur) {
805 spin_lock_irqsave(&acm->write_lock, flags);
806 wbn = acm_wb_alloc(acm);
807 if (wbn >= 0) {
808 cur = &acm->wb[wbn];
809 acm->putbuffer = cur;
810 }
811 spin_unlock_irqrestore(&acm->write_lock, flags);
812 if (!cur)
813 return 0;
814 }
815
816 if (cur->len == acm->writesize) {
817 acm_tty_flush_chars(tty);
818 goto overflow;
819 }
820
821 cur->buf[cur->len++] = ch;
822 return 1;
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825static int acm_tty_write_room(struct tty_struct *tty)
826{
827 struct acm *acm = tty->driver_data;
Oliver Neukum884b6002005-04-21 21:28:02 +0200828 /*
829 * Do not let the line discipline to know that we have a reserve,
830 * or it might get too enthusiastic.
831 */
David Brownell934da462008-08-06 18:44:12 -0700832 return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835static int acm_tty_chars_in_buffer(struct tty_struct *tty)
836{
837 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800838 /*
839 * if the device was unplugged then any remaining characters fell out
840 * of the connector ;)
841 */
842 if (acm->disconnected)
Alan Cox23198fd2009-07-20 16:05:27 +0100843 return 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200844 /*
845 * This is inaccurate (overcounts), but it works.
846 */
Oliver Neukum86478942006-05-13 22:50:47 +0200847 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static void acm_tty_throttle(struct tty_struct *tty)
851{
852 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100853
Johan Hovold088c64f2011-03-25 11:06:02 +0100854 spin_lock_irq(&acm->read_lock);
855 acm->throttle_req = 1;
856 spin_unlock_irq(&acm->read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859static void acm_tty_unthrottle(struct tty_struct *tty)
860{
861 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100862 unsigned int was_throttled;
863
Johan Hovold088c64f2011-03-25 11:06:02 +0100864 spin_lock_irq(&acm->read_lock);
865 was_throttled = acm->throttled;
866 acm->throttled = 0;
867 acm->throttle_req = 0;
868 spin_unlock_irq(&acm->read_lock);
869
870 if (was_throttled)
871 acm_submit_read_urbs(acm, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Alan Cox9e989662008-07-22 11:18:03 +0100874static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct acm *acm = tty->driver_data;
Alan Cox9e989662008-07-22 11:18:03 +0100877 int retval;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800878
Alan Cox9e989662008-07-22 11:18:03 +0100879 retval = acm_send_break(acm, state ? 0xffff : 0);
880 if (retval < 0)
Ladislav Michl90744af82016-11-18 19:01:22 +0100881 dev_dbg(&acm->control->dev,
882 "%s - send break failed\n", __func__);
Alan Cox9e989662008-07-22 11:18:03 +0100883 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Alan Cox60b33c12011-02-14 16:26:14 +0000886static int acm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct acm *acm = tty->driver_data;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
891 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
892 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
893 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
894 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
895 TIOCM_CTS;
896}
897
Alan Cox20b9d172011-02-14 16:26:50 +0000898static int acm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned int set, unsigned int clear)
900{
901 struct acm *acm = tty->driver_data;
902 unsigned int newctrl;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 newctrl = acm->ctrlout;
Alan Cox6e47e062009-06-11 12:37:06 +0100905 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
906 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
907 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
908 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 newctrl = (newctrl & ~clear) | set;
911
912 if (acm->ctrlout == newctrl)
913 return 0;
914 return acm_set_control(acm, acm->ctrlout = newctrl);
915}
916
Oliver Neukum18c75722012-02-17 17:21:24 -0500917static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
918{
919 struct serial_struct tmp;
920
Oliver Neukum18c75722012-02-17 17:21:24 -0500921 memset(&tmp, 0, sizeof(tmp));
Oliver Neukum18c75722012-02-17 17:21:24 -0500922 tmp.xmit_fifo_size = acm->writesize;
923 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600924 tmp.close_delay = acm->port.close_delay / 10;
925 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
926 ASYNC_CLOSING_WAIT_NONE :
927 acm->port.closing_wait / 10;
Oliver Neukum18c75722012-02-17 17:21:24 -0500928
929 if (copy_to_user(info, &tmp, sizeof(tmp)))
930 return -EFAULT;
931 else
932 return 0;
933}
934
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600935static int set_serial_info(struct acm *acm,
936 struct serial_struct __user *newinfo)
937{
938 struct serial_struct new_serial;
939 unsigned int closing_wait, close_delay;
940 int retval = 0;
941
942 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
943 return -EFAULT;
944
945 close_delay = new_serial.close_delay * 10;
946 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
947 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
948
949 mutex_lock(&acm->port.mutex);
950
951 if (!capable(CAP_SYS_ADMIN)) {
952 if ((close_delay != acm->port.close_delay) ||
953 (closing_wait != acm->port.closing_wait))
954 retval = -EPERM;
955 else
956 retval = -EOPNOTSUPP;
957 } else {
958 acm->port.close_delay = close_delay;
959 acm->port.closing_wait = closing_wait;
960 }
961
962 mutex_unlock(&acm->port.mutex);
963 return retval;
964}
965
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100966static int wait_serial_change(struct acm *acm, unsigned long arg)
967{
968 int rv = 0;
969 DECLARE_WAITQUEUE(wait, current);
970 struct async_icount old, new;
971
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100972 do {
973 spin_lock_irq(&acm->read_lock);
974 old = acm->oldcount;
975 new = acm->iocount;
976 acm->oldcount = new;
977 spin_unlock_irq(&acm->read_lock);
978
979 if ((arg & TIOCM_DSR) &&
980 old.dsr != new.dsr)
981 break;
982 if ((arg & TIOCM_CD) &&
983 old.dcd != new.dcd)
984 break;
985 if ((arg & TIOCM_RI) &&
986 old.rng != new.rng)
987 break;
988
989 add_wait_queue(&acm->wioctl, &wait);
990 set_current_state(TASK_INTERRUPTIBLE);
991 schedule();
992 remove_wait_queue(&acm->wioctl, &wait);
993 if (acm->disconnected) {
994 if (arg & TIOCM_CD)
995 break;
996 else
997 rv = -ENODEV;
998 } else {
999 if (signal_pending(current))
1000 rv = -ERESTARTSYS;
1001 }
1002 } while (!rv);
1003
1004
1005
1006 return rv;
1007}
1008
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001009static int acm_tty_get_icount(struct tty_struct *tty,
1010 struct serial_icounter_struct *icount)
Oliver Neukum797ef132013-11-20 11:35:35 +01001011{
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001012 struct acm *acm = tty->driver_data;
Oliver Neukum797ef132013-11-20 11:35:35 +01001013
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001014 icount->dsr = acm->iocount.dsr;
1015 icount->rng = acm->iocount.rng;
1016 icount->dcd = acm->iocount.dcd;
1017 icount->frame = acm->iocount.frame;
1018 icount->overrun = acm->iocount.overrun;
1019 icount->parity = acm->iocount.parity;
1020 icount->brk = acm->iocount.brk;
Oliver Neukum797ef132013-11-20 11:35:35 +01001021
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001022 return 0;
Oliver Neukum797ef132013-11-20 11:35:35 +01001023}
1024
Alan Cox6caa76b2011-02-14 16:27:22 +00001025static int acm_tty_ioctl(struct tty_struct *tty,
Alan Cox6e47e062009-06-11 12:37:06 +01001026 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Oliver Neukum18c75722012-02-17 17:21:24 -05001028 struct acm *acm = tty->driver_data;
1029 int rv = -ENOIOCTLCMD;
1030
1031 switch (cmd) {
1032 case TIOCGSERIAL: /* gets serial port data */
1033 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
1034 break;
Dan Williamsba2d8ce2012-11-08 12:47:41 -06001035 case TIOCSSERIAL:
1036 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
1037 break;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001038 case TIOCMIWAIT:
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001039 rv = usb_autopm_get_interface(acm->control);
1040 if (rv < 0) {
1041 rv = -EIO;
1042 break;
1043 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001044 rv = wait_serial_change(acm, arg);
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001045 usb_autopm_put_interface(acm->control);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001046 break;
Oliver Neukum18c75722012-02-17 17:21:24 -05001047 }
1048
1049 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Alan Cox6e47e062009-06-11 12:37:06 +01001052static void acm_tty_set_termios(struct tty_struct *tty,
1053 struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct acm *acm = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01001056 struct ktermios *termios = &tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct usb_cdc_line_coding newline;
1058 int newctrl = acm->ctrlout;
1059
Alan Cox9b80fee2009-09-19 13:13:23 -07001060 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
1062 newline.bParityType = termios->c_cflag & PARENB ?
Alan Cox6e47e062009-06-11 12:37:06 +01001063 (termios->c_cflag & PARODD ? 1 : 2) +
1064 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
Nicolas Boullis301a29d2012-10-16 00:06:23 +02001065 switch (termios->c_cflag & CSIZE) {
1066 case CS5:
1067 newline.bDataBits = 5;
1068 break;
1069 case CS6:
1070 newline.bDataBits = 6;
1071 break;
1072 case CS7:
1073 newline.bDataBits = 7;
1074 break;
1075 case CS8:
1076 default:
1077 newline.bDataBits = 8;
1078 break;
1079 }
Alan Cox6e47e062009-06-11 12:37:06 +01001080 /* FIXME: Needs to clear unsupported bits in the termios */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
1082
Johan Hovold4473d052014-11-05 18:41:59 +01001083 if (C_BAUD(tty) == B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 newline.dwDTERate = acm->line.dwDTERate;
1085 newctrl &= ~ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001086 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001087 newctrl |= ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 if (newctrl != acm->ctrlout)
1091 acm_set_control(acm, acm->ctrlout = newctrl);
1092
1093 if (memcmp(&acm->line, &newline, sizeof newline)) {
1094 memcpy(&acm->line, &newline, sizeof newline);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001095 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
1096 __func__,
1097 le32_to_cpu(newline.dwDTERate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 newline.bCharFormat, newline.bParityType,
1099 newline.bDataBits);
1100 acm_set_line(acm, &acm->line);
1101 }
1102}
1103
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001104static const struct tty_port_operations acm_port_ops = {
Johan Hovold0943d8e2014-05-26 19:23:51 +02001105 .dtr_rts = acm_port_dtr_rts,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001106 .shutdown = acm_port_shutdown,
1107 .activate = acm_port_activate,
1108 .destruct = acm_port_destruct,
1109};
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/*
1112 * USB probe and disconnect routines.
1113 */
1114
Oliver Neukum830f4022008-06-25 14:17:16 +02001115/* Little helpers: write/read buffers free */
Oliver Neukum884b6002005-04-21 21:28:02 +02001116static void acm_write_buffers_free(struct acm *acm)
1117{
1118 int i;
1119 struct acm_wb *wb;
1120
Alan Cox6e47e062009-06-11 12:37:06 +01001121 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
Ladislav Michle4614602016-11-18 19:06:10 +01001122 usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
Oliver Neukum884b6002005-04-21 21:28:02 +02001123}
1124
Oliver Neukum830f4022008-06-25 14:17:16 +02001125static void acm_read_buffers_free(struct acm *acm)
1126{
Johan Hovolddab54c92011-03-22 11:12:21 +01001127 int i;
Oliver Neukum830f4022008-06-25 14:17:16 +02001128
Johan Hovolddab54c92011-03-22 11:12:21 +01001129 for (i = 0; i < acm->rx_buflimit; i++)
Ladislav Michle4614602016-11-18 19:06:10 +01001130 usb_free_coherent(acm->dev, acm->readsize,
Johan Hovold088c64f2011-03-25 11:06:02 +01001131 acm->read_buffers[i].base, acm->read_buffers[i].dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001132}
1133
Oliver Neukum884b6002005-04-21 21:28:02 +02001134/* Little helper: write buffers allocate */
1135static int acm_write_buffers_alloc(struct acm *acm)
1136{
1137 int i;
1138 struct acm_wb *wb;
1139
Oliver Neukum86478942006-05-13 22:50:47 +02001140 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Daniel Mack997ea582010-04-12 13:17:25 +02001141 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
Oliver Neukum884b6002005-04-21 21:28:02 +02001142 &wb->dmah);
1143 if (!wb->buf) {
1144 while (i != 0) {
1145 --i;
1146 --wb;
Daniel Mack997ea582010-04-12 13:17:25 +02001147 usb_free_coherent(acm->dev, acm->writesize,
Oliver Neukum884b6002005-04-21 21:28:02 +02001148 wb->buf, wb->dmah);
1149 }
1150 return -ENOMEM;
1151 }
1152 }
1153 return 0;
1154}
1155
Alan Cox10077d42009-06-11 12:36:09 +01001156static int acm_probe(struct usb_interface *intf,
1157 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001160 struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
David Brownellc6dbf552008-04-13 14:00:44 -07001161 unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int buflen = intf->altsetting->extralen;
1163 struct usb_interface *control_interface;
1164 struct usb_interface *data_interface;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001165 struct usb_endpoint_descriptor *epctrl = NULL;
1166 struct usb_endpoint_descriptor *epread = NULL;
1167 struct usb_endpoint_descriptor *epwrite = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct usb_device *usb_dev = interface_to_usbdev(intf);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001169 struct usb_cdc_parsed_header h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct acm *acm;
1171 int minor;
Alan Cox6e47e062009-06-11 12:37:06 +01001172 int ctrlsize, readsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 u8 *buf;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001174 int call_intf_num = -1;
1175 int data_intf_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +02001177 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +01001178 int i;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001179 int combined_interfaces = 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001180 struct device *tty_dev;
1181 int rv = -ENOMEM;
Johan Hovoldf8d84642017-03-17 11:35:48 +01001182 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Oliver Neukum86478942006-05-13 22:50:47 +02001184 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 quirks = (unsigned long)id->driver_info;
Dmitry Torokhov16142652013-02-02 11:02:14 -08001186
1187 if (quirks == IGNORE_DEVICE)
1188 return -ENODEV;
1189
Oliver Neukum7309aa82016-11-02 14:42:52 +01001190 memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
1191
Oliver Neukum86478942006-05-13 22:50:47 +02001192 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
1193
1194 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (quirks == NO_UNION_NORMAL) {
1196 data_interface = usb_ifnum_to_if(usb_dev, 1);
1197 control_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukum8835ba42016-03-15 10:14:04 +01001198 /* we would crash */
1199 if (!data_interface || !control_interface)
1200 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto skip_normal_probe;
1202 }
Alan Cox6e47e062009-06-11 12:37:06 +01001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* normal probing*/
1205 if (!buffer) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001206 dev_err(&intf->dev, "Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -EINVAL;
1208 }
1209
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001210 if (!intf->cur_altsetting)
1211 return -EINVAL;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!buflen) {
Toby Gray577045c2010-09-02 10:46:20 +01001214 if (intf->cur_altsetting->endpoint &&
1215 intf->cur_altsetting->endpoint->extralen &&
Alan Cox6e47e062009-06-11 12:37:06 +01001216 intf->cur_altsetting->endpoint->extra) {
1217 dev_dbg(&intf->dev,
1218 "Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 buflen = intf->cur_altsetting->endpoint->extralen;
1220 buffer = intf->cur_altsetting->endpoint->extra;
1221 } else {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001222 dev_err(&intf->dev,
1223 "Zero length descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -EINVAL;
1225 }
1226 }
1227
Oliver Neukumcb42b632016-07-14 15:41:34 +02001228 cdc_parse_cdc_header(&h, intf, buffer, buflen);
1229 union_header = h.usb_cdc_union_desc;
1230 cmgmd = h.usb_cdc_call_mgmt_descriptor;
1231 if (cmgmd)
1232 call_intf_num = cmgmd->bDataInterface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!union_header) {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001235 if (call_intf_num > 0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001236 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
Erik Slagterfd5054c2011-05-11 12:06:55 +02001237 /* quirks for Droids MuIn LCD */
Oliver Neukumcb42b632016-07-14 15:41:34 +02001238 if (quirks & NO_DATA_INTERFACE) {
Erik Slagterfd5054c2011-05-11 12:06:55 +02001239 data_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001240 } else {
1241 data_intf_num = call_intf_num;
1242 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 control_interface = intf;
1245 } else {
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001246 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1247 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1248 return -ENODEV;
1249 } else {
1250 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1251 combined_interfaces = 1;
1252 control_interface = data_interface = intf;
1253 goto look_for_collapsed_interface;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 } else {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001257 data_intf_num = union_header->bSlaveInterface0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001259 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
Greg Kroah-Hartman403dff42014-11-07 08:48:15 -08001260 }
1261
1262 if (!control_interface || !data_interface) {
1263 dev_dbg(&intf->dev, "no interfaces\n");
1264 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001266 if (!data_interface->cur_altsetting || !control_interface->cur_altsetting)
1267 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001268
Oliver Neukumcb42b632016-07-14 15:41:34 +02001269 if (data_intf_num != call_intf_num)
Alan Cox6e47e062009-06-11 12:37:06 +01001270 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001272 if (control_interface == data_interface) {
1273 /* some broken devices designed for windows work this way */
1274 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1275 combined_interfaces = 1;
1276 /* a popular other OS doesn't use it */
1277 quirks |= NO_CAP_LINE;
1278 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1279 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1280 return -EINVAL;
1281 }
1282look_for_collapsed_interface:
Johan Hovoldf8d84642017-03-17 11:35:48 +01001283 res = usb_find_common_endpoints(data_interface->cur_altsetting,
1284 &epread, &epwrite, &epctrl, NULL);
1285 if (res)
1286 return res;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001287
Johan Hovoldf8d84642017-03-17 11:35:48 +01001288 goto made_compressed_probe;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001289 }
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291skip_normal_probe:
1292
1293 /*workaround for switched interfaces */
Alan Cox6e47e062009-06-11 12:37:06 +01001294 if (data_interface->cur_altsetting->desc.bInterfaceClass
1295 != CDC_DATA_INTERFACE_TYPE) {
1296 if (control_interface->cur_altsetting->desc.bInterfaceClass
1297 == CDC_DATA_INTERFACE_TYPE) {
Alan Cox6e47e062009-06-11 12:37:06 +01001298 dev_dbg(&intf->dev,
1299 "Your device has switched interfaces.\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001300 swap(control_interface, data_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 } else {
1302 return -EINVAL;
1303 }
1304 }
Alan Stern74da5d62007-08-02 13:29:10 -04001305
1306 /* Accept probe requests only for the control interface */
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001307 if (!combined_interfaces && intf != control_interface)
Alan Stern74da5d62007-08-02 13:29:10 -04001308 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001309
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001310 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1311 /* valid in this context */
Alan Cox6e47e062009-06-11 12:37:06 +01001312 dev_dbg(&intf->dev, "The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EBUSY;
1314 }
1315
1316
Sven Schnelle99f347c2012-08-17 21:43:43 +02001317 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1318 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EINVAL;
1320
1321 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1322 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1323 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1324
1325
1326 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -03001327 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* descriptors are swapped */
Alan Cox6e47e062009-06-11 12:37:06 +01001329 dev_dbg(&intf->dev,
1330 "The data interface has switched endpoints\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001331 swap(epread, epwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001333made_compressed_probe:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001334 dev_dbg(&intf->dev, "interfaces are valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alan Cox6e47e062009-06-11 12:37:06 +01001336 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001337 if (acm == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001340 minor = acm_alloc_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001341 if (minor < 0)
1342 goto alloc_fail1;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001343
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001344 ctrlsize = usb_endpoint_maxp(epctrl);
1345 readsize = usb_endpoint_maxp(epread) *
Alan Cox6e47e062009-06-11 12:37:06 +01001346 (quirks == SINGLE_RX_URB ? 1 : 2);
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001347 acm->combined_interfaces = combined_interfaces;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001348 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 acm->control = control_interface;
1350 acm->data = data_interface;
1351 acm->minor = minor;
1352 acm->dev = usb_dev;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001353 if (h.usb_cdc_acm_descriptor)
1354 acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001355 if (quirks & NO_CAP_LINE)
1356 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 acm->ctrlsize = ctrlsize;
1358 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +02001359 acm->rx_buflimit = num_rx_buf;
David Howellsc4028952006-11-22 14:57:56 +00001360 INIT_WORK(&acm->work, acm_softint);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001361 init_waitqueue_head(&acm->wioctl);
Oliver Neukum884b6002005-04-21 21:28:02 +02001362 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +01001363 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001364 mutex_init(&acm->mutex);
Ladislav Michld3053942016-11-18 19:10:19 +01001365 if (usb_endpoint_xfer_int(epread)) {
Oliver Neukumcf7fdd52009-08-04 23:52:09 +02001366 acm->bInterval = epread->bInterval;
Ladislav Michl74bccc92016-11-18 19:09:19 +01001367 acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
1368 } else {
1369 acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1370 }
1371 if (usb_endpoint_xfer_int(epwrite))
1372 acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
1373 else
1374 acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
Alan Cox739e0282009-06-11 12:27:50 +01001375 tty_port_init(&acm->port);
1376 acm->port.ops = &acm_port_ops;
Johan Hovold140cb812014-05-26 19:23:38 +02001377 init_usb_anchor(&acm->delayed);
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001378 acm->quirks = quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Daniel Mack997ea582010-04-12 13:17:25 +02001380 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001381 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto alloc_fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 acm->ctrl_buffer = buf;
1384
Oliver Neukum17136d42015-01-28 16:56:24 +01001385 if (acm_write_buffers_alloc(acm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 goto alloc_fail4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001389 if (!acm->ctrlurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto alloc_fail5;
Oliver Neukum17136d42015-01-28 16:56:24 +01001391
Oliver Neukum86478942006-05-13 22:50:47 +02001392 for (i = 0; i < num_rx_buf; i++) {
Johan Hovold088c64f2011-03-25 11:06:02 +01001393 struct acm_rb *rb = &(acm->read_buffers[i]);
1394 struct urb *urb;
David Kubicek61a87ad2005-11-01 18:51:34 +01001395
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001396 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1397 &rb->dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001398 if (!rb->base)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001399 goto alloc_fail6;
Johan Hovold088c64f2011-03-25 11:06:02 +01001400 rb->index = i;
1401 rb->instance = acm;
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001402
Johan Hovold088c64f2011-03-25 11:06:02 +01001403 urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001404 if (!urb)
Axel Linc2572b72010-05-31 08:04:47 +08001405 goto alloc_fail6;
Oliver Neukum17136d42015-01-28 16:56:24 +01001406
Johan Hovold088c64f2011-03-25 11:06:02 +01001407 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1408 urb->transfer_dma = rb->dma;
Ladislav Michld3053942016-11-18 19:10:19 +01001409 if (usb_endpoint_xfer_int(epread))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001410 usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001411 acm->readsize,
1412 acm_read_bulk_callback, rb,
1413 acm->bInterval);
Ladislav Michl74bccc92016-11-18 19:09:19 +01001414 else
1415 usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001416 acm->readsize,
1417 acm_read_bulk_callback, rb);
David Kubicek61a87ad2005-11-01 18:51:34 +01001418
Johan Hovold088c64f2011-03-25 11:06:02 +01001419 acm->read_urbs[i] = urb;
1420 __set_bit(i, &acm->read_urbs_free);
David Kubicek61a87ad2005-11-01 18:51:34 +01001421 }
Alan Cox6e47e062009-06-11 12:37:06 +01001422 for (i = 0; i < ACM_NW; i++) {
David Engrafe4cf3aa2008-03-20 10:01:34 +01001423 struct acm_wb *snd = &(acm->wb[i]);
1424
Alan Cox6e47e062009-06-11 12:37:06 +01001425 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001426 if (snd->urb == NULL)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001427 goto alloc_fail7;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001428
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001429 if (usb_endpoint_xfer_int(epwrite))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001430 usb_fill_int_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001431 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1432 else
Ladislav Michl74bccc92016-11-18 19:09:19 +01001433 usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001434 NULL, acm->writesize, acm_write_bulk, snd);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001435 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Lu Baoluffdb1e32016-01-06 15:10:04 +08001436 if (quirks & SEND_ZERO_PACKET)
1437 snd->urb->transfer_flags |= URB_ZERO_PACKET;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001438 snd->instance = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Alan Cox6e47e062009-06-11 12:37:06 +01001441 usb_set_intfdata(intf, acm);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001442
1443 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1444 if (i < 0)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001445 goto alloc_fail7;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001446
Oliver Neukumcb42b632016-07-14 15:41:34 +02001447 if (h.usb_cdc_country_functional_desc) { /* export the country data */
1448 struct usb_cdc_country_functional_desc * cfd =
1449 h.usb_cdc_country_functional_desc;
1450
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001451 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1452 if (!acm->country_codes)
1453 goto skip_countries;
1454 acm->country_code_size = cfd->bLength - 4;
Alan Cox6e47e062009-06-11 12:37:06 +01001455 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1456 cfd->bLength - 4);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001457 acm->country_rel_date = cfd->iCountryCodeRelDate;
1458
1459 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1460 if (i < 0) {
1461 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001462 acm->country_codes = NULL;
1463 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001464 goto skip_countries;
1465 }
1466
Alan Cox6e47e062009-06-11 12:37:06 +01001467 i = device_create_file(&intf->dev,
1468 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001469 if (i < 0) {
Axel Linc2572b72010-05-31 08:04:47 +08001470 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001471 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001472 acm->country_codes = NULL;
1473 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001474 goto skip_countries;
1475 }
1476 }
1477
1478skip_countries:
Alan Cox6e47e062009-06-11 12:37:06 +01001479 usb_fill_int_urb(acm->ctrlurb, usb_dev,
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001480 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1481 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1482 /* works around buggy devices */
Felipe Balbid102e782013-07-01 11:23:24 +03001483 epctrl->bInterval ? epctrl->bInterval : 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1485 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 acm->line.dwDTERate = cpu_to_le32(9600);
1490 acm->line.bDataBits = 8;
1491 acm_set_line(acm, &acm->line);
1492
1493 usb_driver_claim_interface(&acm_driver, data_interface, acm);
David Brownell672c4e12008-08-06 18:41:12 -07001494 usb_set_intfdata(data_interface, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001496 usb_get_intf(control_interface);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001497 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
Jiri Slaby734cc172012-08-07 21:47:47 +02001498 &control_interface->dev);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001499 if (IS_ERR(tty_dev)) {
1500 rv = PTR_ERR(tty_dev);
1501 goto alloc_fail8;
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001504 if (quirks & CLEAR_HALT_CONDITIONS) {
Ladislav Michl74bccc92016-11-18 19:09:19 +01001505 usb_clear_halt(usb_dev, acm->in);
1506 usb_clear_halt(usb_dev, acm->out);
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001507 }
1508
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001509 return 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001510alloc_fail8:
1511 if (acm->country_codes) {
1512 device_remove_file(&acm->control->dev,
1513 &dev_attr_wCountryCodes);
1514 device_remove_file(&acm->control->dev,
1515 &dev_attr_iCountryCodeRelDate);
Oliver Neukumd908f842014-11-20 14:54:35 +01001516 kfree(acm->country_codes);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001517 }
1518 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001519alloc_fail7:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001520 usb_set_intfdata(intf, NULL);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001521 for (i = 0; i < ACM_NW; i++)
1522 usb_free_urb(acm->wb[i].urb);
Axel Linc2572b72010-05-31 08:04:47 +08001523alloc_fail6:
Oliver Neukum86478942006-05-13 22:50:47 +02001524 for (i = 0; i < num_rx_buf; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +01001525 usb_free_urb(acm->read_urbs[i]);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001526 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 usb_free_urb(acm->ctrlurb);
1528alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001529 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530alloc_fail4:
Daniel Mack997ea582010-04-12 13:17:25 +02001531 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532alloc_fail2:
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001533 acm_release_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001534alloc_fail1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kfree(acm);
1536alloc_fail:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001537 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540static void acm_disconnect(struct usb_interface *intf)
1541{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001542 struct acm *acm = usb_get_intfdata(intf);
Alan Cox10077d42009-06-11 12:36:09 +01001543 struct tty_struct *tty;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001544
David Brownell672c4e12008-08-06 18:41:12 -07001545 /* sibling interface is already cleaning up */
1546 if (!acm)
Oliver Neukum86067eea2006-01-08 12:39:13 +01001547 return;
David Brownell672c4e12008-08-06 18:41:12 -07001548
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001549 mutex_lock(&acm->mutex);
1550 acm->disconnected = true;
Alan Cox6e47e062009-06-11 12:37:06 +01001551 if (acm->country_codes) {
Alan Stern74da5d62007-08-02 13:29:10 -04001552 device_remove_file(&acm->control->dev,
1553 &dev_attr_wCountryCodes);
1554 device_remove_file(&acm->control->dev,
1555 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001556 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001557 wake_up_all(&acm->wioctl);
Alan Stern74da5d62007-08-02 13:29:10 -04001558 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001559 usb_set_intfdata(acm->control, NULL);
1560 usb_set_intfdata(acm->data, NULL);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001561 mutex_unlock(&acm->mutex);
1562
1563 tty = tty_port_tty_get(&acm->port);
1564 if (tty) {
1565 tty_vhangup(tty);
1566 tty_kref_put(tty);
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Ladislav Michlba8c9312016-11-18 19:07:08 +01001569 acm_kill_urbs(acm);
1570 cancel_work_sync(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Johan Hovoldcb255052013-03-19 09:21:06 +01001572 tty_unregister_device(acm_tty_driver, acm->minor);
1573
Oliver Neukum884b6002005-04-21 21:28:02 +02001574 acm_write_buffers_free(acm);
Ladislav Michle4614602016-11-18 19:06:10 +01001575 usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001576 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001578 if (!acm->combined_interfaces)
1579 usb_driver_release_interface(&acm_driver, intf == acm->control ?
Oliver Neukum830f4022008-06-25 14:17:16 +02001580 acm->data : acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001582 tty_port_put(&acm->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Oliver Neukum35758582008-07-01 19:10:08 +02001585#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001586static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1587{
1588 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001589 int cnt;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001590
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001591 spin_lock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001592 if (PMSG_IS_AUTO(message)) {
1593 if (acm->transmitting) {
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001594 spin_unlock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001595 return -EBUSY;
1596 }
1597 }
Oliver Neukum11ea8592008-06-20 11:25:57 +02001598 cnt = acm->susp_count++;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001599 spin_unlock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001600
1601 if (cnt)
Oliver Neukum1365baf2007-10-12 17:24:28 +02001602 return 0;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001603
Ladislav Michlba8c9312016-11-18 19:07:08 +01001604 acm_kill_urbs(acm);
1605 cancel_work_sync(&acm->work);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001606
Oliver Neukum1365baf2007-10-12 17:24:28 +02001607 return 0;
1608}
1609
1610static int acm_resume(struct usb_interface *intf)
1611{
1612 struct acm *acm = usb_get_intfdata(intf);
Johan Hovold140cb812014-05-26 19:23:38 +02001613 struct urb *urb;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001614 int rv = 0;
1615
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001616 spin_lock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001617
Johan Hovolde144ed282014-05-26 19:23:37 +02001618 if (--acm->susp_count)
1619 goto out;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001620
Peter Hurleyd41861c2016-04-09 17:53:25 -07001621 if (tty_port_initialized(&acm->port)) {
Johan Hovolde144ed282014-05-26 19:23:37 +02001622 rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001623
Johan Hovold140cb812014-05-26 19:23:38 +02001624 for (;;) {
1625 urb = usb_get_from_anchor(&acm->delayed);
1626 if (!urb)
1627 break;
1628
1629 acm_start_wb(acm, urb->context);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001630 }
1631
1632 /*
1633 * delayed error checking because we must
1634 * do the write path at all cost
1635 */
Oliver Neukum1365baf2007-10-12 17:24:28 +02001636 if (rv < 0)
Johan Hovolde144ed282014-05-26 19:23:37 +02001637 goto out;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001638
Johan Hovolde144ed282014-05-26 19:23:37 +02001639 rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001640 }
Johan Hovolde144ed282014-05-26 19:23:37 +02001641out:
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001642 spin_unlock_irq(&acm->write_lock);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001643
Oliver Neukum1365baf2007-10-12 17:24:28 +02001644 return rv;
1645}
Oliver Neukum35758582008-07-01 19:10:08 +02001646
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001647static int acm_reset_resume(struct usb_interface *intf)
1648{
1649 struct acm *acm = usb_get_intfdata(intf);
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001650
Peter Hurleyd41861c2016-04-09 17:53:25 -07001651 if (tty_port_initialized(&acm->port))
Jiri Slabyaa27a092013-03-07 13:12:30 +01001652 tty_port_tty_hangup(&acm->port, false);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001653
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001654 return acm_resume(intf);
1655}
1656
Oliver Neukum35758582008-07-01 19:10:08 +02001657#endif /* CONFIG_PM */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001658
Ladislav Michl1aba5792016-11-18 19:11:26 +01001659static int acm_pre_reset(struct usb_interface *intf)
1660{
1661 struct acm *acm = usb_get_intfdata(intf);
1662
1663 clear_bit(EVENT_RX_STALL, &acm->flags);
1664
1665 return 0;
1666}
1667
Adrian Taylorc1479a92009-11-19 10:35:33 +00001668#define NOKIA_PCSUITE_ACM_INFO(x) \
1669 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1670 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1671 USB_CDC_ACM_PROTO_VENDOR)
1672
Toby Gray4035e452010-09-01 16:01:19 +01001673#define SAMSUNG_PCSUITE_ACM_INFO(x) \
1674 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1675 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1676 USB_CDC_ACM_PROTO_VENDOR)
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * USB driver structure.
1680 */
1681
Németh Márton6ef48522010-01-10 15:33:45 +01001682static const struct usb_device_id acm_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* quirky and broken devices */
Björn Gerhartb20b1612015-02-18 07:19:44 +01001684 { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
1685 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
David Cluytens3b59d162013-12-03 14:18:57 +01001686 { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
1687 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1689 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1690 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001691 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1692 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1693 },
Andrew Lunn0f9c7b42008-12-23 17:31:23 +01001694 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1695 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1696 },
Masahito Omote8753e652005-07-29 12:17:25 -07001697 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1698 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1699 },
Chris Malley91a9c922006-10-03 10:08:28 +01001700 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1701 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1702 },
Alan Cox7abcf202009-04-06 17:35:01 +01001703 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1704 .driver_info = SINGLE_RX_URB,
1705 },
Oliver Neukum86478942006-05-13 22:50:47 +02001706 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1707 .driver_info = SINGLE_RX_URB, /* firmware bug */
1708 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001709 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1710 .driver_info = SINGLE_RX_URB, /* firmware bug */
1711 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001712 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1713 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1714 },
Iain McFarlane6149ed52008-05-04 00:13:49 +01001715 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1716 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1717 },
Eric Sandeenc8fd2c32008-08-14 08:25:40 -05001718 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1719 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1720 },
Alan Coxc89c60e2009-01-11 19:53:10 +00001721 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1722 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1723 },
Xiao Kaijiancab98a02009-05-08 00:48:23 +08001724 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1725 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1726 },
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001727 { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
1728 .driver_info = QUIRK_CONTROL_LINE_STATE, },
Johan Hovoldcf84a692014-10-27 18:34:33 +01001729 { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
Nathaniel Quillin30121602016-12-05 06:53:00 -08001730 { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
Dmitriy Taychenachev155df652009-02-25 12:36:51 +08001731 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1732 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001733 /* Motorola H24 HSPA module: */
1734 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
Michael Ulbricht895d2402014-03-25 10:34:18 +01001735 { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
1736 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1737 },
1738 { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
1739 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1740 },
1741 { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
1742 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1743 },
1744 { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
1745 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1746 },
1747 { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
1748 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1749 },
1750 { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
1751 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1752 },
1753 { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
1754 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1755 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001756
Adam Richterc332b4e2009-02-18 16:17:15 -08001757 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1758 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1759 data interface instead of
1760 communications interface.
1761 Maybe we should define a new
1762 quirk for this. */
1763 },
Jean-Christian de Rivaze7d491a2012-10-10 12:49:02 +00001764 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1765 .driver_info = NO_UNION_NORMAL,
1766 },
Denis N Ladin036915a2012-12-26 18:29:44 +05001767 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1768 .driver_info = NO_UNION_NORMAL,
1769 },
Kir Kolyshkin1f17c502009-05-28 20:33:58 +04001770 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1771 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1772 },
Russ Nelsonc3baa192010-04-21 23:07:03 -04001773 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1774 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1775 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001776
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001777 { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
1778 .driver_info = CLEAR_HALT_CONDITIONS,
1779 },
1780
Adrian Taylorc1479a92009-11-19 10:35:33 +00001781 /* Nokia S60 phones expose two ACM channels. The first is
1782 * a modem and is picked up by the standard AT-command
1783 * information below. The second is 'vendor-specific' but
1784 * is treated as a serial device at the S60 end, so we want
1785 * to expose it on Linux too. */
1786 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1787 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1788 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1789 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1790 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1791 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1792 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1793 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1794 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1795 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1796 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1797 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1798 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1799 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1800 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1801 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1802 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1803 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1804 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1805 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1806 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1807 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1808 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1809 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1810 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1811 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1812 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1813 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1814 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1815 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1816 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1817 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1818 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1819 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1820 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1821 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1822 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1823 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1824 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1825 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1826 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1827 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1828 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
Przemo Firszt83a4eae2010-06-28 21:29:34 +01001829 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
Toby Gray4035e452010-09-01 16:01:19 +01001830 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1831 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1832 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1833 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1834 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1835 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1836 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1837 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1838 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1839 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
Arvid Ephraim Picciani721d92f2011-01-25 15:58:40 +01001840 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
Toby Gray4061fde2011-06-06 14:52:48 +01001841 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1842 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
Toby Gray4035e452010-09-01 16:01:19 +01001843 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001844
Denis Pershin65e52f42011-09-04 17:37:21 +07001845 /* Support for Owen devices */
1846 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1847
Adrian Taylorc1479a92009-11-19 10:35:33 +00001848 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1849
Erik Slagterfd5054c2011-05-11 12:06:55 +02001850 /* Support for Droids MuIn LCD */
1851 { USB_DEVICE(0x04d8, 0x000b),
1852 .driver_info = NO_DATA_INTERFACE,
1853 },
1854
Dmitry Torokhov16142652013-02-02 11:02:14 -08001855#if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1856 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1857 .driver_info = IGNORE_DEVICE,
1858 },
1859 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1860 .driver_info = IGNORE_DEVICE,
1861 },
1862#endif
1863
Oliver Neukume912e682016-01-18 15:45:18 +01001864 /*Samsung phone in firmware update mode */
1865 { USB_DEVICE(0x04e8, 0x685d),
1866 .driver_info = IGNORE_DEVICE,
1867 },
1868
Jonas Jonssonf33a7f72015-11-22 11:47:17 +01001869 /* Exclude Infineon Flash Loader utility */
1870 { USB_DEVICE(0x058b, 0x0041),
1871 .driver_info = IGNORE_DEVICE,
1872 },
1873
Philippe Corbes5b239f02010-08-31 19:31:32 +02001874 /* control interfaces without any protocol set */
1875 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1876 USB_CDC_PROTO_NONE) },
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* control interfaces with various AT-command sets */
1879 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1880 USB_CDC_ACM_PROTO_AT_V25TER) },
1881 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1882 USB_CDC_ACM_PROTO_AT_PCCA101) },
1883 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1884 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1885 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1886 USB_CDC_ACM_PROTO_AT_GSM) },
1887 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
Alan Cox6e47e062009-06-11 12:37:06 +01001888 USB_CDC_ACM_PROTO_AT_3G) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1890 USB_CDC_ACM_PROTO_AT_CDMA) },
1891
Lu Baoluffdb1e32016-01-06 15:10:04 +08001892 { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
1893 .driver_info = SEND_ZERO_PACKET,
1894 },
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 { }
1897};
1898
Alan Cox6e47e062009-06-11 12:37:06 +01001899MODULE_DEVICE_TABLE(usb, acm_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .name = "cdc_acm",
1903 .probe = acm_probe,
1904 .disconnect = acm_disconnect,
Oliver Neukum35758582008-07-01 19:10:08 +02001905#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001906 .suspend = acm_suspend,
1907 .resume = acm_resume,
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001908 .reset_resume = acm_reset_resume,
Oliver Neukum35758582008-07-01 19:10:08 +02001909#endif
Ladislav Michl1aba5792016-11-18 19:11:26 +01001910 .pre_reset = acm_pre_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 .id_table = acm_ids,
Oliver Neukum35758582008-07-01 19:10:08 +02001912#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001913 .supports_autosuspend = 1,
Oliver Neukum35758582008-07-01 19:10:08 +02001914#endif
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001915 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916};
1917
1918/*
1919 * TTY driver structures.
1920 */
1921
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001922static const struct tty_operations acm_ops = {
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001923 .install = acm_tty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 .open = acm_tty_open,
1925 .close = acm_tty_close,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001926 .cleanup = acm_tty_cleanup,
Alan Cox10077d42009-06-11 12:36:09 +01001927 .hangup = acm_tty_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 .write = acm_tty_write,
Oliver Neukuma81cf972016-02-10 10:39:49 +01001929 .put_char = acm_tty_put_char,
1930 .flush_chars = acm_tty_flush_chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 .write_room = acm_tty_write_room,
1932 .ioctl = acm_tty_ioctl,
1933 .throttle = acm_tty_throttle,
1934 .unthrottle = acm_tty_unthrottle,
1935 .chars_in_buffer = acm_tty_chars_in_buffer,
1936 .break_ctl = acm_tty_break_ctl,
1937 .set_termios = acm_tty_set_termios,
1938 .tiocmget = acm_tty_tiocmget,
1939 .tiocmset = acm_tty_tiocmset,
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001940 .get_icount = acm_tty_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941};
1942
1943/*
1944 * Init / exit.
1945 */
1946
1947static int __init acm_init(void)
1948{
1949 int retval;
1950 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1951 if (!acm_tty_driver)
1952 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 acm_tty_driver->driver_name = "acm",
1954 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 acm_tty_driver->major = ACM_TTY_MAJOR,
1956 acm_tty_driver->minor_start = 0,
1957 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1958 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001959 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 acm_tty_driver->init_termios = tty_std_termios;
Alan Cox6e47e062009-06-11 12:37:06 +01001961 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1962 HUPCL | CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 tty_set_operations(acm_tty_driver, &acm_ops);
1964
1965 retval = tty_register_driver(acm_tty_driver);
1966 if (retval) {
1967 put_tty_driver(acm_tty_driver);
1968 return retval;
1969 }
1970
1971 retval = usb_register(&acm_driver);
1972 if (retval) {
1973 tty_unregister_driver(acm_tty_driver);
1974 put_tty_driver(acm_tty_driver);
1975 return retval;
1976 }
1977
Johan Hovolda2c7b932011-03-22 11:12:18 +01001978 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 return 0;
1981}
1982
1983static void __exit acm_exit(void)
1984{
1985 usb_deregister(&acm_driver);
1986 tty_unregister_driver(acm_tty_driver);
1987 put_tty_driver(acm_tty_driver);
Johannes Thumshirn91b72562015-07-08 17:25:42 +02001988 idr_destroy(&acm_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991module_init(acm_init);
1992module_exit(acm_exit);
1993
Alan Cox6e47e062009-06-11 12:37:06 +01001994MODULE_AUTHOR(DRIVER_AUTHOR);
1995MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996MODULE_LICENSE("GPL");
Scott James Remnante766aeb2009-04-06 17:33:18 +01001997MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);