blob: 5357d83bbda2b87d8823b24995dbfcd626c2262f [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>
Tobias Herzogea258352017-03-30 22:15:11 +020039#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/tty.h>
Oliver Neukum7af25b42009-09-08 23:51:28 +020041#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/tty_driver.h>
43#include <linux/tty_flip.h>
44#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010045#include <linux/mutex.h>
Alan Cox10077d42009-06-11 12:36:09 +010046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070048#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/byteorder.h>
50#include <asm/unaligned.h>
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020051#include <linux/idr.h>
David Kubicek61a87ad2005-11-01 18:51:34 +010052#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "cdc-acm.h"
55
David Brownelle5fbab52008-08-06 18:46:10 -070056
Johan Hovold088c64f2011-03-25 11:06:02 +010057#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
59
60static struct usb_driver acm_driver;
61static struct tty_driver *acm_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020063static DEFINE_IDR(acm_minors);
64static DEFINE_MUTEX(acm_minors_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jim Paris24cb4502014-10-30 11:04:47 -040066static void acm_tty_set_termios(struct tty_struct *tty,
67 struct ktermios *termios_old);
68
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080069/*
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020070 * acm_minors accessors
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080073/*
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020074 * Look up an ACM structure by minor. If found and not disconnected, increment
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080075 * its refcount and return it with its mutex held.
76 */
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020077static struct acm *acm_get_by_minor(unsigned int minor)
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080078{
79 struct acm *acm;
80
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020081 mutex_lock(&acm_minors_lock);
82 acm = idr_find(&acm_minors, minor);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080083 if (acm) {
84 mutex_lock(&acm->mutex);
85 if (acm->disconnected) {
86 mutex_unlock(&acm->mutex);
87 acm = NULL;
88 } else {
89 tty_port_get(&acm->port);
90 mutex_unlock(&acm->mutex);
91 }
92 }
Johan Hovold6cb4f4d2015-05-18 17:34:11 +020093 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080094 return acm;
95}
96
97/*
98 * Try to find an available minor number and if found, associate it with 'acm'.
99 */
100static int acm_alloc_minor(struct acm *acm)
101{
102 int minor;
103
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200104 mutex_lock(&acm_minors_lock);
105 minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
106 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800107
108 return minor;
109}
110
111/* Release the minor number associated with 'acm'. */
112static void acm_release_minor(struct acm *acm)
113{
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200114 mutex_lock(&acm_minors_lock);
115 idr_remove(&acm_minors, acm->minor);
116 mutex_unlock(&acm_minors_lock);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800117}
Alan Cox739e0282009-06-11 12:27:50 +0100118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Functions for ACM control messages.
121 */
122
Alan Cox6e47e062009-06-11 12:37:06 +0100123static int acm_ctrl_msg(struct acm *acm, int request, int value,
124 void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200126 int retval;
127
128 retval = usb_autopm_get_interface(acm->control);
129 if (retval)
130 return retval;
131
132 retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 request, USB_RT_ACM, value,
134 acm->control->altsetting[0].desc.bInterfaceNumber,
135 buf, len, 5000);
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200136
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100137 dev_dbg(&acm->control->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100138 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
139 __func__, request, value, len, retval);
Johan Hovoldbae3f4c2014-05-26 19:23:39 +0200140
141 usb_autopm_put_interface(acm->control);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return retval < 0 ? retval : 0;
144}
145
146/* devices aren't required to support these requests.
147 * the cdc acm descriptor tells whether they do...
148 */
Johan Hovold2a8cdfd2014-11-06 18:08:33 +0100149static inline int acm_set_control(struct acm *acm, int control)
150{
151 if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
152 return -EOPNOTSUPP;
153
154 return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
155 control, NULL, 0);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define acm_set_line(acm, line) \
159 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
160#define acm_send_break(acm, ms) \
161 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
162
Ladislav Michlba8c9312016-11-18 19:07:08 +0100163static void acm_kill_urbs(struct acm *acm)
164{
165 int i;
166
167 usb_kill_urb(acm->ctrlurb);
168 for (i = 0; i < ACM_NW; i++)
169 usb_kill_urb(acm->wb[i].urb);
170 for (i = 0; i < acm->rx_buflimit; i++)
171 usb_kill_urb(acm->read_urbs[i]);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Oliver Neukum884b6002005-04-21 21:28:02 +0200175 * Write buffer management.
176 * All of these assume proper locks taken by the caller.
177 */
178
179static int acm_wb_alloc(struct acm *acm)
180{
181 int i, wbn;
182 struct acm_wb *wb;
183
David Engrafe4cf3aa2008-03-20 10:01:34 +0100184 wbn = 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200185 i = 0;
186 for (;;) {
187 wb = &acm->wb[wbn];
188 if (!wb->use) {
189 wb->use = 1;
190 return wbn;
191 }
Oliver Neukum86478942006-05-13 22:50:47 +0200192 wbn = (wbn + 1) % ACM_NW;
193 if (++i >= ACM_NW)
Oliver Neukum884b6002005-04-21 21:28:02 +0200194 return -1;
195 }
196}
197
Oliver Neukum884b6002005-04-21 21:28:02 +0200198static int acm_wb_is_avail(struct acm *acm)
199{
200 int i, n;
David Brownelle5fbab52008-08-06 18:46:10 -0700201 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200202
Oliver Neukum86478942006-05-13 22:50:47 +0200203 n = ACM_NW;
David Brownelle5fbab52008-08-06 18:46:10 -0700204 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100205 for (i = 0; i < ACM_NW; i++)
Oliver Neukum86478942006-05-13 22:50:47 +0200206 n -= acm->wb[i].use;
David Brownelle5fbab52008-08-06 18:46:10 -0700207 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200208 return n;
209}
210
Oliver Neukum884b6002005-04-21 21:28:02 +0200211/*
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800212 * Finish write. Caller must hold acm->write_lock
Oliver Neukum884b6002005-04-21 21:28:02 +0200213 */
David Engrafe4cf3aa2008-03-20 10:01:34 +0100214static void acm_write_done(struct acm *acm, struct acm_wb *wb)
Oliver Neukum884b6002005-04-21 21:28:02 +0200215{
David Engrafe4cf3aa2008-03-20 10:01:34 +0100216 wb->use = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200217 acm->transmitting--;
Oliver Neukum97d35f92009-12-16 17:05:57 +0100218 usb_autopm_put_interface_async(acm->control);
Oliver Neukum884b6002005-04-21 21:28:02 +0200219}
220
221/*
222 * Poke write.
Oliver Neukum11ea8592008-06-20 11:25:57 +0200223 *
224 * the caller is responsible for locking
Oliver Neukum884b6002005-04-21 21:28:02 +0200225 */
Oliver Neukum11ea8592008-06-20 11:25:57 +0200226
227static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
228{
229 int rc;
230
231 acm->transmitting++;
232
233 wb->urb->transfer_buffer = wb->buf;
234 wb->urb->transfer_dma = wb->dmah;
235 wb->urb->transfer_buffer_length = wb->len;
236 wb->urb->dev = acm->dev;
237
Alan Cox6e47e062009-06-11 12:37:06 +0100238 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
239 if (rc < 0) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100240 dev_err(&acm->data->dev,
241 "%s - usb_submit_urb(write bulk) failed: %d\n",
242 __func__, rc);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200243 acm_write_done(acm, wb);
244 }
245 return rc;
246}
247
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100248/*
249 * attributes exported through sysfs
250 */
251static ssize_t show_caps
252(struct device *dev, struct device_attribute *attr, char *buf)
253{
254 struct usb_interface *intf = to_usb_interface(dev);
255 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum884b6002005-04-21 21:28:02 +0200256
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100257 return sprintf(buf, "%d", acm->ctrl_caps);
258}
259static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
260
261static ssize_t show_country_codes
262(struct device *dev, struct device_attribute *attr, char *buf)
263{
264 struct usb_interface *intf = to_usb_interface(dev);
265 struct acm *acm = usb_get_intfdata(intf);
266
267 memcpy(buf, acm->country_codes, acm->country_code_size);
268 return acm->country_code_size;
269}
270
271static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
272
273static ssize_t show_country_rel_date
274(struct device *dev, struct device_attribute *attr, char *buf)
275{
276 struct usb_interface *intf = to_usb_interface(dev);
277 struct acm *acm = usb_get_intfdata(intf);
278
279 return sprintf(buf, "%d", acm->country_rel_date);
280}
281
282static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
Oliver Neukum884b6002005-04-21 21:28:02 +0200283/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * Interrupt handlers for various ACM device responses
285 */
286
Tobias Herzogea258352017-03-30 22:15:11 +0200287static void acm_process_notification(struct acm *acm, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int newctrl;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100290 int difference;
Tobias Herzogea258352017-03-30 22:15:11 +0200291 struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf;
292 unsigned char *data = buf + sizeof(struct usb_cdc_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 switch (dr->bNotificationType) {
Alan Cox6e47e062009-06-11 12:37:06 +0100295 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
Ladislav Michl90744af82016-11-18 19:01:22 +0100296 dev_dbg(&acm->control->dev,
297 "%s - network connection: %d\n", __func__, dr->wValue);
Alan Cox6e47e062009-06-11 12:37:06 +0100298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Alan Cox6e47e062009-06-11 12:37:06 +0100300 case USB_CDC_NOTIFY_SERIAL_STATE:
Tobias Herzog1bb99142017-03-30 22:15:10 +0200301 if (le16_to_cpu(dr->wLength) != 2) {
302 dev_dbg(&acm->control->dev,
303 "%s - malformed serial state\n", __func__);
304 break;
305 }
306
Alan Cox6e47e062009-06-11 12:37:06 +0100307 newctrl = get_unaligned_le16(data);
Tobias Herzog2d6dfd12017-03-30 22:15:12 +0200308 dev_dbg(&acm->control->dev,
309 "%s - serial state: 0x%x\n", __func__, newctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Jiri Slabyaa27a092013-03-07 13:12:30 +0100311 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
Ladislav Michl90744af82016-11-18 19:01:22 +0100312 dev_dbg(&acm->control->dev,
313 "%s - calling hangup\n", __func__);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100314 tty_port_tty_hangup(&acm->port, false);
Alan Cox6e47e062009-06-11 12:37:06 +0100315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100317 difference = acm->ctrlin ^ newctrl;
318 spin_lock(&acm->read_lock);
Alan Cox6e47e062009-06-11 12:37:06 +0100319 acm->ctrlin = newctrl;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100320 acm->oldcount = acm->iocount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Oliver Neukum5a6a62b2013-11-20 11:35:34 +0100322 if (difference & ACM_CTRL_DSR)
323 acm->iocount.dsr++;
324 if (difference & ACM_CTRL_BRK)
325 acm->iocount.brk++;
326 if (difference & ACM_CTRL_RI)
327 acm->iocount.rng++;
328 if (difference & ACM_CTRL_DCD)
329 acm->iocount.dcd++;
330 if (difference & ACM_CTRL_FRAMING)
331 acm->iocount.frame++;
332 if (difference & ACM_CTRL_PARITY)
333 acm->iocount.parity++;
334 if (difference & ACM_CTRL_OVERRUN)
335 acm->iocount.overrun++;
336 spin_unlock(&acm->read_lock);
337
338 if (difference)
339 wake_up_all(&acm->wioctl);
340
341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Alan Cox6e47e062009-06-11 12:37:06 +0100343 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100344 dev_dbg(&acm->control->dev,
Tobias Herzog1bb99142017-03-30 22:15:10 +0200345 "%s - unknown notification %d received: index %d len %d\n",
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100346 __func__,
Tobias Herzog1bb99142017-03-30 22:15:10 +0200347 dr->bNotificationType, dr->wIndex, dr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Tobias Herzogea258352017-03-30 22:15:11 +0200349}
350
351/* control interface reports status changes with "interrupt" transfers */
352static void acm_ctrl_irq(struct urb *urb)
353{
354 struct acm *acm = urb->context;
355 struct usb_cdc_notification *dr = urb->transfer_buffer;
356 unsigned int current_size = urb->actual_length;
357 unsigned int expected_size, copy_size, alloc_size;
358 int retval;
359 int status = urb->status;
360
361 switch (status) {
362 case 0:
363 /* success */
364 break;
365 case -ECONNRESET:
366 case -ENOENT:
367 case -ESHUTDOWN:
368 /* this urb is terminated, clean up */
369 acm->nb_index = 0;
370 dev_dbg(&acm->control->dev,
371 "%s - urb shutting down with status: %d\n",
372 __func__, status);
373 return;
374 default:
375 dev_dbg(&acm->control->dev,
376 "%s - nonzero urb status received: %d\n",
377 __func__, status);
378 goto exit;
379 }
380
381 usb_mark_last_busy(acm->dev);
382
383 if (acm->nb_index)
384 dr = (struct usb_cdc_notification *)acm->notification_buffer;
385
386 /* size = notification-header + (optional) data */
387 expected_size = sizeof(struct usb_cdc_notification) +
388 le16_to_cpu(dr->wLength);
389
390 if (current_size < expected_size) {
391 /* notification is transmitted fragmented, reassemble */
392 if (acm->nb_size < expected_size) {
393 if (acm->nb_size) {
394 kfree(acm->notification_buffer);
395 acm->nb_size = 0;
396 }
397 alloc_size = roundup_pow_of_two(expected_size);
398 /*
399 * kmalloc ensures a valid notification_buffer after a
400 * use of kfree in case the previous allocation was too
401 * small. Final freeing is done on disconnect.
402 */
403 acm->notification_buffer =
404 kmalloc(alloc_size, GFP_ATOMIC);
405 if (!acm->notification_buffer)
406 goto exit;
407 acm->nb_size = alloc_size;
408 }
409
410 copy_size = min(current_size,
411 expected_size - acm->nb_index);
412
413 memcpy(&acm->notification_buffer[acm->nb_index],
414 urb->transfer_buffer, copy_size);
415 acm->nb_index += copy_size;
416 current_size = acm->nb_index;
417 }
418
419 if (current_size >= expected_size) {
420 /* notification complete */
421 acm_process_notification(acm, (unsigned char *)dr);
422 acm->nb_index = 0;
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425exit:
Alan Cox6e47e062009-06-11 12:37:06 +0100426 retval = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum6c8074e2015-03-20 11:25:17 +0100427 if (retval && retval != -EPERM)
Ladislav Michl90744af82016-11-18 19:01:22 +0100428 dev_err(&acm->control->dev,
429 "%s - usb_submit_urb failed: %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Johan Hovold088c64f2011-03-25 11:06:02 +0100432static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Johan Hovold088c64f2011-03-25 11:06:02 +0100434 int res;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700435
Johan Hovold088c64f2011-03-25 11:06:02 +0100436 if (!test_and_clear_bit(index, &acm->read_urbs_free))
437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Johan Hovold088c64f2011-03-25 11:06:02 +0100439 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
440 if (res) {
441 if (res != -EPERM) {
442 dev_err(&acm->data->dev,
Ladislav Michl90744af82016-11-18 19:01:22 +0100443 "urb %d failed submission with %d\n",
444 index, res);
Johan Hovold088c64f2011-03-25 11:06:02 +0100445 }
446 set_bit(index, &acm->read_urbs_free);
447 return res;
Oliver Neukum46e75072016-09-13 16:44:36 +0200448 } else {
449 dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
Johan Hovold088c64f2011-03-25 11:06:02 +0100450 }
451
452 return 0;
453}
454
455static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
456{
457 int res;
458 int i;
459
460 for (i = 0; i < acm->rx_buflimit; ++i) {
461 res = acm_submit_read_urb(acm, i, mem_flags);
462 if (res)
463 return res;
464 }
465
466 return 0;
467}
468
469static void acm_process_read_urb(struct acm *acm, struct urb *urb)
470{
Johan Hovold088c64f2011-03-25 11:06:02 +0100471 if (!urb->actual_length)
472 return;
473
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100474 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
475 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100476 tty_flip_buffer_push(&acm->port);
Johan Hovold088c64f2011-03-25 11:06:02 +0100477}
478
479static void acm_read_bulk_callback(struct urb *urb)
480{
481 struct acm_rb *rb = urb->context;
482 struct acm *acm = rb->instance;
483 unsigned long flags;
Oliver Neukum36e59e02015-03-20 09:24:24 +0100484 int status = urb->status;
Johan Hovold088c64f2011-03-25 11:06:02 +0100485
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200486 dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
Ladislav Michl90744af82016-11-18 19:01:22 +0100487 rb->index, urb->actual_length, status);
Johan Hovold088c64f2011-03-25 11:06:02 +0100488
Ladislav Michl1aba5792016-11-18 19:11:26 +0100489 set_bit(rb->index, &acm->read_urbs_free);
490
Johan Hovold088c64f2011-03-25 11:06:02 +0100491 if (!acm->dev) {
492 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ladislav Michl1aba5792016-11-18 19:11:26 +0100496 switch (status) {
497 case 0:
498 usb_mark_last_busy(acm->dev);
499 acm_process_read_urb(acm, urb);
500 break;
501 case -EPIPE:
502 set_bit(EVENT_RX_STALL, &acm->flags);
503 schedule_work(&acm->work);
504 return;
505 case -ENOENT:
506 case -ECONNRESET:
507 case -ESHUTDOWN:
508 dev_dbg(&acm->data->dev,
509 "%s - urb shutting down with status: %d\n",
510 __func__, status);
511 return;
512 default:
513 dev_dbg(&acm->data->dev,
514 "%s - nonzero urb status received: %d\n",
515 __func__, status);
516 break;
Johan Hovold088c64f2011-03-25 11:06:02 +0100517 }
Johan Hovold4a8ee502014-05-26 19:23:49 +0200518
Oliver Neukum36e59e02015-03-20 09:24:24 +0100519 /*
520 * Unthrottle may run on another CPU which needs to see events
521 * in the same order. Submission has an implict barrier
522 */
523 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Johan Hovold088c64f2011-03-25 11:06:02 +0100525 /* throttle device if requested by tty */
526 spin_lock_irqsave(&acm->read_lock, flags);
527 acm->throttled = acm->throttle_req;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +0200528 if (!acm->throttled) {
Johan Hovold088c64f2011-03-25 11:06:02 +0100529 spin_unlock_irqrestore(&acm->read_lock, flags);
530 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
Oliver Neukum86478942006-05-13 22:50:47 +0200531 } else {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200532 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100537static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Ming Leicdc97792008-02-24 18:41:47 +0800539 struct acm_wb *wb = urb->context;
David Brownelle5fbab52008-08-06 18:46:10 -0700540 struct acm *acm = wb->instance;
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800541 unsigned long flags;
Oliver Neukum4132cd02015-03-20 11:41:06 +0100542 int status = urb->status;
Oliver Neukum884b6002005-04-21 21:28:02 +0200543
Oliver Neukum4132cd02015-03-20 11:41:06 +0100544 if (status || (urb->actual_length != urb->transfer_buffer_length))
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200545 dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
David Brownelle5fbab52008-08-06 18:46:10 -0700546 urb->actual_length,
547 urb->transfer_buffer_length,
Oliver Neukum4132cd02015-03-20 11:41:06 +0100548 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800550 spin_lock_irqsave(&acm->write_lock, flags);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100551 acm_write_done(acm, wb);
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800552 spin_unlock_irqrestore(&acm->write_lock, flags);
Ladislav Michl1aba5792016-11-18 19:11:26 +0100553 set_bit(EVENT_TTY_WAKEUP, &acm->flags);
Havard Skinnemoen99823f42011-12-09 16:51:54 -0800554 schedule_work(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
David Howellsc4028952006-11-22 14:57:56 +0000557static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Ladislav Michl1aba5792016-11-18 19:11:26 +0100559 int i;
David Howellsc4028952006-11-22 14:57:56 +0000560 struct acm *acm = container_of(work, struct acm, work);
David Brownelle5fbab52008-08-06 18:46:10 -0700561
Ladislav Michl1aba5792016-11-18 19:11:26 +0100562 if (test_bit(EVENT_RX_STALL, &acm->flags)) {
563 if (!(usb_autopm_get_interface(acm->data))) {
564 for (i = 0; i < acm->rx_buflimit; i++)
565 usb_kill_urb(acm->read_urbs[i]);
566 usb_clear_halt(acm->dev, acm->in);
567 acm_submit_read_urbs(acm, GFP_KERNEL);
568 usb_autopm_put_interface(acm->data);
569 }
570 clear_bit(EVENT_RX_STALL, &acm->flags);
571 }
572
573 if (test_bit(EVENT_TTY_WAKEUP, &acm->flags)) {
574 tty_port_tty_wakeup(&acm->port);
575 clear_bit(EVENT_TTY_WAKEUP, &acm->flags);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/*
580 * TTY handlers
581 */
582
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800583static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct acm *acm;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800586 int retval;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100587
Johan Hovold6cb4f4d2015-05-18 17:34:11 +0200588 acm = acm_get_by_minor(tty->index);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800589 if (!acm)
590 return -ENODEV;
591
Jiri Slabyf8a8c102012-03-05 14:51:48 +0100592 retval = tty_standard_install(driver, tty);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800593 if (retval)
594 goto error_init_termios;
595
596 tty->driver_data = acm;
597
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800598 return 0;
599
600error_init_termios:
601 tty_port_put(&acm->port);
602 return retval;
603}
604
605static int acm_tty_open(struct tty_struct *tty, struct file *filp)
606{
607 struct acm *acm = tty->driver_data;
608
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800609 return tty_port_open(&acm->port, tty, filp);
610}
611
Johan Hovold0943d8e2014-05-26 19:23:51 +0200612static void acm_port_dtr_rts(struct tty_port *port, int raise)
613{
614 struct acm *acm = container_of(port, struct acm, port);
615 int val;
616 int res;
617
618 if (raise)
619 val = ACM_CTRL_DTR | ACM_CTRL_RTS;
620 else
621 val = 0;
622
623 /* FIXME: add missing ctrlout locking throughout driver */
624 acm->ctrlout = val;
625
626 res = acm_set_control(acm, val);
627 if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
628 dev_err(&acm->control->dev, "failed to set dtr/rts\n");
629}
630
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800631static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
632{
633 struct acm *acm = container_of(port, struct acm, port);
634 int retval = -ENODEV;
Johan Hovolde4c36072014-05-26 19:23:44 +0200635 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Oliver Neukum1365baf72007-10-12 17:24:28 +0200637 mutex_lock(&acm->mutex);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800638 if (acm->disconnected)
639 goto disconnected;
640
641 retval = usb_autopm_get_interface(acm->control);
642 if (retval)
643 goto error_get_interface;
644
645 /*
646 * FIXME: Why do we need this? Allocating 64K of physically contiguous
647 * memory is really nasty...
648 */
649 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
650 acm->control->needs_remote_wakeup = 1;
Oliver Neukum1365baf72007-10-12 17:24:28 +0200651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 acm->ctrlurb->dev = acm->dev;
Johan Hovold8727bf62014-05-26 19:23:43 +0200653 retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
654 if (retval) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100655 dev_err(&acm->control->dev,
656 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800657 goto error_submit_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Jim Paris24cb4502014-10-30 11:04:47 -0400660 acm_tty_set_termios(tty, NULL);
661
Otto Meta6c4707f2012-06-06 18:46:21 +0200662 /*
663 * Unthrottle device in case the TTY was closed while throttled.
664 */
665 spin_lock_irq(&acm->read_lock);
666 acm->throttled = 0;
667 acm->throttle_req = 0;
668 spin_unlock_irq(&acm->read_lock);
669
Johan Hovold8727bf62014-05-26 19:23:43 +0200670 retval = acm_submit_read_urbs(acm, GFP_KERNEL);
671 if (retval)
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800672 goto error_submit_read_urbs;
Oliver Neukum2b626dc2010-02-03 17:10:22 +0100673
Johan Hovold703df322014-05-26 19:23:42 +0200674 usb_autopm_put_interface(acm->control);
675
Oliver Neukum1365baf72007-10-12 17:24:28 +0200676 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800678 return 0;
679
680error_submit_read_urbs:
Johan Hovolde4c36072014-05-26 19:23:44 +0200681 for (i = 0; i < acm->rx_buflimit; i++)
682 usb_kill_urb(acm->read_urbs[i]);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800683 usb_kill_urb(acm->ctrlurb);
684error_submit_urb:
Johan Hovold703df322014-05-26 19:23:42 +0200685 usb_autopm_put_interface(acm->control);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800686error_get_interface:
687disconnected:
688 mutex_unlock(&acm->mutex);
Johan Hovold8727bf62014-05-26 19:23:43 +0200689
690 return usb_translate_errors(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800693static void acm_port_destruct(struct tty_port *port)
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700694{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800695 struct acm *acm = container_of(port, struct acm, port);
696
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800697 acm_release_minor(acm);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700698 usb_put_intf(acm->control);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100699 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700700 kfree(acm);
701}
702
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800703static void acm_port_shutdown(struct tty_port *port)
Alan Cox10077d42009-06-11 12:36:09 +0100704{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800705 struct acm *acm = container_of(port, struct acm, port);
Johan Hovold140cb812014-05-26 19:23:38 +0200706 struct urb *urb;
707 struct acm_wb *wb;
Johan Hovolddab54c92011-03-22 11:12:21 +0100708
Johan Hovoldb1d42ef2014-05-26 19:23:48 +0200709 /*
710 * Need to grab write_lock to prevent race with resume, but no need to
711 * hold it due to the tty-port initialised flag.
712 */
713 spin_lock_irq(&acm->write_lock);
714 spin_unlock_irq(&acm->write_lock);
715
716 usb_autopm_get_interface_no_resume(acm->control);
717 acm->control->needs_remote_wakeup = 0;
718 usb_autopm_put_interface(acm->control);
719
Johan Hovold89e54e42014-05-26 19:23:47 +0200720 for (;;) {
721 urb = usb_get_from_anchor(&acm->delayed);
722 if (!urb)
723 break;
724 wb = urb->context;
725 wb->use = 0;
726 usb_autopm_put_interface_async(acm->control);
Alan Cox10077d42009-06-11 12:36:09 +0100727 }
Johan Hovold89e54e42014-05-26 19:23:47 +0200728
Ladislav Michlba8c9312016-11-18 19:07:08 +0100729 acm_kill_urbs(acm);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800730}
731
732static void acm_tty_cleanup(struct tty_struct *tty)
733{
734 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200735
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800736 tty_port_put(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100737}
738
739static void acm_tty_hangup(struct tty_struct *tty)
740{
741 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200742
Alan Cox10077d42009-06-11 12:36:09 +0100743 tty_port_hangup(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static void acm_tty_close(struct tty_struct *tty, struct file *filp)
747{
748 struct acm *acm = tty->driver_data;
Oliver Neukumab57f862016-09-07 14:30:01 +0200749
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800750 tty_port_close(&acm->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Alan Cox6e47e062009-06-11 12:37:06 +0100753static int acm_tty_write(struct tty_struct *tty,
754 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct acm *acm = tty->driver_data;
757 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200758 unsigned long flags;
759 int wbn;
760 struct acm_wb *wb;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!count)
763 return 0;
764
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200765 dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100766
Oliver Neukum884b6002005-04-21 21:28:02 +0200767 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100768 wbn = acm_wb_alloc(acm);
769 if (wbn < 0) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200770 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200773 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700775 if (!acm->dev) {
776 wb->use = 0;
777 spin_unlock_irqrestore(&acm->write_lock, flags);
778 return -ENODEV;
779 }
780
Oliver Neukum884b6002005-04-21 21:28:02 +0200781 count = (count > acm->writesize) ? acm->writesize : count;
Oliver Neukumefbe27b2016-09-13 16:44:37 +0200782 dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
Oliver Neukum884b6002005-04-21 21:28:02 +0200783 memcpy(wb->buf, buf, count);
784 wb->len = count;
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700785
Johan Hovold183a4502014-05-26 19:23:41 +0200786 stat = usb_autopm_get_interface_async(acm->control);
787 if (stat) {
788 wb->use = 0;
789 spin_unlock_irqrestore(&acm->write_lock, flags);
790 return stat;
791 }
792
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700793 if (acm->susp_count) {
Oliver Neukuma81cf972016-02-10 10:39:49 +0100794 if (acm->putbuffer) {
795 /* now to preserve order */
796 usb_anchor_urb(acm->putbuffer->urb, &acm->delayed);
797 acm->putbuffer = NULL;
798 }
Johan Hovold140cb812014-05-26 19:23:38 +0200799 usb_anchor_urb(wb->urb, &acm->delayed);
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700800 spin_unlock_irqrestore(&acm->write_lock, flags);
Johan Hovold140cb812014-05-26 19:23:38 +0200801 return count;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100802 } else {
803 if (acm->putbuffer) {
804 /* at this point there is no good way to handle errors */
805 acm_start_wb(acm, acm->putbuffer);
806 acm->putbuffer = NULL;
807 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700808 }
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700809
810 stat = acm_start_wb(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200811 spin_unlock_irqrestore(&acm->write_lock, flags);
812
Alan Cox6e47e062009-06-11 12:37:06 +0100813 if (stat < 0)
Oliver Neukum884b6002005-04-21 21:28:02 +0200814 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return count;
816}
817
Oliver Neukuma81cf972016-02-10 10:39:49 +0100818static void acm_tty_flush_chars(struct tty_struct *tty)
819{
820 struct acm *acm = tty->driver_data;
821 struct acm_wb *cur = acm->putbuffer;
822 int err;
823 unsigned long flags;
824
Oliver Neukum2a147592016-04-04 14:30:53 +0200825 if (!cur) /* nothing to do */
826 return;
827
Oliver Neukuma81cf972016-02-10 10:39:49 +0100828 acm->putbuffer = NULL;
829 err = usb_autopm_get_interface_async(acm->control);
830 spin_lock_irqsave(&acm->write_lock, flags);
831 if (err < 0) {
832 cur->use = 0;
Oliver Neukum2a147592016-04-04 14:30:53 +0200833 acm->putbuffer = cur;
Oliver Neukuma81cf972016-02-10 10:39:49 +0100834 goto out;
835 }
836
837 if (acm->susp_count)
838 usb_anchor_urb(cur->urb, &acm->delayed);
839 else
840 acm_start_wb(acm, cur);
841out:
842 spin_unlock_irqrestore(&acm->write_lock, flags);
843 return;
844}
845
846static int acm_tty_put_char(struct tty_struct *tty, unsigned char ch)
847{
848 struct acm *acm = tty->driver_data;
849 struct acm_wb *cur;
850 int wbn;
851 unsigned long flags;
852
853overflow:
854 cur = acm->putbuffer;
855 if (!cur) {
856 spin_lock_irqsave(&acm->write_lock, flags);
857 wbn = acm_wb_alloc(acm);
858 if (wbn >= 0) {
859 cur = &acm->wb[wbn];
860 acm->putbuffer = cur;
861 }
862 spin_unlock_irqrestore(&acm->write_lock, flags);
863 if (!cur)
864 return 0;
865 }
866
867 if (cur->len == acm->writesize) {
868 acm_tty_flush_chars(tty);
869 goto overflow;
870 }
871
872 cur->buf[cur->len++] = ch;
873 return 1;
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static int acm_tty_write_room(struct tty_struct *tty)
877{
878 struct acm *acm = tty->driver_data;
Oliver Neukum884b6002005-04-21 21:28:02 +0200879 /*
880 * Do not let the line discipline to know that we have a reserve,
881 * or it might get too enthusiastic.
882 */
David Brownell934da462008-08-06 18:44:12 -0700883 return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static int acm_tty_chars_in_buffer(struct tty_struct *tty)
887{
888 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800889 /*
890 * if the device was unplugged then any remaining characters fell out
891 * of the connector ;)
892 */
893 if (acm->disconnected)
Alan Cox23198fd2009-07-20 16:05:27 +0100894 return 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200895 /*
896 * This is inaccurate (overcounts), but it works.
897 */
Oliver Neukum86478942006-05-13 22:50:47 +0200898 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static void acm_tty_throttle(struct tty_struct *tty)
902{
903 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100904
Johan Hovold088c64f2011-03-25 11:06:02 +0100905 spin_lock_irq(&acm->read_lock);
906 acm->throttle_req = 1;
907 spin_unlock_irq(&acm->read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static void acm_tty_unthrottle(struct tty_struct *tty)
911{
912 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100913 unsigned int was_throttled;
914
Johan Hovold088c64f2011-03-25 11:06:02 +0100915 spin_lock_irq(&acm->read_lock);
916 was_throttled = acm->throttled;
917 acm->throttled = 0;
918 acm->throttle_req = 0;
919 spin_unlock_irq(&acm->read_lock);
920
921 if (was_throttled)
922 acm_submit_read_urbs(acm, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Alan Cox9e98966c2008-07-22 11:18:03 +0100925static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct acm *acm = tty->driver_data;
Alan Cox9e98966c2008-07-22 11:18:03 +0100928 int retval;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800929
Alan Cox9e98966c2008-07-22 11:18:03 +0100930 retval = acm_send_break(acm, state ? 0xffff : 0);
931 if (retval < 0)
Ladislav Michl90744af82016-11-18 19:01:22 +0100932 dev_dbg(&acm->control->dev,
933 "%s - send break failed\n", __func__);
Alan Cox9e98966c2008-07-22 11:18:03 +0100934 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Alan Cox60b33c12011-02-14 16:26:14 +0000937static int acm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct acm *acm = tty->driver_data;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
942 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
943 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
944 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
945 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
946 TIOCM_CTS;
947}
948
Alan Cox20b9d172011-02-14 16:26:50 +0000949static int acm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 unsigned int set, unsigned int clear)
951{
952 struct acm *acm = tty->driver_data;
953 unsigned int newctrl;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 newctrl = acm->ctrlout;
Alan Cox6e47e062009-06-11 12:37:06 +0100956 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
957 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
958 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
959 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 newctrl = (newctrl & ~clear) | set;
962
963 if (acm->ctrlout == newctrl)
964 return 0;
965 return acm_set_control(acm, acm->ctrlout = newctrl);
966}
967
Oliver Neukum18c75722012-02-17 17:21:24 -0500968static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
969{
970 struct serial_struct tmp;
971
Oliver Neukum18c75722012-02-17 17:21:24 -0500972 memset(&tmp, 0, sizeof(tmp));
Oliver Neukum18c75722012-02-17 17:21:24 -0500973 tmp.xmit_fifo_size = acm->writesize;
974 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600975 tmp.close_delay = acm->port.close_delay / 10;
976 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
977 ASYNC_CLOSING_WAIT_NONE :
978 acm->port.closing_wait / 10;
Oliver Neukum18c75722012-02-17 17:21:24 -0500979
980 if (copy_to_user(info, &tmp, sizeof(tmp)))
981 return -EFAULT;
982 else
983 return 0;
984}
985
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600986static int set_serial_info(struct acm *acm,
987 struct serial_struct __user *newinfo)
988{
989 struct serial_struct new_serial;
990 unsigned int closing_wait, close_delay;
991 int retval = 0;
992
993 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
994 return -EFAULT;
995
996 close_delay = new_serial.close_delay * 10;
997 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
998 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
999
1000 mutex_lock(&acm->port.mutex);
1001
1002 if (!capable(CAP_SYS_ADMIN)) {
1003 if ((close_delay != acm->port.close_delay) ||
1004 (closing_wait != acm->port.closing_wait))
1005 retval = -EPERM;
1006 else
1007 retval = -EOPNOTSUPP;
1008 } else {
1009 acm->port.close_delay = close_delay;
1010 acm->port.closing_wait = closing_wait;
1011 }
1012
1013 mutex_unlock(&acm->port.mutex);
1014 return retval;
1015}
1016
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001017static int wait_serial_change(struct acm *acm, unsigned long arg)
1018{
1019 int rv = 0;
1020 DECLARE_WAITQUEUE(wait, current);
1021 struct async_icount old, new;
1022
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001023 do {
1024 spin_lock_irq(&acm->read_lock);
1025 old = acm->oldcount;
1026 new = acm->iocount;
1027 acm->oldcount = new;
1028 spin_unlock_irq(&acm->read_lock);
1029
1030 if ((arg & TIOCM_DSR) &&
1031 old.dsr != new.dsr)
1032 break;
1033 if ((arg & TIOCM_CD) &&
1034 old.dcd != new.dcd)
1035 break;
1036 if ((arg & TIOCM_RI) &&
1037 old.rng != new.rng)
1038 break;
1039
1040 add_wait_queue(&acm->wioctl, &wait);
1041 set_current_state(TASK_INTERRUPTIBLE);
1042 schedule();
1043 remove_wait_queue(&acm->wioctl, &wait);
1044 if (acm->disconnected) {
1045 if (arg & TIOCM_CD)
1046 break;
1047 else
1048 rv = -ENODEV;
1049 } else {
1050 if (signal_pending(current))
1051 rv = -ERESTARTSYS;
1052 }
1053 } while (!rv);
1054
1055
1056
1057 return rv;
1058}
1059
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001060static int acm_tty_get_icount(struct tty_struct *tty,
1061 struct serial_icounter_struct *icount)
Oliver Neukum797ef132013-11-20 11:35:35 +01001062{
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001063 struct acm *acm = tty->driver_data;
Oliver Neukum797ef132013-11-20 11:35:35 +01001064
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001065 icount->dsr = acm->iocount.dsr;
1066 icount->rng = acm->iocount.rng;
1067 icount->dcd = acm->iocount.dcd;
1068 icount->frame = acm->iocount.frame;
1069 icount->overrun = acm->iocount.overrun;
1070 icount->parity = acm->iocount.parity;
1071 icount->brk = acm->iocount.brk;
Oliver Neukum797ef132013-11-20 11:35:35 +01001072
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001073 return 0;
Oliver Neukum797ef132013-11-20 11:35:35 +01001074}
1075
Alan Cox6caa76b2011-02-14 16:27:22 +00001076static int acm_tty_ioctl(struct tty_struct *tty,
Alan Cox6e47e062009-06-11 12:37:06 +01001077 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Oliver Neukum18c75722012-02-17 17:21:24 -05001079 struct acm *acm = tty->driver_data;
1080 int rv = -ENOIOCTLCMD;
1081
1082 switch (cmd) {
1083 case TIOCGSERIAL: /* gets serial port data */
1084 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
1085 break;
Dan Williamsba2d8ce2012-11-08 12:47:41 -06001086 case TIOCSSERIAL:
1087 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
1088 break;
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001089 case TIOCMIWAIT:
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001090 rv = usb_autopm_get_interface(acm->control);
1091 if (rv < 0) {
1092 rv = -EIO;
1093 break;
1094 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001095 rv = wait_serial_change(acm, arg);
Oliver Neukum8fdbeb22013-11-20 11:35:36 +01001096 usb_autopm_put_interface(acm->control);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001097 break;
Oliver Neukum18c75722012-02-17 17:21:24 -05001098 }
1099
1100 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Alan Cox6e47e062009-06-11 12:37:06 +01001103static void acm_tty_set_termios(struct tty_struct *tty,
1104 struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct acm *acm = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01001107 struct ktermios *termios = &tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct usb_cdc_line_coding newline;
1109 int newctrl = acm->ctrlout;
1110
Alan Cox9b80fee2009-09-19 13:13:23 -07001111 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
1113 newline.bParityType = termios->c_cflag & PARENB ?
Alan Cox6e47e062009-06-11 12:37:06 +01001114 (termios->c_cflag & PARODD ? 1 : 2) +
1115 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
Nicolas Boullis301a29d2012-10-16 00:06:23 +02001116 switch (termios->c_cflag & CSIZE) {
1117 case CS5:
1118 newline.bDataBits = 5;
1119 break;
1120 case CS6:
1121 newline.bDataBits = 6;
1122 break;
1123 case CS7:
1124 newline.bDataBits = 7;
1125 break;
1126 case CS8:
1127 default:
1128 newline.bDataBits = 8;
1129 break;
1130 }
Alan Cox6e47e062009-06-11 12:37:06 +01001131 /* FIXME: Needs to clear unsupported bits in the termios */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
1133
Johan Hovold4473d052014-11-05 18:41:59 +01001134 if (C_BAUD(tty) == B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 newline.dwDTERate = acm->line.dwDTERate;
1136 newctrl &= ~ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001137 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001138 newctrl |= ACM_CTRL_DTR;
Johan Hovold4473d052014-11-05 18:41:59 +01001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (newctrl != acm->ctrlout)
1142 acm_set_control(acm, acm->ctrlout = newctrl);
1143
1144 if (memcmp(&acm->line, &newline, sizeof newline)) {
1145 memcpy(&acm->line, &newline, sizeof newline);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001146 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
1147 __func__,
1148 le32_to_cpu(newline.dwDTERate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 newline.bCharFormat, newline.bParityType,
1150 newline.bDataBits);
1151 acm_set_line(acm, &acm->line);
1152 }
1153}
1154
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001155static const struct tty_port_operations acm_port_ops = {
Johan Hovold0943d8e2014-05-26 19:23:51 +02001156 .dtr_rts = acm_port_dtr_rts,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001157 .shutdown = acm_port_shutdown,
1158 .activate = acm_port_activate,
1159 .destruct = acm_port_destruct,
1160};
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/*
1163 * USB probe and disconnect routines.
1164 */
1165
Oliver Neukum830f4022008-06-25 14:17:16 +02001166/* Little helpers: write/read buffers free */
Oliver Neukum884b6002005-04-21 21:28:02 +02001167static void acm_write_buffers_free(struct acm *acm)
1168{
1169 int i;
1170 struct acm_wb *wb;
1171
Alan Cox6e47e062009-06-11 12:37:06 +01001172 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
Ladislav Michle4614602016-11-18 19:06:10 +01001173 usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
Oliver Neukum884b6002005-04-21 21:28:02 +02001174}
1175
Oliver Neukum830f4022008-06-25 14:17:16 +02001176static void acm_read_buffers_free(struct acm *acm)
1177{
Johan Hovolddab54c92011-03-22 11:12:21 +01001178 int i;
Oliver Neukum830f4022008-06-25 14:17:16 +02001179
Johan Hovolddab54c92011-03-22 11:12:21 +01001180 for (i = 0; i < acm->rx_buflimit; i++)
Ladislav Michle4614602016-11-18 19:06:10 +01001181 usb_free_coherent(acm->dev, acm->readsize,
Johan Hovold088c64f2011-03-25 11:06:02 +01001182 acm->read_buffers[i].base, acm->read_buffers[i].dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001183}
1184
Oliver Neukum884b6002005-04-21 21:28:02 +02001185/* Little helper: write buffers allocate */
1186static int acm_write_buffers_alloc(struct acm *acm)
1187{
1188 int i;
1189 struct acm_wb *wb;
1190
Oliver Neukum86478942006-05-13 22:50:47 +02001191 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Daniel Mack997ea582010-04-12 13:17:25 +02001192 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
Oliver Neukum884b6002005-04-21 21:28:02 +02001193 &wb->dmah);
1194 if (!wb->buf) {
1195 while (i != 0) {
1196 --i;
1197 --wb;
Daniel Mack997ea582010-04-12 13:17:25 +02001198 usb_free_coherent(acm->dev, acm->writesize,
Oliver Neukum884b6002005-04-21 21:28:02 +02001199 wb->buf, wb->dmah);
1200 }
1201 return -ENOMEM;
1202 }
1203 }
1204 return 0;
1205}
1206
Alan Cox10077d42009-06-11 12:36:09 +01001207static int acm_probe(struct usb_interface *intf,
1208 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001211 struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
David Brownellc6dbf552008-04-13 14:00:44 -07001212 unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int buflen = intf->altsetting->extralen;
1214 struct usb_interface *control_interface;
1215 struct usb_interface *data_interface;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001216 struct usb_endpoint_descriptor *epctrl = NULL;
1217 struct usb_endpoint_descriptor *epread = NULL;
1218 struct usb_endpoint_descriptor *epwrite = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct usb_device *usb_dev = interface_to_usbdev(intf);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001220 struct usb_cdc_parsed_header h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct acm *acm;
1222 int minor;
Alan Cox6e47e062009-06-11 12:37:06 +01001223 int ctrlsize, readsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 u8 *buf;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001225 int call_intf_num = -1;
1226 int data_intf_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +02001228 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +01001229 int i;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001230 int combined_interfaces = 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001231 struct device *tty_dev;
1232 int rv = -ENOMEM;
Johan Hovoldf8d84642017-03-17 11:35:48 +01001233 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Oliver Neukum86478942006-05-13 22:50:47 +02001235 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 quirks = (unsigned long)id->driver_info;
Dmitry Torokhov16142652013-02-02 11:02:14 -08001237
1238 if (quirks == IGNORE_DEVICE)
1239 return -ENODEV;
1240
Oliver Neukum7309aa82016-11-02 14:42:52 +01001241 memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
1242
Oliver Neukum86478942006-05-13 22:50:47 +02001243 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
1244
1245 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (quirks == NO_UNION_NORMAL) {
1247 data_interface = usb_ifnum_to_if(usb_dev, 1);
1248 control_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukum8835ba42016-03-15 10:14:04 +01001249 /* we would crash */
1250 if (!data_interface || !control_interface)
1251 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto skip_normal_probe;
1253 }
Alan Cox6e47e062009-06-11 12:37:06 +01001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* normal probing*/
1256 if (!buffer) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001257 dev_err(&intf->dev, "Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EINVAL;
1259 }
1260
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001261 if (!intf->cur_altsetting)
1262 return -EINVAL;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!buflen) {
Toby Gray577045c2010-09-02 10:46:20 +01001265 if (intf->cur_altsetting->endpoint &&
1266 intf->cur_altsetting->endpoint->extralen &&
Alan Cox6e47e062009-06-11 12:37:06 +01001267 intf->cur_altsetting->endpoint->extra) {
1268 dev_dbg(&intf->dev,
1269 "Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 buflen = intf->cur_altsetting->endpoint->extralen;
1271 buffer = intf->cur_altsetting->endpoint->extra;
1272 } else {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001273 dev_err(&intf->dev,
1274 "Zero length descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EINVAL;
1276 }
1277 }
1278
Oliver Neukumcb42b632016-07-14 15:41:34 +02001279 cdc_parse_cdc_header(&h, intf, buffer, buflen);
1280 union_header = h.usb_cdc_union_desc;
1281 cmgmd = h.usb_cdc_call_mgmt_descriptor;
1282 if (cmgmd)
1283 call_intf_num = cmgmd->bDataInterface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!union_header) {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001286 if (call_intf_num > 0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001287 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
Erik Slagterfd5054c12011-05-11 12:06:55 +02001288 /* quirks for Droids MuIn LCD */
Oliver Neukumcb42b632016-07-14 15:41:34 +02001289 if (quirks & NO_DATA_INTERFACE) {
Erik Slagterfd5054c12011-05-11 12:06:55 +02001290 data_interface = usb_ifnum_to_if(usb_dev, 0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001291 } else {
1292 data_intf_num = call_intf_num;
1293 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 control_interface = intf;
1296 } else {
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001297 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1298 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1299 return -ENODEV;
1300 } else {
1301 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1302 combined_interfaces = 1;
1303 control_interface = data_interface = intf;
1304 goto look_for_collapsed_interface;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 } else {
Oliver Neukumcb42b632016-07-14 15:41:34 +02001308 data_intf_num = union_header->bSlaveInterface0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
Oliver Neukumcb42b632016-07-14 15:41:34 +02001310 data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
Greg Kroah-Hartman403dff42014-11-07 08:48:15 -08001311 }
1312
1313 if (!control_interface || !data_interface) {
1314 dev_dbg(&intf->dev, "no interfaces\n");
1315 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Oliver Neukum2ad9d542016-09-20 15:45:42 +02001317 if (!data_interface->cur_altsetting || !control_interface->cur_altsetting)
1318 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001319
Oliver Neukumcb42b632016-07-14 15:41:34 +02001320 if (data_intf_num != call_intf_num)
Alan Cox6e47e062009-06-11 12:37:06 +01001321 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001323 if (control_interface == data_interface) {
1324 /* some broken devices designed for windows work this way */
1325 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1326 combined_interfaces = 1;
1327 /* a popular other OS doesn't use it */
1328 quirks |= NO_CAP_LINE;
1329 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1330 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1331 return -EINVAL;
1332 }
1333look_for_collapsed_interface:
Johan Hovoldf8d84642017-03-17 11:35:48 +01001334 res = usb_find_common_endpoints(data_interface->cur_altsetting,
1335 &epread, &epwrite, &epctrl, NULL);
1336 if (res)
1337 return res;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001338
Johan Hovoldf8d84642017-03-17 11:35:48 +01001339 goto made_compressed_probe;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001340 }
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342skip_normal_probe:
1343
1344 /*workaround for switched interfaces */
Alan Cox6e47e062009-06-11 12:37:06 +01001345 if (data_interface->cur_altsetting->desc.bInterfaceClass
1346 != CDC_DATA_INTERFACE_TYPE) {
1347 if (control_interface->cur_altsetting->desc.bInterfaceClass
1348 == CDC_DATA_INTERFACE_TYPE) {
Alan Cox6e47e062009-06-11 12:37:06 +01001349 dev_dbg(&intf->dev,
1350 "Your device has switched interfaces.\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001351 swap(control_interface, data_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 } else {
1353 return -EINVAL;
1354 }
1355 }
Alan Stern74da5d62007-08-02 13:29:10 -04001356
1357 /* Accept probe requests only for the control interface */
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001358 if (!combined_interfaces && intf != control_interface)
Alan Stern74da5d62007-08-02 13:29:10 -04001359 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001360
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001361 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1362 /* valid in this context */
Alan Cox6e47e062009-06-11 12:37:06 +01001363 dev_dbg(&intf->dev, "The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EBUSY;
1365 }
1366
1367
Sven Schnelle99f347c2012-08-17 21:43:43 +02001368 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1369 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
1371
1372 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1373 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1374 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1375
1376
1377 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -03001378 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* descriptors are swapped */
Alan Cox6e47e062009-06-11 12:37:06 +01001380 dev_dbg(&intf->dev,
1381 "The data interface has switched endpoints\n");
Fabian Frederick2cfef792015-05-18 19:59:37 +02001382 swap(epread, epwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001384made_compressed_probe:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001385 dev_dbg(&intf->dev, "interfaces are valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Alan Cox6e47e062009-06-11 12:37:06 +01001387 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001388 if (acm == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001391 minor = acm_alloc_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001392 if (minor < 0)
1393 goto alloc_fail1;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001394
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001395 ctrlsize = usb_endpoint_maxp(epctrl);
1396 readsize = usb_endpoint_maxp(epread) *
Alan Cox6e47e062009-06-11 12:37:06 +01001397 (quirks == SINGLE_RX_URB ? 1 : 2);
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001398 acm->combined_interfaces = combined_interfaces;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001399 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 acm->control = control_interface;
1401 acm->data = data_interface;
1402 acm->minor = minor;
1403 acm->dev = usb_dev;
Oliver Neukumcb42b632016-07-14 15:41:34 +02001404 if (h.usb_cdc_acm_descriptor)
1405 acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001406 if (quirks & NO_CAP_LINE)
1407 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 acm->ctrlsize = ctrlsize;
1409 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +02001410 acm->rx_buflimit = num_rx_buf;
David Howellsc4028952006-11-22 14:57:56 +00001411 INIT_WORK(&acm->work, acm_softint);
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001412 init_waitqueue_head(&acm->wioctl);
Oliver Neukum884b6002005-04-21 21:28:02 +02001413 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +01001414 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001415 mutex_init(&acm->mutex);
Ladislav Michld3053942016-11-18 19:10:19 +01001416 if (usb_endpoint_xfer_int(epread)) {
Oliver Neukumcf7fdd52009-08-04 23:52:09 +02001417 acm->bInterval = epread->bInterval;
Ladislav Michl74bccc92016-11-18 19:09:19 +01001418 acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
1419 } else {
1420 acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1421 }
1422 if (usb_endpoint_xfer_int(epwrite))
1423 acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
1424 else
1425 acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
Alan Cox739e0282009-06-11 12:27:50 +01001426 tty_port_init(&acm->port);
1427 acm->port.ops = &acm_port_ops;
Johan Hovold140cb812014-05-26 19:23:38 +02001428 init_usb_anchor(&acm->delayed);
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001429 acm->quirks = quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Daniel Mack997ea582010-04-12 13:17:25 +02001431 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001432 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto alloc_fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 acm->ctrl_buffer = buf;
1435
Oliver Neukum17136d42015-01-28 16:56:24 +01001436 if (acm_write_buffers_alloc(acm) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto alloc_fail4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001440 if (!acm->ctrlurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto alloc_fail5;
Oliver Neukum17136d42015-01-28 16:56:24 +01001442
Oliver Neukum86478942006-05-13 22:50:47 +02001443 for (i = 0; i < num_rx_buf; i++) {
Johan Hovold088c64f2011-03-25 11:06:02 +01001444 struct acm_rb *rb = &(acm->read_buffers[i]);
1445 struct urb *urb;
David Kubicek61a87ad2005-11-01 18:51:34 +01001446
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001447 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1448 &rb->dma);
Oliver Neukum17136d42015-01-28 16:56:24 +01001449 if (!rb->base)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001450 goto alloc_fail6;
Johan Hovold088c64f2011-03-25 11:06:02 +01001451 rb->index = i;
1452 rb->instance = acm;
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001453
Johan Hovold088c64f2011-03-25 11:06:02 +01001454 urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001455 if (!urb)
Axel Linc2572b72010-05-31 08:04:47 +08001456 goto alloc_fail6;
Oliver Neukum17136d42015-01-28 16:56:24 +01001457
Johan Hovold088c64f2011-03-25 11:06:02 +01001458 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1459 urb->transfer_dma = rb->dma;
Ladislav Michld3053942016-11-18 19:10:19 +01001460 if (usb_endpoint_xfer_int(epread))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001461 usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001462 acm->readsize,
1463 acm_read_bulk_callback, rb,
1464 acm->bInterval);
Ladislav Michl74bccc92016-11-18 19:09:19 +01001465 else
1466 usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
Johan Hovold088c64f2011-03-25 11:06:02 +01001467 acm->readsize,
1468 acm_read_bulk_callback, rb);
David Kubicek61a87ad2005-11-01 18:51:34 +01001469
Johan Hovold088c64f2011-03-25 11:06:02 +01001470 acm->read_urbs[i] = urb;
1471 __set_bit(i, &acm->read_urbs_free);
David Kubicek61a87ad2005-11-01 18:51:34 +01001472 }
Alan Cox6e47e062009-06-11 12:37:06 +01001473 for (i = 0; i < ACM_NW; i++) {
David Engrafe4cf3aa2008-03-20 10:01:34 +01001474 struct acm_wb *snd = &(acm->wb[i]);
1475
Alan Cox6e47e062009-06-11 12:37:06 +01001476 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum17136d42015-01-28 16:56:24 +01001477 if (snd->urb == NULL)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001478 goto alloc_fail7;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001479
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001480 if (usb_endpoint_xfer_int(epwrite))
Ladislav Michl74bccc92016-11-18 19:09:19 +01001481 usb_fill_int_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001482 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1483 else
Ladislav Michl74bccc92016-11-18 19:09:19 +01001484 usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001485 NULL, acm->writesize, acm_write_bulk, snd);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001486 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Lu Baoluffdb1e32016-01-06 15:10:04 +08001487 if (quirks & SEND_ZERO_PACKET)
1488 snd->urb->transfer_flags |= URB_ZERO_PACKET;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001489 snd->instance = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
Alan Cox6e47e062009-06-11 12:37:06 +01001492 usb_set_intfdata(intf, acm);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001493
1494 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1495 if (i < 0)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001496 goto alloc_fail7;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001497
Oliver Neukumcb42b632016-07-14 15:41:34 +02001498 if (h.usb_cdc_country_functional_desc) { /* export the country data */
1499 struct usb_cdc_country_functional_desc * cfd =
1500 h.usb_cdc_country_functional_desc;
1501
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001502 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1503 if (!acm->country_codes)
1504 goto skip_countries;
1505 acm->country_code_size = cfd->bLength - 4;
Alan Cox6e47e062009-06-11 12:37:06 +01001506 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1507 cfd->bLength - 4);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001508 acm->country_rel_date = cfd->iCountryCodeRelDate;
1509
1510 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1511 if (i < 0) {
1512 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001513 acm->country_codes = NULL;
1514 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001515 goto skip_countries;
1516 }
1517
Alan Cox6e47e062009-06-11 12:37:06 +01001518 i = device_create_file(&intf->dev,
1519 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001520 if (i < 0) {
Axel Linc2572b72010-05-31 08:04:47 +08001521 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001522 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001523 acm->country_codes = NULL;
1524 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001525 goto skip_countries;
1526 }
1527 }
1528
1529skip_countries:
Alan Cox6e47e062009-06-11 12:37:06 +01001530 usb_fill_int_urb(acm->ctrlurb, usb_dev,
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001531 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1532 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1533 /* works around buggy devices */
Felipe Balbid102e782013-07-01 11:23:24 +03001534 epctrl->bInterval ? epctrl->bInterval : 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1536 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
Tobias Herzogea258352017-03-30 22:15:11 +02001537 acm->notification_buffer = NULL;
1538 acm->nb_index = 0;
1539 acm->nb_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 acm->line.dwDTERate = cpu_to_le32(9600);
1544 acm->line.bDataBits = 8;
1545 acm_set_line(acm, &acm->line);
1546
1547 usb_driver_claim_interface(&acm_driver, data_interface, acm);
David Brownell672c4e12008-08-06 18:41:12 -07001548 usb_set_intfdata(data_interface, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001550 usb_get_intf(control_interface);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001551 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
Jiri Slaby734cc172012-08-07 21:47:47 +02001552 &control_interface->dev);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001553 if (IS_ERR(tty_dev)) {
1554 rv = PTR_ERR(tty_dev);
1555 goto alloc_fail8;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001558 if (quirks & CLEAR_HALT_CONDITIONS) {
Ladislav Michl74bccc92016-11-18 19:09:19 +01001559 usb_clear_halt(usb_dev, acm->in);
1560 usb_clear_halt(usb_dev, acm->out);
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001561 }
1562
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001563 return 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001564alloc_fail8:
1565 if (acm->country_codes) {
1566 device_remove_file(&acm->control->dev,
1567 &dev_attr_wCountryCodes);
1568 device_remove_file(&acm->control->dev,
1569 &dev_attr_iCountryCodeRelDate);
Oliver Neukumd908f842014-11-20 14:54:35 +01001570 kfree(acm->country_codes);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001571 }
1572 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001573alloc_fail7:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001574 usb_set_intfdata(intf, NULL);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001575 for (i = 0; i < ACM_NW; i++)
1576 usb_free_urb(acm->wb[i].urb);
Axel Linc2572b72010-05-31 08:04:47 +08001577alloc_fail6:
Oliver Neukum86478942006-05-13 22:50:47 +02001578 for (i = 0; i < num_rx_buf; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +01001579 usb_free_urb(acm->read_urbs[i]);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001580 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 usb_free_urb(acm->ctrlurb);
1582alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001583 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584alloc_fail4:
Daniel Mack997ea582010-04-12 13:17:25 +02001585 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586alloc_fail2:
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001587 acm_release_minor(acm);
Oliver Neukum6dd35872016-07-14 15:41:32 +02001588alloc_fail1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 kfree(acm);
1590alloc_fail:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001591 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594static void acm_disconnect(struct usb_interface *intf)
1595{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001596 struct acm *acm = usb_get_intfdata(intf);
Alan Cox10077d42009-06-11 12:36:09 +01001597 struct tty_struct *tty;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001598
David Brownell672c4e12008-08-06 18:41:12 -07001599 /* sibling interface is already cleaning up */
1600 if (!acm)
Oliver Neukum86067eea2006-01-08 12:39:13 +01001601 return;
David Brownell672c4e12008-08-06 18:41:12 -07001602
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001603 mutex_lock(&acm->mutex);
1604 acm->disconnected = true;
Alan Cox6e47e062009-06-11 12:37:06 +01001605 if (acm->country_codes) {
Alan Stern74da5d62007-08-02 13:29:10 -04001606 device_remove_file(&acm->control->dev,
1607 &dev_attr_wCountryCodes);
1608 device_remove_file(&acm->control->dev,
1609 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001610 }
Oliver Neukum5a6a62b2013-11-20 11:35:34 +01001611 wake_up_all(&acm->wioctl);
Alan Stern74da5d62007-08-02 13:29:10 -04001612 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001613 usb_set_intfdata(acm->control, NULL);
1614 usb_set_intfdata(acm->data, NULL);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001615 mutex_unlock(&acm->mutex);
1616
1617 tty = tty_port_tty_get(&acm->port);
1618 if (tty) {
1619 tty_vhangup(tty);
1620 tty_kref_put(tty);
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Ladislav Michlba8c9312016-11-18 19:07:08 +01001623 acm_kill_urbs(acm);
1624 cancel_work_sync(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Johan Hovoldcb255052013-03-19 09:21:06 +01001626 tty_unregister_device(acm_tty_driver, acm->minor);
1627
Oliver Neukum884b6002005-04-21 21:28:02 +02001628 acm_write_buffers_free(acm);
Ladislav Michle4614602016-11-18 19:06:10 +01001629 usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001630 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Tobias Herzogea258352017-03-30 22:15:11 +02001632 kfree(acm->notification_buffer);
1633
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001634 if (!acm->combined_interfaces)
1635 usb_driver_release_interface(&acm_driver, intf == acm->control ?
Oliver Neukum830f4022008-06-25 14:17:16 +02001636 acm->data : acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001638 tty_port_put(&acm->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Oliver Neukum35758582008-07-01 19:10:08 +02001641#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001642static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1643{
1644 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001645 int cnt;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001646
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001647 spin_lock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001648 if (PMSG_IS_AUTO(message)) {
1649 if (acm->transmitting) {
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001650 spin_unlock_irq(&acm->write_lock);
Johan Hovold5a345c22014-05-26 19:23:36 +02001651 return -EBUSY;
1652 }
1653 }
Oliver Neukum11ea8592008-06-20 11:25:57 +02001654 cnt = acm->susp_count++;
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001655 spin_unlock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001656
1657 if (cnt)
Oliver Neukum1365baf72007-10-12 17:24:28 +02001658 return 0;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001659
Ladislav Michlba8c9312016-11-18 19:07:08 +01001660 acm_kill_urbs(acm);
1661 cancel_work_sync(&acm->work);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001662
Oliver Neukum1365baf72007-10-12 17:24:28 +02001663 return 0;
1664}
1665
1666static int acm_resume(struct usb_interface *intf)
1667{
1668 struct acm *acm = usb_get_intfdata(intf);
Johan Hovold140cb812014-05-26 19:23:38 +02001669 struct urb *urb;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001670 int rv = 0;
1671
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001672 spin_lock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001673
Johan Hovolde144ed282014-05-26 19:23:37 +02001674 if (--acm->susp_count)
1675 goto out;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001676
Peter Hurleyd41861c2016-04-09 17:53:25 -07001677 if (tty_port_initialized(&acm->port)) {
Johan Hovolde144ed282014-05-26 19:23:37 +02001678 rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001679
Johan Hovold140cb812014-05-26 19:23:38 +02001680 for (;;) {
1681 urb = usb_get_from_anchor(&acm->delayed);
1682 if (!urb)
1683 break;
1684
1685 acm_start_wb(acm, urb->context);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001686 }
1687
1688 /*
1689 * delayed error checking because we must
1690 * do the write path at all cost
1691 */
Oliver Neukum1365baf72007-10-12 17:24:28 +02001692 if (rv < 0)
Johan Hovolde144ed282014-05-26 19:23:37 +02001693 goto out;
Oliver Neukum1365baf72007-10-12 17:24:28 +02001694
Johan Hovolde144ed282014-05-26 19:23:37 +02001695 rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001696 }
Johan Hovolde144ed282014-05-26 19:23:37 +02001697out:
Johan Hovoldbbf0cb32014-05-26 19:23:46 +02001698 spin_unlock_irq(&acm->write_lock);
Oliver Neukum1365baf72007-10-12 17:24:28 +02001699
Oliver Neukum1365baf72007-10-12 17:24:28 +02001700 return rv;
1701}
Oliver Neukum35758582008-07-01 19:10:08 +02001702
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001703static int acm_reset_resume(struct usb_interface *intf)
1704{
1705 struct acm *acm = usb_get_intfdata(intf);
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001706
Peter Hurleyd41861c2016-04-09 17:53:25 -07001707 if (tty_port_initialized(&acm->port))
Jiri Slabyaa27a092013-03-07 13:12:30 +01001708 tty_port_tty_hangup(&acm->port, false);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001709
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001710 return acm_resume(intf);
1711}
1712
Oliver Neukum35758582008-07-01 19:10:08 +02001713#endif /* CONFIG_PM */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001714
Ladislav Michl1aba5792016-11-18 19:11:26 +01001715static int acm_pre_reset(struct usb_interface *intf)
1716{
1717 struct acm *acm = usb_get_intfdata(intf);
1718
1719 clear_bit(EVENT_RX_STALL, &acm->flags);
1720
1721 return 0;
1722}
1723
Adrian Taylorc1479a92009-11-19 10:35:33 +00001724#define NOKIA_PCSUITE_ACM_INFO(x) \
1725 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1726 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1727 USB_CDC_ACM_PROTO_VENDOR)
1728
Toby Gray4035e452010-09-01 16:01:19 +01001729#define SAMSUNG_PCSUITE_ACM_INFO(x) \
1730 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1731 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1732 USB_CDC_ACM_PROTO_VENDOR)
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734/*
1735 * USB driver structure.
1736 */
1737
Németh Márton6ef48522010-01-10 15:33:45 +01001738static const struct usb_device_id acm_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* quirky and broken devices */
Björn Gerhartb20b1612015-02-18 07:19:44 +01001740 { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
1741 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
David Cluytens3b59d162013-12-03 14:18:57 +01001742 { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
1743 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1745 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1746 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001747 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1748 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1749 },
Andrew Lunn0f9c7b42008-12-23 17:31:23 +01001750 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1751 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1752 },
Masahito Omote8753e652005-07-29 12:17:25 -07001753 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1754 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1755 },
Chris Malley91a9c922006-10-03 10:08:28 +01001756 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1757 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1758 },
Alan Cox7abcf202009-04-06 17:35:01 +01001759 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1760 .driver_info = SINGLE_RX_URB,
1761 },
Oliver Neukum86478942006-05-13 22:50:47 +02001762 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1763 .driver_info = SINGLE_RX_URB, /* firmware bug */
1764 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001765 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1766 .driver_info = SINGLE_RX_URB, /* firmware bug */
1767 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001768 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1769 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1770 },
Iain McFarlane6149ed52008-05-04 00:13:49 +01001771 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1772 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1773 },
Eric Sandeenc8fd2c32008-08-14 08:25:40 -05001774 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1775 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1776 },
Alan Coxc89c60e2009-01-11 19:53:10 +00001777 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1778 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1779 },
Xiao Kaijiancab98a02009-05-08 00:48:23 +08001780 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1781 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1782 },
Johan Hovold2a8cdfd2014-11-06 18:08:33 +01001783 { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
1784 .driver_info = QUIRK_CONTROL_LINE_STATE, },
Johan Hovoldcf84a692014-10-27 18:34:33 +01001785 { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
Nathaniel Quillin30121602016-12-05 06:53:00 -08001786 { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
Dmitriy Taychenachev155df652009-02-25 12:36:51 +08001787 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1788 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001789 /* Motorola H24 HSPA module: */
1790 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
Michael Ulbricht895d2402014-03-25 10:34:18 +01001791 { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
1792 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1793 },
1794 { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
1795 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1796 },
1797 { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
1798 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1799 },
1800 { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
1801 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1802 },
1803 { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
1804 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1805 },
1806 { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
1807 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1808 },
1809 { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
1810 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1811 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001812
Adam Richterc332b4e2009-02-18 16:17:15 -08001813 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1814 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1815 data interface instead of
1816 communications interface.
1817 Maybe we should define a new
1818 quirk for this. */
1819 },
Jean-Christian de Rivaze7d491a2012-10-10 12:49:02 +00001820 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1821 .driver_info = NO_UNION_NORMAL,
1822 },
Denis N Ladin036915a2012-12-26 18:29:44 +05001823 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1824 .driver_info = NO_UNION_NORMAL,
1825 },
Kir Kolyshkin1f17c502009-05-28 20:33:58 +04001826 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1827 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1828 },
Russ Nelsonc3baa192010-04-21 23:07:03 -04001829 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1830 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1831 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001832
Alexey Sokolov15bf7222015-06-02 11:49:30 +03001833 { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
1834 .driver_info = CLEAR_HALT_CONDITIONS,
1835 },
1836
Adrian Taylorc1479a92009-11-19 10:35:33 +00001837 /* Nokia S60 phones expose two ACM channels. The first is
1838 * a modem and is picked up by the standard AT-command
1839 * information below. The second is 'vendor-specific' but
1840 * is treated as a serial device at the S60 end, so we want
1841 * to expose it on Linux too. */
1842 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1843 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1844 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1845 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1846 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1847 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1848 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1849 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1850 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1851 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1852 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1853 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1854 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1855 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1856 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1857 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1858 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1859 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1860 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1861 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1862 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1863 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1864 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1865 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1866 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1867 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1868 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1869 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1870 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1871 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1872 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1873 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1874 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1875 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1876 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1877 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1878 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1879 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1880 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1881 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1882 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1883 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1884 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
Przemo Firszt83a4eae2010-06-28 21:29:34 +01001885 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
Toby Gray4035e452010-09-01 16:01:19 +01001886 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1887 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1888 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1889 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1890 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1891 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1892 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1893 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1894 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1895 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
Arvid Ephraim Picciani721d92f2011-01-25 15:58:40 +01001896 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
Toby Gray4061fde2011-06-06 14:52:48 +01001897 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1898 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
Toby Gray4035e452010-09-01 16:01:19 +01001899 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001900
Denis Pershin65e52f42011-09-04 17:37:21 +07001901 /* Support for Owen devices */
1902 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1903
Adrian Taylorc1479a92009-11-19 10:35:33 +00001904 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1905
Erik Slagterfd5054c12011-05-11 12:06:55 +02001906 /* Support for Droids MuIn LCD */
1907 { USB_DEVICE(0x04d8, 0x000b),
1908 .driver_info = NO_DATA_INTERFACE,
1909 },
1910
Dmitry Torokhov16142652013-02-02 11:02:14 -08001911#if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1912 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1913 .driver_info = IGNORE_DEVICE,
1914 },
1915 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1916 .driver_info = IGNORE_DEVICE,
1917 },
1918#endif
1919
Oliver Neukume912e682016-01-18 15:45:18 +01001920 /*Samsung phone in firmware update mode */
1921 { USB_DEVICE(0x04e8, 0x685d),
1922 .driver_info = IGNORE_DEVICE,
1923 },
1924
Jonas Jonssonf33a7f72015-11-22 11:47:17 +01001925 /* Exclude Infineon Flash Loader utility */
1926 { USB_DEVICE(0x058b, 0x0041),
1927 .driver_info = IGNORE_DEVICE,
1928 },
1929
Philippe Corbes5b239f02010-08-31 19:31:32 +02001930 /* control interfaces without any protocol set */
1931 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1932 USB_CDC_PROTO_NONE) },
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* control interfaces with various AT-command sets */
1935 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1936 USB_CDC_ACM_PROTO_AT_V25TER) },
1937 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1938 USB_CDC_ACM_PROTO_AT_PCCA101) },
1939 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1940 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1941 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1942 USB_CDC_ACM_PROTO_AT_GSM) },
1943 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
Alan Cox6e47e062009-06-11 12:37:06 +01001944 USB_CDC_ACM_PROTO_AT_3G) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1946 USB_CDC_ACM_PROTO_AT_CDMA) },
1947
Lu Baoluffdb1e32016-01-06 15:10:04 +08001948 { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
1949 .driver_info = SEND_ZERO_PACKET,
1950 },
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 { }
1953};
1954
Alan Cox6e47e062009-06-11 12:37:06 +01001955MODULE_DEVICE_TABLE(usb, acm_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 .name = "cdc_acm",
1959 .probe = acm_probe,
1960 .disconnect = acm_disconnect,
Oliver Neukum35758582008-07-01 19:10:08 +02001961#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001962 .suspend = acm_suspend,
1963 .resume = acm_resume,
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001964 .reset_resume = acm_reset_resume,
Oliver Neukum35758582008-07-01 19:10:08 +02001965#endif
Ladislav Michl1aba5792016-11-18 19:11:26 +01001966 .pre_reset = acm_pre_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 .id_table = acm_ids,
Oliver Neukum35758582008-07-01 19:10:08 +02001968#ifdef CONFIG_PM
Oliver Neukum1365baf72007-10-12 17:24:28 +02001969 .supports_autosuspend = 1,
Oliver Neukum35758582008-07-01 19:10:08 +02001970#endif
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001971 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972};
1973
1974/*
1975 * TTY driver structures.
1976 */
1977
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001978static const struct tty_operations acm_ops = {
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001979 .install = acm_tty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 .open = acm_tty_open,
1981 .close = acm_tty_close,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001982 .cleanup = acm_tty_cleanup,
Alan Cox10077d42009-06-11 12:36:09 +01001983 .hangup = acm_tty_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 .write = acm_tty_write,
Oliver Neukuma81cf972016-02-10 10:39:49 +01001985 .put_char = acm_tty_put_char,
1986 .flush_chars = acm_tty_flush_chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .write_room = acm_tty_write_room,
1988 .ioctl = acm_tty_ioctl,
1989 .throttle = acm_tty_throttle,
1990 .unthrottle = acm_tty_unthrottle,
1991 .chars_in_buffer = acm_tty_chars_in_buffer,
1992 .break_ctl = acm_tty_break_ctl,
1993 .set_termios = acm_tty_set_termios,
1994 .tiocmget = acm_tty_tiocmget,
1995 .tiocmset = acm_tty_tiocmset,
Johan Hovoldbb2d3872016-11-08 13:28:24 +01001996 .get_icount = acm_tty_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997};
1998
1999/*
2000 * Init / exit.
2001 */
2002
2003static int __init acm_init(void)
2004{
2005 int retval;
2006 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
2007 if (!acm_tty_driver)
2008 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 acm_tty_driver->driver_name = "acm",
2010 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 acm_tty_driver->major = ACM_TTY_MAJOR,
2012 acm_tty_driver->minor_start = 0,
2013 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
2014 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07002015 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 acm_tty_driver->init_termios = tty_std_termios;
Alan Cox6e47e062009-06-11 12:37:06 +01002017 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
2018 HUPCL | CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 tty_set_operations(acm_tty_driver, &acm_ops);
2020
2021 retval = tty_register_driver(acm_tty_driver);
2022 if (retval) {
2023 put_tty_driver(acm_tty_driver);
2024 return retval;
2025 }
2026
2027 retval = usb_register(&acm_driver);
2028 if (retval) {
2029 tty_unregister_driver(acm_tty_driver);
2030 put_tty_driver(acm_tty_driver);
2031 return retval;
2032 }
2033
Johan Hovolda2c7b932011-03-22 11:12:18 +01002034 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 return 0;
2037}
2038
2039static void __exit acm_exit(void)
2040{
2041 usb_deregister(&acm_driver);
2042 tty_unregister_driver(acm_tty_driver);
2043 put_tty_driver(acm_tty_driver);
Johannes Thumshirn91b72562015-07-08 17:25:42 +02002044 idr_destroy(&acm_minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047module_init(acm_init);
2048module_exit(acm_exit);
2049
Alan Cox6e47e062009-06-11 12:37:06 +01002050MODULE_AUTHOR(DRIVER_AUTHOR);
2051MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052MODULE_LICENSE("GPL");
Scott James Remnante766aeb2009-04-06 17:33:18 +01002053MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);