blob: 00d55ba8983f9f5f1762850532842c83d3168660 [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:
Alan Cox6e47e062009-06-11 12:37:06 +0100326 newctrl = get_unaligned_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jiri Slabyaa27a092013-03-07 13:12:30 +0100328 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
Ladislav Michl90744af82016-11-18 19:01:22 +0100329 dev_dbg(&acm->control->dev,
330 "%s - calling hangup\n", __func__);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100331 tty_port_tty_hangup(&acm->port, false);
Alan Cox6e47e062009-06-11 12:37:06 +0100332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100334 difference = acm->ctrlin ^ newctrl;
335 spin_lock(&acm->read_lock);
Alan Cox6e47e062009-06-11 12:37:06 +0100336 acm->ctrlin = newctrl;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100337 acm->oldcount = acm->iocount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100339 if (difference & ACM_CTRL_DSR)
340 acm->iocount.dsr++;
341 if (difference & ACM_CTRL_BRK)
342 acm->iocount.brk++;
343 if (difference & ACM_CTRL_RI)
344 acm->iocount.rng++;
345 if (difference & ACM_CTRL_DCD)
346 acm->iocount.dcd++;
347 if (difference & ACM_CTRL_FRAMING)
348 acm->iocount.frame++;
349 if (difference & ACM_CTRL_PARITY)
350 acm->iocount.parity++;
351 if (difference & ACM_CTRL_OVERRUN)
352 acm->iocount.overrun++;
353 spin_unlock(&acm->read_lock);
354
355 if (difference)
356 wake_up_all(&acm->wioctl);
357
358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Alan Cox6e47e062009-06-11 12:37:06 +0100360 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100361 dev_dbg(&acm->control->dev,
362 "%s - unknown notification %d received: index %d "
363 "len %d data0 %d data1 %d\n",
364 __func__,
Alan Cox6e47e062009-06-11 12:37:06 +0100365 dr->bNotificationType, dr->wIndex,
366 dr->wLength, data[0], data[1]);
367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369exit:
Alan Cox6e47e062009-06-11 12:37:06 +0100370 retval = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum6c8074e2015-03-20 11:25:17 +0100371 if (retval && retval != -EPERM)
Ladislav Michl90744af82016-11-18 19:01:22 +0100372 dev_err(&acm->control->dev,
373 "%s - usb_submit_urb failed: %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Johan Hovold088c64f2011-03-25 11:06:02 +0100376static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Johan Hovold088c64f2011-03-25 11:06:02 +0100378 int res;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700379
Johan Hovold088c64f2011-03-25 11:06:02 +0100380 if (!test_and_clear_bit(index, &acm->read_urbs_free))
381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Johan Hovold088c64f2011-03-25 11:06:02 +0100383 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
384 if (res) {
385 if (res != -EPERM) {
386 dev_err(&acm->data->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100387 "urb %d failed submission with %d\n",
388 index, res);
Johan Hovold088c64f2011-03-25 11:06:02 +0100389 }
390 set_bit(index, &acm->read_urbs_free);
391 return res;
Oliver Neukum46e75072016-09-13 16:44:36 +0200392 } else {
393 dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
Johan Hovold088c64f2011-03-25 11:06:02 +0100394 }
395
396 return 0;
397}
398
399static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
400{
401 int res;
402 int i;
403
404 for (i = 0; i < acm->rx_buflimit; ++i) {
405 res = acm_submit_read_urb(acm, i, mem_flags);
406 if (res)
407 return res;
408 }
409
410 return 0;
411}
412
413static void acm_process_read_urb(struct acm *acm, struct urb *urb)
414{
Johan Hovold088c64f2011-03-25 11:06:02 +0100415 if (!urb->actual_length)
416 return;
417
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100418 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
419 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100420 tty_flip_buffer_push(&acm->port);
Johan Hovold088c64f2011-03-25 11:06:02 +0100421}
422
423static void acm_read_bulk_callback(struct urb *urb)
424{
425 struct acm_rb *rb = urb->context;
426 struct acm *acm = rb->instance;
427 unsigned long flags;
Oliver Neukum36e59e02015-03-20 09:24:24 +0100428 int status = urb->status;
Johan Hovold088c64f2011-03-25 11:06:02 +0100429
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200430 dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
Ladislav Michl90744af82016-11-18 19:01:22 +0100431 rb->index, urb->actual_length, status);
Johan Hovold088c64f2011-03-25 11:06:02 +0100432
Ladislav Michl1aba5792016-11-18 19:11:26 +0100433 set_bit(rb->index, &acm->read_urbs_free);
434
Johan Hovold088c64f2011-03-25 11:06:02 +0100435 if (!acm->dev) {
436 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Ladislav Michl1aba5792016-11-18 19:11:26 +0100440 switch (status) {
441 case 0:
442 usb_mark_last_busy(acm->dev);
443 acm_process_read_urb(acm, urb);
444 break;
445 case -EPIPE:
446 set_bit(EVENT_RX_STALL, &acm->flags);
447 schedule_work(&acm->work);
448 return;
449 case -ENOENT:
450 case -ECONNRESET:
451 case -ESHUTDOWN:
452 dev_dbg(&acm->data->dev,
453 "%s - urb shutting down with status: %d\n",
454 __func__, status);
455 return;
456 default:
457 dev_dbg(&acm->data->dev,
458 "%s - nonzero urb status received: %d\n",
459 __func__, status);
460 break;
Johan Hovold088c64f2011-03-25 11:06:02 +0100461 }
Johan Hovold4a8ee502014-05-26 19:23:49 +0200462
Oliver Neukum36e59e02015-03-20 09:24:24 +0100463 /*
464 * Unthrottle may run on another CPU which needs to see events
465 * in the same order. Submission has an implict barrier
466 */
467 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Johan Hovold088c64f2011-03-25 11:06:02 +0100469 /* throttle device if requested by tty */
470 spin_lock_irqsave(&acm->read_lock, flags);
471 acm->throttled = acm->throttle_req;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +0200472 if (!acm->throttled) {
Johan Hovold088c64f2011-03-25 11:06:02 +0100473 spin_unlock_irqrestore(&acm->read_lock, flags);
474 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
Oliver Neukum86478942006-05-13 22:50:47 +0200475 } else {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200476 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100481static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Ming Leicdc97792008-02-24 18:41:47 +0800483 struct acm_wb *wb = urb->context;
David Brownelle5fbab52008-08-06 18:46:10 -0700484 struct acm *acm = wb->instance;
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800485 unsigned long flags;
Oliver Neukum4132cd02015-03-20 11:41:06 +0100486 int status = urb->status;
Oliver Neukum884b6002005-04-21 21:28:02 +0200487
Oliver Neukum4132cd02015-03-20 11:41:06 +0100488 if (status || (urb->actual_length != urb->transfer_buffer_length))
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200489 dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
David Brownelle5fbab52008-08-06 18:46:10 -0700490 urb->actual_length,
491 urb->transfer_buffer_length,
Oliver Neukum4132cd02015-03-20 11:41:06 +0100492 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800494 spin_lock_irqsave(&acm->write_lock, flags);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100495 acm_write_done(acm, wb);
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800496 spin_unlock_irqrestore(&acm->write_lock, flags);
Ladislav Michl1aba5792016-11-18 19:11:26 +0100497 set_bit(EVENT_TTY_WAKEUP, &acm->flags);
Havard Skinnemoen99823f42011-12-09 16:51:54 -0800498 schedule_work(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
David Howellsc4028952006-11-22 14:57:56 +0000501static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Ladislav Michl1aba5792016-11-18 19:11:26 +0100503 int i;
David Howellsc4028952006-11-22 14:57:56 +0000504 struct acm *acm = container_of(work, struct acm, work);
David Brownelle5fbab52008-08-06 18:46:10 -0700505
Ladislav Michl1aba5792016-11-18 19:11:26 +0100506 if (test_bit(EVENT_RX_STALL, &acm->flags)) {
507 if (!(usb_autopm_get_interface(acm->data))) {
508 for (i = 0; i < acm->rx_buflimit; i++)
509 usb_kill_urb(acm->read_urbs[i]);
510 usb_clear_halt(acm->dev, acm->in);
511 acm_submit_read_urbs(acm, GFP_KERNEL);
512 usb_autopm_put_interface(acm->data);
513 }
514 clear_bit(EVENT_RX_STALL, &acm->flags);
515 }
516
517 if (test_bit(EVENT_TTY_WAKEUP, &acm->flags)) {
518 tty_port_tty_wakeup(&acm->port);
519 clear_bit(EVENT_TTY_WAKEUP, &acm->flags);
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
524 * TTY handlers
525 */
526
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800527static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct acm *acm;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800530 int retval;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100531
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200532 acm = acm_get_by_minor(tty->index);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800533 if (!acm)
534 return -ENODEV;
535
Jiri Slabyf8a8c102012-03-05 14:51:48 +0100536 retval = tty_standard_install(driver, tty);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800537 if (retval)
538 goto error_init_termios;
539
540 tty->driver_data = acm;
541
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800542 return 0;
543
544error_init_termios:
545 tty_port_put(&acm->port);
546 return retval;
547}
548
549static int acm_tty_open(struct tty_struct *tty, struct file *filp)
550{
551 struct acm *acm = tty->driver_data;
552
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800553 return tty_port_open(&acm->port, tty, filp);
554}
555
Johan Hovold0943d8e2014-05-26 19:23:51 +0200556static void acm_port_dtr_rts(struct tty_port *port, int raise)
557{
558 struct acm *acm = container_of(port, struct acm, port);
559 int val;
560 int res;
561
562 if (raise)
563 val = ACM_CTRL_DTR | ACM_CTRL_RTS;
564 else
565 val = 0;
566
567 /* FIXME: add missing ctrlout locking throughout driver */
568 acm->ctrlout = val;
569
570 res = acm_set_control(acm, val);
571 if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
572 dev_err(&acm->control->dev, "failed to set dtr/rts\n");
573}
574
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800575static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
576{
577 struct acm *acm = container_of(port, struct acm, port);
578 int retval = -ENODEV;
Johan Hovolde4c36072014-05-26 19:23:44 +0200579 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Oliver Neukum1365baf72007-10-12 17:24:28 +0200581 mutex_lock(&acm->mutex);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800582 if (acm->disconnected)
583 goto disconnected;
584
585 retval = usb_autopm_get_interface(acm->control);
586 if (retval)
587 goto error_get_interface;
588
589 /*
590 * FIXME: Why do we need this? Allocating 64K of physically contiguous
591 * memory is really nasty...
592 */
593 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
594 acm->control->needs_remote_wakeup = 1;
Oliver Neukum1365baf72007-10-12 17:24:28 +0200595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 acm->ctrlurb->dev = acm->dev;
Johan Hovold8727bf62014-05-26 19:23:43 +0200597 retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
598 if (retval) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100599 dev_err(&acm->control->dev,
600 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800601 goto error_submit_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Jim Paris24cb4502014-10-30 11:04:47 -0400604 acm_tty_set_termios(tty, NULL);
605
Otto Meta6c4707f2012-06-06 18:46:21 +0200606 /*
607 * Unthrottle device in case the TTY was closed while throttled.
608 */
609 spin_lock_irq(&acm->read_lock);
610 acm->throttled = 0;
611 acm->throttle_req = 0;
612 spin_unlock_irq(&acm->read_lock);
613
Johan Hovold8727bf62014-05-26 19:23:43 +0200614 retval = acm_submit_read_urbs(acm, GFP_KERNEL);
615 if (retval)
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800616 goto error_submit_read_urbs;
Oliver Neukum2b626dc2010-02-03 17:10:22 +0100617
Johan Hovold703df322014-05-26 19:23:42 +0200618 usb_autopm_put_interface(acm->control);
619
Oliver Neukum1365baf72007-10-12 17:24:28 +0200620 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800622 return 0;
623
624error_submit_read_urbs:
Johan Hovolde4c36072014-05-26 19:23:44 +0200625 for (i = 0; i < acm->rx_buflimit; i++)
626 usb_kill_urb(acm->read_urbs[i]);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800627 usb_kill_urb(acm->ctrlurb);
628error_submit_urb:
Johan Hovold703df322014-05-26 19:23:42 +0200629 usb_autopm_put_interface(acm->control);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800630error_get_interface:
631disconnected:
632 mutex_unlock(&acm->mutex);
Johan Hovold8727bf62014-05-26 19:23:43 +0200633
634 return usb_translate_errors(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800637static void acm_port_destruct(struct tty_port *port)
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700638{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800639 struct acm *acm = container_of(port, struct acm, port);
640
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800641 acm_release_minor(acm);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700642 usb_put_intf(acm->control);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100643 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700644 kfree(acm);
645}
646
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800647static void acm_port_shutdown(struct tty_port *port)
Alan Cox10077d42009-06-11 12:36:09 +0100648{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800649 struct acm *acm = container_of(port, struct acm, port);
Johan Hovold140cb812014-05-26 19:23:38 +0200650 struct urb *urb;
651 struct acm_wb *wb;
Johan Hovolddab54c92011-03-22 11:12:21 +0100652
Johan Hovoldb1d42ef2014-05-26 19:23:48 +0200653 /*
654 * Need to grab write_lock to prevent race with resume, but no need to
655 * hold it due to the tty-port initialised flag.
656 */
657 spin_lock_irq(&acm->write_lock);
658 spin_unlock_irq(&acm->write_lock);
659
660 usb_autopm_get_interface_no_resume(acm->control);
661 acm->control->needs_remote_wakeup = 0;
662 usb_autopm_put_interface(acm->control);
663
Johan Hovold89e54e42014-05-26 19:23:47 +0200664 for (;;) {
665 urb = usb_get_from_anchor(&acm->delayed);
666 if (!urb)
667 break;
668 wb = urb->context;
669 wb->use = 0;
670 usb_autopm_put_interface_async(acm->control);
Alan Cox10077d42009-06-11 12:36:09 +0100671 }
Johan Hovold89e54e42014-05-26 19:23:47 +0200672
Ladislav Michlba8c9312016-11-18 19:07:08 +0100673 acm_kill_urbs(acm);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800674}
675
676static void acm_tty_cleanup(struct tty_struct *tty)
677{
678 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200679
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800680 tty_port_put(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100681}
682
683static void acm_tty_hangup(struct tty_struct *tty)
684{
685 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200686
Alan Cox10077d42009-06-11 12:36:09 +0100687 tty_port_hangup(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static void acm_tty_close(struct tty_struct *tty, struct file *filp)
691{
692 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200693
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800694 tty_port_close(&acm->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Alan Cox6e47e062009-06-11 12:37:06 +0100697static int acm_tty_write(struct tty_struct *tty,
698 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct acm *acm = tty->driver_data;
701 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200702 unsigned long flags;
703 int wbn;
704 struct acm_wb *wb;
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!count)
707 return 0;
708
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200709 dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100710
Oliver Neukum884b6002005-04-21 21:28:02 +0200711 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100712 wbn = acm_wb_alloc(acm);
713 if (wbn < 0) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200714 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200717 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700719 if (!acm->dev) {
720 wb->use = 0;
721 spin_unlock_irqrestore(&acm->write_lock, flags);
722 return -ENODEV;
723 }
724
Oliver Neukum884b6002005-04-21 21:28:02 +0200725 count = (count > acm->writesize) ? acm->writesize : count;
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200726 dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
Oliver Neukum884b6002005-04-21 21:28:02 +0200727 memcpy(wb->buf, buf, count);
728 wb->len = count;
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700729
Johan Hovold183a4502014-05-26 19:23:41 +0200730 stat = usb_autopm_get_interface_async(acm->control);
731 if (stat) {
732 wb->use = 0;
733 spin_unlock_irqrestore(&acm->write_lock, flags);
734 return stat;
735 }
736
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700737 if (acm->susp_count) {
Oliver Neukuma81cf972016-02-10 10:39:49 +0100738 if (acm->putbuffer) {
739 /* now to preserve order */
740 usb_anchor_urb(acm->putbuffer->urb, &acm->delayed);
741 acm->putbuffer = NULL;
742 }
Johan Hovold140cb812014-05-26 19:23:38 +0200743 usb_anchor_urb(wb->urb, &acm->delayed);
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700744 spin_unlock_irqrestore(&acm->write_lock, flags);
Johan Hovold140cb812014-05-26 19:23:38 +0200745 return count;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100746 } else {
747 if (acm->putbuffer) {
748 /* at this point there is no good way to handle errors */
749 acm_start_wb(acm, acm->putbuffer);
750 acm->putbuffer = NULL;
751 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700752 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700753
754 stat = acm_start_wb(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200755 spin_unlock_irqrestore(&acm->write_lock, flags);
756
Alan Cox6e47e062009-06-11 12:37:06 +0100757 if (stat < 0)
Oliver Neukum884b6002005-04-21 21:28:02 +0200758 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return count;
760}
761
Oliver Neukuma81cf972016-02-10 10:39:49 +0100762static void acm_tty_flush_chars(struct tty_struct *tty)
763{
764 struct acm *acm = tty->driver_data;
765 struct acm_wb *cur = acm->putbuffer;
766 int err;
767 unsigned long flags;
768
Oliver Neukum2a147592016-04-04 14:30:53 +0200769 if (!cur) /* nothing to do */
770 return;
771
Oliver Neukuma81cf972016-02-10 10:39:49 +0100772 acm->putbuffer = NULL;
773 err = usb_autopm_get_interface_async(acm->control);
774 spin_lock_irqsave(&acm->write_lock, flags);
775 if (err < 0) {
776 cur->use = 0;
Oliver Neukum2a147592016-04-04 14:30:53 +0200777 acm->putbuffer = cur;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100778 goto out;
779 }
780
781 if (acm->susp_count)
782 usb_anchor_urb(cur->urb, &acm->delayed);
783 else
784 acm_start_wb(acm, cur);
785out:
786 spin_unlock_irqrestore(&acm->write_lock, flags);
787 return;
788}
789
790static int acm_tty_put_char(struct tty_struct *tty, unsigned char ch)
791{
792 struct acm *acm = tty->driver_data;
793 struct acm_wb *cur;
794 int wbn;
795 unsigned long flags;
796
797overflow:
798 cur = acm->putbuffer;
799 if (!cur) {
800 spin_lock_irqsave(&acm->write_lock, flags);
801 wbn = acm_wb_alloc(acm);
802 if (wbn >= 0) {
803 cur = &acm->wb[wbn];
804 acm->putbuffer = cur;
805 }
806 spin_unlock_irqrestore(&acm->write_lock, flags);
807 if (!cur)
808 return 0;
809 }
810
811 if (cur->len == acm->writesize) {
812 acm_tty_flush_chars(tty);
813 goto overflow;
814 }
815
816 cur->buf[cur->len++] = ch;
817 return 1;
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static int acm_tty_write_room(struct tty_struct *tty)
821{
822 struct acm *acm = tty->driver_data;
Oliver Neukum884b6002005-04-21 21:28:02 +0200823 /*
824 * Do not let the line discipline to know that we have a reserve,
825 * or it might get too enthusiastic.
826 */
David Brownell934da462008-08-06 18:44:12 -0700827 return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830static int acm_tty_chars_in_buffer(struct tty_struct *tty)
831{
832 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800833 /*
834 * if the device was unplugged then any remaining characters fell out
835 * of the connector ;)
836 */
837 if (acm->disconnected)
Alan Cox23198fd2009-07-20 16:05:27 +0100838 return 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200839 /*
840 * This is inaccurate (overcounts), but it works.
841 */
Oliver Neukum86478942006-05-13 22:50:47 +0200842 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845static void acm_tty_throttle(struct tty_struct *tty)
846{
847 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100848
Johan Hovold088c64f2011-03-25 11:06:02 +0100849 spin_lock_irq(&acm->read_lock);
850 acm->throttle_req = 1;
851 spin_unlock_irq(&acm->read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854static void acm_tty_unthrottle(struct tty_struct *tty)
855{
856 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100857 unsigned int was_throttled;
858
Johan Hovold088c64f2011-03-25 11:06:02 +0100859 spin_lock_irq(&acm->read_lock);
860 was_throttled = acm->throttled;
861 acm->throttled = 0;
862 acm->throttle_req = 0;
863 spin_unlock_irq(&acm->read_lock);
864
865 if (was_throttled)
866 acm_submit_read_urbs(acm, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Alan Cox9e989662008-07-22 11:18:03 +0100869static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct acm *acm = tty->driver_data;
Alan Cox9e989662008-07-22 11:18:03 +0100872 int retval;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800873
Alan Cox9e989662008-07-22 11:18:03 +0100874 retval = acm_send_break(acm, state ? 0xffff : 0);
875 if (retval < 0)
Ladislav Michl90744af82016-11-18 19:01:22 +0100876 dev_dbg(&acm->control->dev,
877 "%s - send break failed\n", __func__);
Alan Cox9e989662008-07-22 11:18:03 +0100878 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Alan Cox60b33c12011-02-14 16:26:14 +0000881static int acm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct acm *acm = tty->driver_data;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
886 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
887 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
888 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
889 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
890 TIOCM_CTS;
891}
892
Alan Cox20b9d172011-02-14 16:26:50 +0000893static int acm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 unsigned int set, unsigned int clear)
895{
896 struct acm *acm = tty->driver_data;
897 unsigned int newctrl;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 newctrl = acm->ctrlout;
Alan Cox6e47e062009-06-11 12:37:06 +0100900 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
901 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
902 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
903 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 newctrl = (newctrl & ~clear) | set;
906
907 if (acm->ctrlout == newctrl)
908 return 0;
909 return acm_set_control(acm, acm->ctrlout = newctrl);
910}
911
Oliver Neukum18c75722012-02-17 17:21:24 -0500912static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
913{
914 struct serial_struct tmp;
915
Oliver Neukum18c75722012-02-17 17:21:24 -0500916 memset(&tmp, 0, sizeof(tmp));
Oliver Neukum18c75722012-02-17 17:21:24 -0500917 tmp.xmit_fifo_size = acm->writesize;
918 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600919 tmp.close_delay = acm->port.close_delay / 10;
920 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
921 ASYNC_CLOSING_WAIT_NONE :
922 acm->port.closing_wait / 10;
Oliver Neukum18c75722012-02-17 17:21:24 -0500923
924 if (copy_to_user(info, &tmp, sizeof(tmp)))
925 return -EFAULT;
926 else
927 return 0;
928}
929
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600930static int set_serial_info(struct acm *acm,
931 struct serial_struct __user *newinfo)
932{
933 struct serial_struct new_serial;
934 unsigned int closing_wait, close_delay;
935 int retval = 0;
936
937 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
938 return -EFAULT;
939
940 close_delay = new_serial.close_delay * 10;
941 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
942 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
943
944 mutex_lock(&acm->port.mutex);
945
946 if (!capable(CAP_SYS_ADMIN)) {
947 if ((close_delay != acm->port.close_delay) ||
948 (closing_wait != acm->port.closing_wait))
949 retval = -EPERM;
950 else
951 retval = -EOPNOTSUPP;
952 } else {
953 acm->port.close_delay = close_delay;
954 acm->port.closing_wait = closing_wait;
955 }
956
957 mutex_unlock(&acm->port.mutex);
958 return retval;
959}
960
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100961static int wait_serial_change(struct acm *acm, unsigned long arg)
962{
963 int rv = 0;
964 DECLARE_WAITQUEUE(wait, current);
965 struct async_icount old, new;
966
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100967 do {
968 spin_lock_irq(&acm->read_lock);
969 old = acm->oldcount;
970 new = acm->iocount;
971 acm->oldcount = new;
972 spin_unlock_irq(&acm->read_lock);
973
974 if ((arg & TIOCM_DSR) &&
975 old.dsr != new.dsr)
976 break;
977 if ((arg & TIOCM_CD) &&
978 old.dcd != new.dcd)
979 break;
980 if ((arg & TIOCM_RI) &&
981 old.rng != new.rng)
982 break;
983
984 add_wait_queue(&acm->wioctl, &wait);
985 set_current_state(TASK_INTERRUPTIBLE);
986 schedule();
987 remove_wait_queue(&acm->wioctl, &wait);
988 if (acm->disconnected) {
989 if (arg & TIOCM_CD)
990 break;
991 else
992 rv = -ENODEV;
993 } else {
994 if (signal_pending(current))
995 rv = -ERESTARTSYS;
996 }
997 } while (!rv);
998
999
1000
1001 return rv;
1002}
1003
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001004static int acm_tty_get_icount(struct tty_struct *tty,
1005 struct serial_icounter_struct *icount)
Oliver Neukum797ef132013-11-20 11:35:35 +01001006{
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001007 struct acm *acm = tty->driver_data;
Oliver Neukum797ef132013-11-20 11:35:35 +01001008
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001009 icount->dsr = acm->iocount.dsr;
1010 icount->rng = acm->iocount.rng;
1011 icount->dcd = acm->iocount.dcd;
1012 icount->frame = acm->iocount.frame;
1013 icount->overrun = acm->iocount.overrun;
1014 icount->parity = acm->iocount.parity;
1015 icount->brk = acm->iocount.brk;
Oliver Neukum797ef132013-11-20 11:35:35 +01001016
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001017 return 0;
Oliver Neukum797ef132013-11-20 11:35:35 +01001018}
1019
Alan Cox6caa76b2011-02-14 16:27:22 +00001020static int acm_tty_ioctl(struct tty_struct *tty,
Alan Cox6e47e062009-06-11 12:37:06 +01001021 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Oliver Neukum18c75722012-02-17 17:21:24 -05001023 struct acm *acm = tty->driver_data;
1024 int rv = -ENOIOCTLCMD;
1025
1026 switch (cmd) {
1027 case TIOCGSERIAL: /* gets serial port data */
1028 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
1029 break;
Dan Williamsba2d8ce2012-11-08 12:47:41 -06001030 case TIOCSSERIAL:
1031 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
1032 break;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001033 case TIOCMIWAIT:
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001034 rv = usb_autopm_get_interface(acm->control);
1035 if (rv < 0) {
1036 rv = -EIO;
1037 break;
1038 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001039 rv = wait_serial_change(acm, arg);
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001040 usb_autopm_put_interface(acm->control);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001041 break;
Oliver Neukum18c75722012-02-17 17:21:24 -05001042 }
1043
1044 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Alan Cox6e47e062009-06-11 12:37:06 +01001047static void acm_tty_set_termios(struct tty_struct *tty,
1048 struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct acm *acm = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01001051 struct ktermios *termios = &tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct usb_cdc_line_coding newline;
1053 int newctrl = acm->ctrlout;
1054
Alan Cox9b80fee2009-09-19 13:13:23 -07001055 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
1057 newline.bParityType = termios->c_cflag & PARENB ?
Alan Cox6e47e062009-06-11 12:37:06 +01001058 (termios->c_cflag & PARODD ? 1 : 2) +
1059 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
Nicolas Boullis301a29d2012-10-16 00:06:23 +02001060 switch (termios->c_cflag & CSIZE) {
1061 case CS5:
1062 newline.bDataBits = 5;
1063 break;
1064 case CS6:
1065 newline.bDataBits = 6;
1066 break;
1067 case CS7:
1068 newline.bDataBits = 7;
1069 break;
1070 case CS8:
1071 default:
1072 newline.bDataBits = 8;
1073 break;
1074 }
Alan Cox6e47e062009-06-11 12:37:06 +01001075 /* FIXME: Needs to clear unsupported bits in the termios */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
1077
Johan Hovold4473d052014-11-05 18:41:59 +01001078 if (C_BAUD(tty) == B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 newline.dwDTERate = acm->line.dwDTERate;
1080 newctrl &= ~ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001081 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001082 newctrl |= ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (newctrl != acm->ctrlout)
1086 acm_set_control(acm, acm->ctrlout = newctrl);
1087
1088 if (memcmp(&acm->line, &newline, sizeof newline)) {
1089 memcpy(&acm->line, &newline, sizeof newline);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001090 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
1091 __func__,
1092 le32_to_cpu(newline.dwDTERate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 newline.bCharFormat, newline.bParityType,
1094 newline.bDataBits);
1095 acm_set_line(acm, &acm->line);
1096 }
1097}
1098
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001099static const struct tty_port_operations acm_port_ops = {
Johan Hovold0943d8e2014-05-26 19:23:51 +02001100 .dtr_rts = acm_port_dtr_rts,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001101 .shutdown = acm_port_shutdown,
1102 .activate = acm_port_activate,
1103 .destruct = acm_port_destruct,
1104};
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
1107 * USB probe and disconnect routines.
1108 */
1109
Oliver Neukum830f4022008-06-25 14:17:16 +02001110/* Little helpers: write/read buffers free */
Oliver Neukum884b6002005-04-21 21:28:02 +02001111static void acm_write_buffers_free(struct acm *acm)
1112{
1113 int i;
1114 struct acm_wb *wb;
1115
Alan Cox6e47e062009-06-11 12:37:06 +01001116 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
Ladislav Michle4614602016-11-18 19:06:10 +01001117 usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
Oliver Neukum884b6002005-04-21 21:28:02 +02001118}
1119
Oliver Neukum830f4022008-06-25 14:17:16 +02001120static void acm_read_buffers_free(struct acm *acm)
1121{
Johan Hovolddab54c92011-03-22 11:12:21 +01001122 int i;
Oliver Neukum830f4022008-06-25 14:17:16 +02001123
Johan Hovolddab54c92011-03-22 11:12:21 +01001124 for (i = 0; i < acm->rx_buflimit; i++)
Ladislav Michle4614602016-11-18 19:06:10 +01001125 usb_free_coherent(acm->dev, acm->readsize,
Johan Hovold088c64f2011-03-25 11:06:02 +01001126 acm->read_buffers[i].base, acm->read_buffers[i].dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001127}
1128
Oliver Neukum884b6002005-04-21 21:28:02 +02001129/* Little helper: write buffers allocate */
1130static int acm_write_buffers_alloc(struct acm *acm)
1131{
1132 int i;
1133 struct acm_wb *wb;
1134
Oliver Neukum86478942006-05-13 22:50:47 +02001135 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Daniel Mack997ea582010-04-12 13:17:25 +02001136 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
Oliver Neukum884b6002005-04-21 21:28:02 +02001137 &wb->dmah);
1138 if (!wb->buf) {
1139 while (i != 0) {
1140 --i;
1141 --wb;
Daniel Mack997ea582010-04-12 13:17:25 +02001142 usb_free_coherent(acm->dev, acm->writesize,
Oliver Neukum884b6002005-04-21 21:28:02 +02001143 wb->buf, wb->dmah);
1144 }
1145 return -ENOMEM;
1146 }
1147 }
1148 return 0;
1149}
1150
Alan Cox10077d42009-06-11 12:36:09 +01001151static int acm_probe(struct usb_interface *intf,
1152 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001155 struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
David Brownellc6dbf552008-04-13 14:00:44 -07001156 unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int buflen = intf->altsetting->extralen;
1158 struct usb_interface *control_interface;
1159 struct usb_interface *data_interface;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001160 struct usb_endpoint_descriptor *epctrl = NULL;
1161 struct usb_endpoint_descriptor *epread = NULL;
1162 struct usb_endpoint_descriptor *epwrite = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct usb_device *usb_dev = interface_to_usbdev(intf);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001164 struct usb_cdc_parsed_header h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct acm *acm;
1166 int minor;
Alan Cox6e47e062009-06-11 12:37:06 +01001167 int ctrlsize, readsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 u8 *buf;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001169 int call_intf_num = -1;
1170 int data_intf_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +02001172 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +01001173 int i;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001174 int combined_interfaces = 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001175 struct device *tty_dev;
1176 int rv = -ENOMEM;
Johan Hovoldf8d84642017-03-17 11:35:48 +01001177 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Oliver Neukum86478942006-05-13 22:50:47 +02001179 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 quirks = (unsigned long)id->driver_info;
Dmitry Torokhov16142652013-02-02 11:02:14 -08001181
1182 if (quirks == IGNORE_DEVICE)
1183 return -ENODEV;
1184
Oliver Neukum7309aa82016-11-02 14:42:52 +01001185 memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
1186
Oliver Neukum86478942006-05-13 22:50:47 +02001187 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
1188
1189 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (quirks == NO_UNION_NORMAL) {
1191 data_interface = usb_ifnum_to_if(usb_dev, 1);
1192 control_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukum8835ba42016-03-15 10:14:04 +01001193 /* we would crash */
1194 if (!data_interface || !control_interface)
1195 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto skip_normal_probe;
1197 }
Alan Cox6e47e062009-06-11 12:37:06 +01001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* normal probing*/
1200 if (!buffer) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001201 dev_err(&intf->dev, "Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return -EINVAL;
1203 }
1204
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001205 if (!intf->cur_altsetting)
1206 return -EINVAL;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!buflen) {
Toby Gray577045c2010-09-02 10:46:20 +01001209 if (intf->cur_altsetting->endpoint &&
1210 intf->cur_altsetting->endpoint->extralen &&
Alan Cox6e47e062009-06-11 12:37:06 +01001211 intf->cur_altsetting->endpoint->extra) {
1212 dev_dbg(&intf->dev,
1213 "Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 buflen = intf->cur_altsetting->endpoint->extralen;
1215 buffer = intf->cur_altsetting->endpoint->extra;
1216 } else {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001217 dev_err(&intf->dev,
1218 "Zero length descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -EINVAL;
1220 }
1221 }
1222
Oliver Neukumcb42b632016-07-14 15:41:34 +02001223 cdc_parse_cdc_header(&h, intf, buffer, buflen);
1224 union_header = h.usb_cdc_union_desc;
1225 cmgmd = h.usb_cdc_call_mgmt_descriptor;
1226 if (cmgmd)
1227 call_intf_num = cmgmd->bDataInterface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (!union_header) {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001230 if (call_intf_num > 0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001231 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
Erik Slagterfd5054c12011-05-11 12:06:55 +02001232 /* quirks for Droids MuIn LCD */
Oliver Neukumcb42b632016-07-14 15:41:34 +02001233 if (quirks & NO_DATA_INTERFACE) {
Erik Slagterfd5054c12011-05-11 12:06:55 +02001234 data_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001235 } else {
1236 data_intf_num = call_intf_num;
1237 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 control_interface = intf;
1240 } else {
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001241 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1242 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1243 return -ENODEV;
1244 } else {
1245 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1246 combined_interfaces = 1;
1247 control_interface = data_interface = intf;
1248 goto look_for_collapsed_interface;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 } else {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001252 data_intf_num = union_header->bSlaveInterface0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001254 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
Greg Kroah-Hartman403dff42014-11-07 08:48:15 -08001255 }
1256
1257 if (!control_interface || !data_interface) {
1258 dev_dbg(&intf->dev, "no interfaces\n");
1259 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001261 if (!data_interface->cur_altsetting || !control_interface->cur_altsetting)
1262 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001263
Oliver Neukumcb42b632016-07-14 15:41:34 +02001264 if (data_intf_num != call_intf_num)
Alan Cox6e47e062009-06-11 12:37:06 +01001265 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001267 if (control_interface == data_interface) {
1268 /* some broken devices designed for windows work this way */
1269 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1270 combined_interfaces = 1;
1271 /* a popular other OS doesn't use it */
1272 quirks |= NO_CAP_LINE;
1273 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1274 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1275 return -EINVAL;
1276 }
1277look_for_collapsed_interface:
Johan Hovoldf8d84642017-03-17 11:35:48 +01001278 res = usb_find_common_endpoints(data_interface->cur_altsetting,
1279 &epread, &epwrite, &epctrl, NULL);
1280 if (res)
1281 return res;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001282
Johan Hovoldf8d84642017-03-17 11:35:48 +01001283 goto made_compressed_probe;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286skip_normal_probe:
1287
1288 /*workaround for switched interfaces */
Alan Cox6e47e062009-06-11 12:37:06 +01001289 if (data_interface->cur_altsetting->desc.bInterfaceClass
1290 != CDC_DATA_INTERFACE_TYPE) {
1291 if (control_interface->cur_altsetting->desc.bInterfaceClass
1292 == CDC_DATA_INTERFACE_TYPE) {
Alan Cox6e47e062009-06-11 12:37:06 +01001293 dev_dbg(&intf->dev,
1294 "Your device has switched interfaces.\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001295 swap(control_interface, data_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 } else {
1297 return -EINVAL;
1298 }
1299 }
Alan Stern74da5d62007-08-02 13:29:10 -04001300
1301 /* Accept probe requests only for the control interface */
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001302 if (!combined_interfaces && intf != control_interface)
Alan Stern74da5d62007-08-02 13:29:10 -04001303 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001304
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001305 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1306 /* valid in this context */
Alan Cox6e47e062009-06-11 12:37:06 +01001307 dev_dbg(&intf->dev, "The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EBUSY;
1309 }
1310
1311
Sven Schnelle99f347c2012-08-17 21:43:43 +02001312 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1313 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EINVAL;
1315
1316 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1317 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1318 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1319
1320
1321 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -03001322 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* descriptors are swapped */
Alan Cox6e47e062009-06-11 12:37:06 +01001324 dev_dbg(&intf->dev,
1325 "The data interface has switched endpoints\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001326 swap(epread, epwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001328made_compressed_probe:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001329 dev_dbg(&intf->dev, "interfaces are valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Alan Cox6e47e062009-06-11 12:37:06 +01001331 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001332 if (acm == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001335 minor = acm_alloc_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001336 if (minor < 0)
1337 goto alloc_fail1;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001338
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001339 ctrlsize = usb_endpoint_maxp(epctrl);
1340 readsize = usb_endpoint_maxp(epread) *
Alan Cox6e47e062009-06-11 12:37:06 +01001341 (quirks == SINGLE_RX_URB ? 1 : 2);
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001342 acm->combined_interfaces = combined_interfaces;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001343 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 acm->control = control_interface;
1345 acm->data = data_interface;
1346 acm->minor = minor;
1347 acm->dev = usb_dev;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001348 if (h.usb_cdc_acm_descriptor)
1349 acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001350 if (quirks & NO_CAP_LINE)
1351 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 acm->ctrlsize = ctrlsize;
1353 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +02001354 acm->rx_buflimit = num_rx_buf;
David Howellsc4028952006-11-22 14:57:56 +00001355 INIT_WORK(&acm->work, acm_softint);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001356 init_waitqueue_head(&acm->wioctl);
Oliver Neukum884b6002005-04-21 21:28:02 +02001357 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +01001358 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001359 mutex_init(&acm->mutex);
Ladislav Michld3053942016-11-18 19:10:19 +01001360 if (usb_endpoint_xfer_int(epread)) {
Oliver Neukumcf7fdd52009-08-04 23:52:09 +02001361 acm->bInterval = epread->bInterval;
Ladislav Michl74bccc92016-11-18 19:09:19 +01001362 acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
1363 } else {
1364 acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1365 }
1366 if (usb_endpoint_xfer_int(epwrite))
1367 acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
1368 else
1369 acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
Alan Cox739e0282009-06-11 12:27:50 +01001370 tty_port_init(&acm->port);
1371 acm->port.ops = &acm_port_ops;
Johan Hovold140cb812014-05-26 19:23:38 +02001372 init_usb_anchor(&acm->delayed);
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001373 acm->quirks = quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Daniel Mack997ea582010-04-12 13:17:25 +02001375 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001376 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto alloc_fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 acm->ctrl_buffer = buf;
1379
Oliver Neukum17136d42015-01-28 16:56:24 +01001380 if (acm_write_buffers_alloc(acm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 goto alloc_fail4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001384 if (!acm->ctrlurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto alloc_fail5;
Oliver Neukum17136d42015-01-28 16:56:24 +01001386
Oliver Neukum86478942006-05-13 22:50:47 +02001387 for (i = 0; i < num_rx_buf; i++) {
Johan Hovold088c64f2011-03-25 11:06:02 +01001388 struct acm_rb *rb = &(acm->read_buffers[i]);
1389 struct urb *urb;
David Kubicek61a87ad2005-11-01 18:51:34 +01001390
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001391 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1392 &rb->dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001393 if (!rb->base)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001394 goto alloc_fail6;
Johan Hovold088c64f2011-03-25 11:06:02 +01001395 rb->index = i;
1396 rb->instance = acm;
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001397
Johan Hovold088c64f2011-03-25 11:06:02 +01001398 urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001399 if (!urb)
Axel Linc2572b72010-05-31 08:04:47 +08001400 goto alloc_fail6;
Oliver Neukum17136d42015-01-28 16:56:24 +01001401
Johan Hovold088c64f2011-03-25 11:06:02 +01001402 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1403 urb->transfer_dma = rb->dma;
Ladislav Michld3053942016-11-18 19:10:19 +01001404 if (usb_endpoint_xfer_int(epread))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001405 usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001406 acm->readsize,
1407 acm_read_bulk_callback, rb,
1408 acm->bInterval);
Ladislav Michl74bccc92016-11-18 19:09:19 +01001409 else
1410 usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001411 acm->readsize,
1412 acm_read_bulk_callback, rb);
David Kubicek61a87ad2005-11-01 18:51:34 +01001413
Johan Hovold088c64f2011-03-25 11:06:02 +01001414 acm->read_urbs[i] = urb;
1415 __set_bit(i, &acm->read_urbs_free);
David Kubicek61a87ad2005-11-01 18:51:34 +01001416 }
Alan Cox6e47e062009-06-11 12:37:06 +01001417 for (i = 0; i < ACM_NW; i++) {
David Engrafe4cf3aa2008-03-20 10:01:34 +01001418 struct acm_wb *snd = &(acm->wb[i]);
1419
Alan Cox6e47e062009-06-11 12:37:06 +01001420 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001421 if (snd->urb == NULL)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001422 goto alloc_fail7;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001423
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001424 if (usb_endpoint_xfer_int(epwrite))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001425 usb_fill_int_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001426 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1427 else
Ladislav Michl74bccc92016-11-18 19:09:19 +01001428 usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001429 NULL, acm->writesize, acm_write_bulk, snd);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001430 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Lu Baoluffdb1e32016-01-06 15:10:04 +08001431 if (quirks & SEND_ZERO_PACKET)
1432 snd->urb->transfer_flags |= URB_ZERO_PACKET;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001433 snd->instance = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Alan Cox6e47e062009-06-11 12:37:06 +01001436 usb_set_intfdata(intf, acm);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001437
1438 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1439 if (i < 0)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001440 goto alloc_fail7;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001441
Oliver Neukumcb42b632016-07-14 15:41:34 +02001442 if (h.usb_cdc_country_functional_desc) { /* export the country data */
1443 struct usb_cdc_country_functional_desc * cfd =
1444 h.usb_cdc_country_functional_desc;
1445
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001446 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1447 if (!acm->country_codes)
1448 goto skip_countries;
1449 acm->country_code_size = cfd->bLength - 4;
Alan Cox6e47e062009-06-11 12:37:06 +01001450 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1451 cfd->bLength - 4);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001452 acm->country_rel_date = cfd->iCountryCodeRelDate;
1453
1454 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1455 if (i < 0) {
1456 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001457 acm->country_codes = NULL;
1458 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001459 goto skip_countries;
1460 }
1461
Alan Cox6e47e062009-06-11 12:37:06 +01001462 i = device_create_file(&intf->dev,
1463 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001464 if (i < 0) {
Axel Linc2572b72010-05-31 08:04:47 +08001465 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001466 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001467 acm->country_codes = NULL;
1468 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001469 goto skip_countries;
1470 }
1471 }
1472
1473skip_countries:
Alan Cox6e47e062009-06-11 12:37:06 +01001474 usb_fill_int_urb(acm->ctrlurb, usb_dev,
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001475 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1476 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1477 /* works around buggy devices */
Felipe Balbid102e782013-07-01 11:23:24 +03001478 epctrl->bInterval ? epctrl->bInterval : 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1480 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 acm->line.dwDTERate = cpu_to_le32(9600);
1485 acm->line.bDataBits = 8;
1486 acm_set_line(acm, &acm->line);
1487
1488 usb_driver_claim_interface(&acm_driver, data_interface, acm);
David Brownell672c4e12008-08-06 18:41:12 -07001489 usb_set_intfdata(data_interface, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001491 usb_get_intf(control_interface);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001492 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
Jiri Slaby734cc172012-08-07 21:47:47 +02001493 &control_interface->dev);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001494 if (IS_ERR(tty_dev)) {
1495 rv = PTR_ERR(tty_dev);
1496 goto alloc_fail8;
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001499 if (quirks & CLEAR_HALT_CONDITIONS) {
Ladislav Michl74bccc92016-11-18 19:09:19 +01001500 usb_clear_halt(usb_dev, acm->in);
1501 usb_clear_halt(usb_dev, acm->out);
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001502 }
1503
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001504 return 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001505alloc_fail8:
1506 if (acm->country_codes) {
1507 device_remove_file(&acm->control->dev,
1508 &dev_attr_wCountryCodes);
1509 device_remove_file(&acm->control->dev,
1510 &dev_attr_iCountryCodeRelDate);
Oliver Neukumd908f842014-11-20 14:54:35 +01001511 kfree(acm->country_codes);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001512 }
1513 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001514alloc_fail7:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001515 usb_set_intfdata(intf, NULL);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001516 for (i = 0; i < ACM_NW; i++)
1517 usb_free_urb(acm->wb[i].urb);
Axel Linc2572b72010-05-31 08:04:47 +08001518alloc_fail6:
Oliver Neukum86478942006-05-13 22:50:47 +02001519 for (i = 0; i < num_rx_buf; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +01001520 usb_free_urb(acm->read_urbs[i]);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001521 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 usb_free_urb(acm->ctrlurb);
1523alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001524 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525alloc_fail4:
Daniel Mack997ea582010-04-12 13:17:25 +02001526 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527alloc_fail2:
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001528 acm_release_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001529alloc_fail1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 kfree(acm);
1531alloc_fail:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001532 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535static void acm_disconnect(struct usb_interface *intf)
1536{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001537 struct acm *acm = usb_get_intfdata(intf);
Alan Cox10077d42009-06-11 12:36:09 +01001538 struct tty_struct *tty;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001539
David Brownell672c4e12008-08-06 18:41:12 -07001540 /* sibling interface is already cleaning up */
1541 if (!acm)
Oliver Neukum86067eea2006-01-08 12:39:13 +01001542 return;
David Brownell672c4e12008-08-06 18:41:12 -07001543
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001544 mutex_lock(&acm->mutex);
1545 acm->disconnected = true;
Alan Cox6e47e062009-06-11 12:37:06 +01001546 if (acm->country_codes) {
Alan Stern74da5d62007-08-02 13:29:10 -04001547 device_remove_file(&acm->control->dev,
1548 &dev_attr_wCountryCodes);
1549 device_remove_file(&acm->control->dev,
1550 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001551 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001552 wake_up_all(&acm->wioctl);
Alan Stern74da5d62007-08-02 13:29:10 -04001553 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001554 usb_set_intfdata(acm->control, NULL);
1555 usb_set_intfdata(acm->data, NULL);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001556 mutex_unlock(&acm->mutex);
1557
1558 tty = tty_port_tty_get(&acm->port);
1559 if (tty) {
1560 tty_vhangup(tty);
1561 tty_kref_put(tty);
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Ladislav Michlba8c9312016-11-18 19:07:08 +01001564 acm_kill_urbs(acm);
1565 cancel_work_sync(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Johan Hovoldcb255052013-03-19 09:21:06 +01001567 tty_unregister_device(acm_tty_driver, acm->minor);
1568
Oliver Neukum884b6002005-04-21 21:28:02 +02001569 acm_write_buffers_free(acm);
Ladislav Michle4614602016-11-18 19:06:10 +01001570 usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001571 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001573 if (!acm->combined_interfaces)
1574 usb_driver_release_interface(&acm_driver, intf == acm->control ?
Oliver Neukum830f4022008-06-25 14:17:16 +02001575 acm->data : acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001577 tty_port_put(&acm->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Oliver Neukum35758582008-07-01 19:10:08 +02001580#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001581static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1582{
1583 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001584 int cnt;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001585
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001586 spin_lock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001587 if (PMSG_IS_AUTO(message)) {
1588 if (acm->transmitting) {
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001589 spin_unlock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001590 return -EBUSY;
1591 }
1592 }
Oliver Neukum11ea8592008-06-20 11:25:57 +02001593 cnt = acm->susp_count++;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001594 spin_unlock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001595
1596 if (cnt)
Oliver Neukum1365baf72007-10-12 17:24:28 +02001597 return 0;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001598
Ladislav Michlba8c9312016-11-18 19:07:08 +01001599 acm_kill_urbs(acm);
1600 cancel_work_sync(&acm->work);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001601
Oliver Neukum1365baf72007-10-12 17:24:28 +02001602 return 0;
1603}
1604
1605static int acm_resume(struct usb_interface *intf)
1606{
1607 struct acm *acm = usb_get_intfdata(intf);
Johan Hovold140cb812014-05-26 19:23:38 +02001608 struct urb *urb;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001609 int rv = 0;
1610
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001611 spin_lock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001612
Johan Hovolde144ed282014-05-26 19:23:37 +02001613 if (--acm->susp_count)
1614 goto out;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001615
Peter Hurleyd41861c2016-04-09 17:53:25 -07001616 if (tty_port_initialized(&acm->port)) {
Johan Hovolde144ed282014-05-26 19:23:37 +02001617 rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001618
Johan Hovold140cb812014-05-26 19:23:38 +02001619 for (;;) {
1620 urb = usb_get_from_anchor(&acm->delayed);
1621 if (!urb)
1622 break;
1623
1624 acm_start_wb(acm, urb->context);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001625 }
1626
1627 /*
1628 * delayed error checking because we must
1629 * do the write path at all cost
1630 */
Oliver Neukum1365baf72007-10-12 17:24:28 +02001631 if (rv < 0)
Johan Hovolde144ed282014-05-26 19:23:37 +02001632 goto out;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001633
Johan Hovolde144ed282014-05-26 19:23:37 +02001634 rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001635 }
Johan Hovolde144ed282014-05-26 19:23:37 +02001636out:
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001637 spin_unlock_irq(&acm->write_lock);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001638
Oliver Neukum1365baf72007-10-12 17:24:28 +02001639 return rv;
1640}
Oliver Neukum35758582008-07-01 19:10:08 +02001641
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001642static int acm_reset_resume(struct usb_interface *intf)
1643{
1644 struct acm *acm = usb_get_intfdata(intf);
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001645
Peter Hurleyd41861c2016-04-09 17:53:25 -07001646 if (tty_port_initialized(&acm->port))
Jiri Slabyaa27a092013-03-07 13:12:30 +01001647 tty_port_tty_hangup(&acm->port, false);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001648
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001649 return acm_resume(intf);
1650}
1651
Oliver Neukum35758582008-07-01 19:10:08 +02001652#endif /* CONFIG_PM */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001653
Ladislav Michl1aba5792016-11-18 19:11:26 +01001654static int acm_pre_reset(struct usb_interface *intf)
1655{
1656 struct acm *acm = usb_get_intfdata(intf);
1657
1658 clear_bit(EVENT_RX_STALL, &acm->flags);
1659
1660 return 0;
1661}
1662
Adrian Taylorc1479a92009-11-19 10:35:33 +00001663#define NOKIA_PCSUITE_ACM_INFO(x) \
1664 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1665 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1666 USB_CDC_ACM_PROTO_VENDOR)
1667
Toby Gray4035e452010-09-01 16:01:19 +01001668#define SAMSUNG_PCSUITE_ACM_INFO(x) \
1669 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1670 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1671 USB_CDC_ACM_PROTO_VENDOR)
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/*
1674 * USB driver structure.
1675 */
1676
Németh Márton6ef48522010-01-10 15:33:45 +01001677static const struct usb_device_id acm_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* quirky and broken devices */
Björn Gerhartb20b1612015-02-18 07:19:44 +01001679 { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
1680 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
David Cluytens3b59d162013-12-03 14:18:57 +01001681 { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
1682 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1684 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1685 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001686 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1687 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1688 },
Andrew Lunn0f9c7b42008-12-23 17:31:23 +01001689 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1690 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1691 },
Masahito Omote8753e652005-07-29 12:17:25 -07001692 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1693 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1694 },
Chris Malley91a9c922006-10-03 10:08:28 +01001695 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1696 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1697 },
Alan Cox7abcf202009-04-06 17:35:01 +01001698 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1699 .driver_info = SINGLE_RX_URB,
1700 },
Oliver Neukum86478942006-05-13 22:50:47 +02001701 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1702 .driver_info = SINGLE_RX_URB, /* firmware bug */
1703 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001704 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1705 .driver_info = SINGLE_RX_URB, /* firmware bug */
1706 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001707 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1708 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1709 },
Iain McFarlane6149ed52008-05-04 00:13:49 +01001710 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1711 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1712 },
Eric Sandeenc8fd2c32008-08-14 08:25:40 -05001713 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1714 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1715 },
Alan Coxc89c60e2009-01-11 19:53:10 +00001716 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1717 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1718 },
Xiao Kaijiancab98a02009-05-08 00:48:23 +08001719 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1720 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1721 },
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001722 { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
1723 .driver_info = QUIRK_CONTROL_LINE_STATE, },
Johan Hovoldcf84a692014-10-27 18:34:33 +01001724 { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
Nathaniel Quillin30121602016-12-05 06:53:00 -08001725 { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
Dmitriy Taychenachev155df652009-02-25 12:36:51 +08001726 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1727 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001728 /* Motorola H24 HSPA module: */
1729 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
Michael Ulbricht895d2402014-03-25 10:34:18 +01001730 { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
1731 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1732 },
1733 { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
1734 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1735 },
1736 { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
1737 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1738 },
1739 { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
1740 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1741 },
1742 { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
1743 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1744 },
1745 { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
1746 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1747 },
1748 { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
1749 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1750 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001751
Adam Richterc332b4e2009-02-18 16:17:15 -08001752 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1753 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1754 data interface instead of
1755 communications interface.
1756 Maybe we should define a new
1757 quirk for this. */
1758 },
Jean-Christian de Rivaze7d491a2012-10-10 12:49:02 +00001759 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1760 .driver_info = NO_UNION_NORMAL,
1761 },
Denis N Ladin036915a2012-12-26 18:29:44 +05001762 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1763 .driver_info = NO_UNION_NORMAL,
1764 },
Kir Kolyshkin1f17c502009-05-28 20:33:58 +04001765 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1766 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1767 },
Russ Nelsonc3baa192010-04-21 23:07:03 -04001768 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1769 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1770 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001771
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001772 { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
1773 .driver_info = CLEAR_HALT_CONDITIONS,
1774 },
1775
Adrian Taylorc1479a92009-11-19 10:35:33 +00001776 /* Nokia S60 phones expose two ACM channels. The first is
1777 * a modem and is picked up by the standard AT-command
1778 * information below. The second is 'vendor-specific' but
1779 * is treated as a serial device at the S60 end, so we want
1780 * to expose it on Linux too. */
1781 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1782 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1783 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1784 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1785 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1786 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1787 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1788 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1789 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1790 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1791 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1792 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1793 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1794 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1795 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1796 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1797 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1798 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1799 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1800 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1801 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1802 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1803 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1804 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1805 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1806 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1807 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1808 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1809 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1810 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1811 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1812 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1813 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1814 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1815 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1816 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1817 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1818 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1819 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1820 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1821 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1822 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1823 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
Przemo Firszt83a4eae2010-06-28 21:29:34 +01001824 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
Toby Gray4035e452010-09-01 16:01:19 +01001825 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1826 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1827 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1828 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1829 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1830 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1831 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1832 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1833 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1834 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
Arvid Ephraim Picciani721d92f2011-01-25 15:58:40 +01001835 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
Toby Gray4061fde2011-06-06 14:52:48 +01001836 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1837 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
Toby Gray4035e452010-09-01 16:01:19 +01001838 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001839
Denis Pershin65e52f42011-09-04 17:37:21 +07001840 /* Support for Owen devices */
1841 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1842
Adrian Taylorc1479a92009-11-19 10:35:33 +00001843 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1844
Erik Slagterfd5054c12011-05-11 12:06:55 +02001845 /* Support for Droids MuIn LCD */
1846 { USB_DEVICE(0x04d8, 0x000b),
1847 .driver_info = NO_DATA_INTERFACE,
1848 },
1849
Dmitry Torokhov16142652013-02-02 11:02:14 -08001850#if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1851 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1852 .driver_info = IGNORE_DEVICE,
1853 },
1854 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1855 .driver_info = IGNORE_DEVICE,
1856 },
1857#endif
1858
Oliver Neukume912e682016-01-18 15:45:18 +01001859 /*Samsung phone in firmware update mode */
1860 { USB_DEVICE(0x04e8, 0x685d),
1861 .driver_info = IGNORE_DEVICE,
1862 },
1863
Jonas Jonssonf33a7f722015-11-22 11:47:17 +01001864 /* Exclude Infineon Flash Loader utility */
1865 { USB_DEVICE(0x058b, 0x0041),
1866 .driver_info = IGNORE_DEVICE,
1867 },
1868
Philippe Corbes5b239f02010-08-31 19:31:32 +02001869 /* control interfaces without any protocol set */
1870 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1871 USB_CDC_PROTO_NONE) },
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* control interfaces with various AT-command sets */
1874 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1875 USB_CDC_ACM_PROTO_AT_V25TER) },
1876 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1877 USB_CDC_ACM_PROTO_AT_PCCA101) },
1878 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1879 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1880 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1881 USB_CDC_ACM_PROTO_AT_GSM) },
1882 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
Alan Cox6e47e062009-06-11 12:37:06 +01001883 USB_CDC_ACM_PROTO_AT_3G) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1885 USB_CDC_ACM_PROTO_AT_CDMA) },
1886
Lu Baoluffdb1e32016-01-06 15:10:04 +08001887 { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
1888 .driver_info = SEND_ZERO_PACKET,
1889 },
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 { }
1892};
1893
Alan Cox6e47e062009-06-11 12:37:06 +01001894MODULE_DEVICE_TABLE(usb, acm_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 .name = "cdc_acm",
1898 .probe = acm_probe,
1899 .disconnect = acm_disconnect,
Oliver Neukum35758582008-07-01 19:10:08 +02001900#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001901 .suspend = acm_suspend,
1902 .resume = acm_resume,
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001903 .reset_resume = acm_reset_resume,
Oliver Neukum35758582008-07-01 19:10:08 +02001904#endif
Ladislav Michl1aba5792016-11-18 19:11:26 +01001905 .pre_reset = acm_pre_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 .id_table = acm_ids,
Oliver Neukum35758582008-07-01 19:10:08 +02001907#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001908 .supports_autosuspend = 1,
Oliver Neukum35758582008-07-01 19:10:08 +02001909#endif
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001910 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
1913/*
1914 * TTY driver structures.
1915 */
1916
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001917static const struct tty_operations acm_ops = {
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001918 .install = acm_tty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 .open = acm_tty_open,
1920 .close = acm_tty_close,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001921 .cleanup = acm_tty_cleanup,
Alan Cox10077d42009-06-11 12:36:09 +01001922 .hangup = acm_tty_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 .write = acm_tty_write,
Oliver Neukuma81cf972016-02-10 10:39:49 +01001924 .put_char = acm_tty_put_char,
1925 .flush_chars = acm_tty_flush_chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 .write_room = acm_tty_write_room,
1927 .ioctl = acm_tty_ioctl,
1928 .throttle = acm_tty_throttle,
1929 .unthrottle = acm_tty_unthrottle,
1930 .chars_in_buffer = acm_tty_chars_in_buffer,
1931 .break_ctl = acm_tty_break_ctl,
1932 .set_termios = acm_tty_set_termios,
1933 .tiocmget = acm_tty_tiocmget,
1934 .tiocmset = acm_tty_tiocmset,
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001935 .get_icount = acm_tty_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936};
1937
1938/*
1939 * Init / exit.
1940 */
1941
1942static int __init acm_init(void)
1943{
1944 int retval;
1945 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1946 if (!acm_tty_driver)
1947 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 acm_tty_driver->driver_name = "acm",
1949 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 acm_tty_driver->major = ACM_TTY_MAJOR,
1951 acm_tty_driver->minor_start = 0,
1952 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1953 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001954 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 acm_tty_driver->init_termios = tty_std_termios;
Alan Cox6e47e062009-06-11 12:37:06 +01001956 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1957 HUPCL | CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 tty_set_operations(acm_tty_driver, &acm_ops);
1959
1960 retval = tty_register_driver(acm_tty_driver);
1961 if (retval) {
1962 put_tty_driver(acm_tty_driver);
1963 return retval;
1964 }
1965
1966 retval = usb_register(&acm_driver);
1967 if (retval) {
1968 tty_unregister_driver(acm_tty_driver);
1969 put_tty_driver(acm_tty_driver);
1970 return retval;
1971 }
1972
Johan Hovolda2c7b932011-03-22 11:12:18 +01001973 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 return 0;
1976}
1977
1978static void __exit acm_exit(void)
1979{
1980 usb_deregister(&acm_driver);
1981 tty_unregister_driver(acm_tty_driver);
1982 put_tty_driver(acm_tty_driver);
Johannes Thumshirn91b72562015-07-08 17:25:42 +02001983 idr_destroy(&acm_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
1986module_init(acm_init);
1987module_exit(acm_exit);
1988
Alan Cox6e47e062009-06-11 12:37:06 +01001989MODULE_AUTHOR(DRIVER_AUTHOR);
1990MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991MODULE_LICENSE("GPL");
Scott James Remnante766aeb2009-04-06 17:33:18 +01001992MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);