blob: 3e7560f004f86d7153ed3ea0eeabed09dabf6b47 [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>
35#include <linux/errno.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/tty.h>
Oliver Neukum7af25b42009-09-08 23:51:28 +020039#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/tty_driver.h>
41#include <linux/tty_flip.h>
42#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010043#include <linux/mutex.h>
Alan Cox10077d42009-06-11 12:36:09 +010044#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070046#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/byteorder.h>
48#include <asm/unaligned.h>
David Kubicek61a87ad2005-11-01 18:51:34 +010049#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "cdc-acm.h"
52
David Brownelle5fbab52008-08-06 18:46:10 -070053
Johan Hovold088c64f2011-03-25 11:06:02 +010054#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
56
57static struct usb_driver acm_driver;
58static struct tty_driver *acm_tty_driver;
59static struct acm *acm_table[ACM_TTY_MINORS];
60
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080061static DEFINE_MUTEX(acm_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080063/*
64 * acm_table accessors
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -080067/*
68 * Look up an ACM structure by index. If found and not disconnected, increment
69 * its refcount and return it with its mutex held.
70 */
71static struct acm *acm_get_by_index(unsigned index)
72{
73 struct acm *acm;
74
75 mutex_lock(&acm_table_lock);
76 acm = acm_table[index];
77 if (acm) {
78 mutex_lock(&acm->mutex);
79 if (acm->disconnected) {
80 mutex_unlock(&acm->mutex);
81 acm = NULL;
82 } else {
83 tty_port_get(&acm->port);
84 mutex_unlock(&acm->mutex);
85 }
86 }
87 mutex_unlock(&acm_table_lock);
88 return acm;
89}
90
91/*
92 * Try to find an available minor number and if found, associate it with 'acm'.
93 */
94static int acm_alloc_minor(struct acm *acm)
95{
96 int minor;
97
98 mutex_lock(&acm_table_lock);
99 for (minor = 0; minor < ACM_TTY_MINORS; minor++) {
100 if (!acm_table[minor]) {
101 acm_table[minor] = acm;
102 break;
103 }
104 }
105 mutex_unlock(&acm_table_lock);
106
107 return minor;
108}
109
110/* Release the minor number associated with 'acm'. */
111static void acm_release_minor(struct acm *acm)
112{
113 mutex_lock(&acm_table_lock);
114 acm_table[acm->minor] = NULL;
115 mutex_unlock(&acm_table_lock);
116}
Alan Cox739e0282009-06-11 12:27:50 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Functions for ACM control messages.
120 */
121
Alan Cox6e47e062009-06-11 12:37:06 +0100122static int acm_ctrl_msg(struct acm *acm, int request, int value,
123 void *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
126 request, USB_RT_ACM, value,
127 acm->control->altsetting[0].desc.bInterfaceNumber,
128 buf, len, 5000);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100129 dev_dbg(&acm->control->dev,
130 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
131 __func__, request, value, len, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return retval < 0 ? retval : 0;
133}
134
135/* devices aren't required to support these requests.
136 * the cdc acm descriptor tells whether they do...
137 */
138#define acm_set_control(acm, control) \
139 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
140#define acm_set_line(acm, line) \
141 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
142#define acm_send_break(acm, ms) \
143 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
144
145/*
Oliver Neukum884b6002005-04-21 21:28:02 +0200146 * Write buffer management.
147 * All of these assume proper locks taken by the caller.
148 */
149
150static int acm_wb_alloc(struct acm *acm)
151{
152 int i, wbn;
153 struct acm_wb *wb;
154
David Engrafe4cf3aa2008-03-20 10:01:34 +0100155 wbn = 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200156 i = 0;
157 for (;;) {
158 wb = &acm->wb[wbn];
159 if (!wb->use) {
160 wb->use = 1;
161 return wbn;
162 }
Oliver Neukum86478942006-05-13 22:50:47 +0200163 wbn = (wbn + 1) % ACM_NW;
164 if (++i >= ACM_NW)
Oliver Neukum884b6002005-04-21 21:28:02 +0200165 return -1;
166 }
167}
168
Oliver Neukum884b6002005-04-21 21:28:02 +0200169static int acm_wb_is_avail(struct acm *acm)
170{
171 int i, n;
David Brownelle5fbab52008-08-06 18:46:10 -0700172 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200173
Oliver Neukum86478942006-05-13 22:50:47 +0200174 n = ACM_NW;
David Brownelle5fbab52008-08-06 18:46:10 -0700175 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100176 for (i = 0; i < ACM_NW; i++)
Oliver Neukum86478942006-05-13 22:50:47 +0200177 n -= acm->wb[i].use;
David Brownelle5fbab52008-08-06 18:46:10 -0700178 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200179 return n;
180}
181
Oliver Neukum884b6002005-04-21 21:28:02 +0200182/*
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800183 * Finish write. Caller must hold acm->write_lock
Oliver Neukum884b6002005-04-21 21:28:02 +0200184 */
David Engrafe4cf3aa2008-03-20 10:01:34 +0100185static void acm_write_done(struct acm *acm, struct acm_wb *wb)
Oliver Neukum884b6002005-04-21 21:28:02 +0200186{
David Engrafe4cf3aa2008-03-20 10:01:34 +0100187 wb->use = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200188 acm->transmitting--;
Oliver Neukum97d35f92009-12-16 17:05:57 +0100189 usb_autopm_put_interface_async(acm->control);
Oliver Neukum884b6002005-04-21 21:28:02 +0200190}
191
192/*
193 * Poke write.
Oliver Neukum11ea8592008-06-20 11:25:57 +0200194 *
195 * the caller is responsible for locking
Oliver Neukum884b6002005-04-21 21:28:02 +0200196 */
Oliver Neukum11ea8592008-06-20 11:25:57 +0200197
198static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
199{
200 int rc;
201
202 acm->transmitting++;
203
204 wb->urb->transfer_buffer = wb->buf;
205 wb->urb->transfer_dma = wb->dmah;
206 wb->urb->transfer_buffer_length = wb->len;
207 wb->urb->dev = acm->dev;
208
Alan Cox6e47e062009-06-11 12:37:06 +0100209 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
210 if (rc < 0) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100211 dev_err(&acm->data->dev,
212 "%s - usb_submit_urb(write bulk) failed: %d\n",
213 __func__, rc);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200214 acm_write_done(acm, wb);
215 }
216 return rc;
217}
218
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100219/*
220 * attributes exported through sysfs
221 */
222static ssize_t show_caps
223(struct device *dev, struct device_attribute *attr, char *buf)
224{
225 struct usb_interface *intf = to_usb_interface(dev);
226 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum884b6002005-04-21 21:28:02 +0200227
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100228 return sprintf(buf, "%d", acm->ctrl_caps);
229}
230static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
231
232static ssize_t show_country_codes
233(struct device *dev, struct device_attribute *attr, char *buf)
234{
235 struct usb_interface *intf = to_usb_interface(dev);
236 struct acm *acm = usb_get_intfdata(intf);
237
238 memcpy(buf, acm->country_codes, acm->country_code_size);
239 return acm->country_code_size;
240}
241
242static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
243
244static ssize_t show_country_rel_date
245(struct device *dev, struct device_attribute *attr, char *buf)
246{
247 struct usb_interface *intf = to_usb_interface(dev);
248 struct acm *acm = usb_get_intfdata(intf);
249
250 return sprintf(buf, "%d", acm->country_rel_date);
251}
252
253static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
Oliver Neukum884b6002005-04-21 21:28:02 +0200254/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Interrupt handlers for various ACM device responses
256 */
257
258/* control interface reports status changes with "interrupt" transfers */
David Howells7d12e782006-10-05 14:55:46 +0100259static void acm_ctrl_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct acm *acm = urb->context;
262 struct usb_cdc_notification *dr = urb->transfer_buffer;
263 unsigned char *data;
264 int newctrl;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700265 int retval;
266 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700268 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 case 0:
270 /* success */
271 break;
272 case -ECONNRESET:
273 case -ENOENT:
274 case -ESHUTDOWN:
275 /* this urb is terminated, clean up */
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100276 dev_dbg(&acm->control->dev,
277 "%s - urb shutting down with status: %d\n",
278 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return;
280 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100281 dev_dbg(&acm->control->dev,
282 "%s - nonzero urb status received: %d\n",
283 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto exit;
285 }
286
Johan Hovold7e7797e2011-03-22 11:12:11 +0100287 usb_mark_last_busy(acm->dev);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 data = (unsigned char *)(dr + 1);
290 switch (dr->bNotificationType) {
Alan Cox6e47e062009-06-11 12:37:06 +0100291 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100292 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
293 __func__, dr->wValue);
Alan Cox6e47e062009-06-11 12:37:06 +0100294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Alan Cox6e47e062009-06-11 12:37:06 +0100296 case USB_CDC_NOTIFY_SERIAL_STATE:
Alan Cox6e47e062009-06-11 12:37:06 +0100297 newctrl = get_unaligned_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jiri Slabyaa27a092013-03-07 13:12:30 +0100299 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
300 dev_dbg(&acm->control->dev, "%s - calling hangup\n",
301 __func__);
302 tty_port_tty_hangup(&acm->port, false);
Alan Cox6e47e062009-06-11 12:37:06 +0100303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Alan Cox6e47e062009-06-11 12:37:06 +0100305 acm->ctrlin = newctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100307 dev_dbg(&acm->control->dev,
308 "%s - input control lines: dcd%c dsr%c break%c "
309 "ring%c framing%c parity%c overrun%c\n",
310 __func__,
Alan Cox6e47e062009-06-11 12:37:06 +0100311 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
312 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
316 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
317 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319
Alan Cox6e47e062009-06-11 12:37:06 +0100320 default:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100321 dev_dbg(&acm->control->dev,
322 "%s - unknown notification %d received: index %d "
323 "len %d data0 %d data1 %d\n",
324 __func__,
Alan Cox6e47e062009-06-11 12:37:06 +0100325 dr->bNotificationType, dr->wIndex,
326 dr->wLength, data[0], data[1]);
327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329exit:
Alan Cox6e47e062009-06-11 12:37:06 +0100330 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700331 if (retval)
Johan Hovold1d9846e2011-03-22 11:12:14 +0100332 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
333 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Johan Hovold088c64f2011-03-25 11:06:02 +0100336static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Johan Hovold088c64f2011-03-25 11:06:02 +0100338 int res;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700339
Johan Hovold088c64f2011-03-25 11:06:02 +0100340 if (!test_and_clear_bit(index, &acm->read_urbs_free))
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Johan Hovold088c64f2011-03-25 11:06:02 +0100343 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
344
345 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
346 if (res) {
347 if (res != -EPERM) {
348 dev_err(&acm->data->dev,
349 "%s - usb_submit_urb failed: %d\n",
350 __func__, res);
351 }
352 set_bit(index, &acm->read_urbs_free);
353 return res;
354 }
355
356 return 0;
357}
358
359static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
360{
361 int res;
362 int i;
363
364 for (i = 0; i < acm->rx_buflimit; ++i) {
365 res = acm_submit_read_urb(acm, i, mem_flags);
366 if (res)
367 return res;
368 }
369
370 return 0;
371}
372
373static void acm_process_read_urb(struct acm *acm, struct urb *urb)
374{
Johan Hovold088c64f2011-03-25 11:06:02 +0100375 if (!urb->actual_length)
376 return;
377
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100378 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
379 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100380 tty_flip_buffer_push(&acm->port);
Johan Hovold088c64f2011-03-25 11:06:02 +0100381}
382
383static void acm_read_bulk_callback(struct urb *urb)
384{
385 struct acm_rb *rb = urb->context;
386 struct acm *acm = rb->instance;
387 unsigned long flags;
388
389 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
390 rb->index, urb->actual_length);
391 set_bit(rb->index, &acm->read_urbs_free);
392
393 if (!acm->dev) {
394 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200396 }
397 usb_mark_last_busy(acm->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Johan Hovold088c64f2011-03-25 11:06:02 +0100399 if (urb->status) {
Johan Hovold1d9846e2011-03-22 11:12:14 +0100400 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
Johan Hovold088c64f2011-03-25 11:06:02 +0100401 __func__, urb->status);
402 return;
403 }
404 acm_process_read_urb(acm, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Johan Hovold088c64f2011-03-25 11:06:02 +0100406 /* throttle device if requested by tty */
407 spin_lock_irqsave(&acm->read_lock, flags);
408 acm->throttled = acm->throttle_req;
409 if (!acm->throttled && !acm->susp_count) {
410 spin_unlock_irqrestore(&acm->read_lock, flags);
411 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
Oliver Neukum86478942006-05-13 22:50:47 +0200412 } else {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200413 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100418static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Ming Leicdc97792008-02-24 18:41:47 +0800420 struct acm_wb *wb = urb->context;
David Brownelle5fbab52008-08-06 18:46:10 -0700421 struct acm *acm = wb->instance;
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800422 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200423
Johan Hovold4fa46262011-03-22 11:12:16 +0100424 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
425 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
Johan Hovold1d9846e2011-03-22 11:12:14 +0100426 __func__,
David Brownelle5fbab52008-08-06 18:46:10 -0700427 urb->actual_length,
428 urb->transfer_buffer_length,
429 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800431 spin_lock_irqsave(&acm->write_lock, flags);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100432 acm_write_done(acm, wb);
Brandon Philipsad0b65e2008-11-06 11:19:11 -0800433 spin_unlock_irqrestore(&acm->write_lock, flags);
Havard Skinnemoen99823f42011-12-09 16:51:54 -0800434 schedule_work(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
David Howellsc4028952006-11-22 14:57:56 +0000437static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
David Howellsc4028952006-11-22 14:57:56 +0000439 struct acm *acm = container_of(work, struct acm, work);
David Brownelle5fbab52008-08-06 18:46:10 -0700440
Johan Hovold1d9846e2011-03-22 11:12:14 +0100441 dev_vdbg(&acm->data->dev, "%s\n", __func__);
442
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100443 tty_port_tty_wakeup(&acm->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * TTY handlers
448 */
449
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800450static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct acm *acm;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800453 int retval;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100454
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800455 dev_dbg(tty->dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800457 acm = acm_get_by_index(tty->index);
458 if (!acm)
459 return -ENODEV;
460
Jiri Slabyf8a8c102012-03-05 14:51:48 +0100461 retval = tty_standard_install(driver, tty);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800462 if (retval)
463 goto error_init_termios;
464
465 tty->driver_data = acm;
466
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800467 return 0;
468
469error_init_termios:
470 tty_port_put(&acm->port);
471 return retval;
472}
473
474static int acm_tty_open(struct tty_struct *tty, struct file *filp)
475{
476 struct acm *acm = tty->driver_data;
477
478 dev_dbg(tty->dev, "%s\n", __func__);
479
480 return tty_port_open(&acm->port, tty, filp);
481}
482
483static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
484{
485 struct acm *acm = container_of(port, struct acm, port);
486 int retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100488 dev_dbg(&acm->control->dev, "%s\n", __func__);
489
Oliver Neukum1365baf2007-10-12 17:24:28 +0200490 mutex_lock(&acm->mutex);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800491 if (acm->disconnected)
492 goto disconnected;
493
494 retval = usb_autopm_get_interface(acm->control);
495 if (retval)
496 goto error_get_interface;
497
498 /*
499 * FIXME: Why do we need this? Allocating 64K of physically contiguous
500 * memory is really nasty...
501 */
502 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
503 acm->control->needs_remote_wakeup = 1;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 acm->ctrlurb->dev = acm->dev;
506 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100507 dev_err(&acm->control->dev,
508 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800509 goto error_submit_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800512 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
513 if (acm_set_control(acm, acm->ctrlout) < 0 &&
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100514 (acm->ctrl_caps & USB_CDC_CAP_LINE))
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800515 goto error_set_control;
Alan Cox10077d42009-06-11 12:36:09 +0100516
Oliver Neukum11ea8592008-06-20 11:25:57 +0200517 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Otto Meta6c4707f2012-06-06 18:46:21 +0200519 /*
520 * Unthrottle device in case the TTY was closed while throttled.
521 */
522 spin_lock_irq(&acm->read_lock);
523 acm->throttled = 0;
524 acm->throttle_req = 0;
525 spin_unlock_irq(&acm->read_lock);
526
Johan Hovold088c64f2011-03-25 11:06:02 +0100527 if (acm_submit_read_urbs(acm, GFP_KERNEL))
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800528 goto error_submit_read_urbs;
Oliver Neukum2b626dc2010-02-03 17:10:22 +0100529
Oliver Neukum1365baf2007-10-12 17:24:28 +0200530 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800532 return 0;
533
534error_submit_read_urbs:
535 acm->ctrlout = 0;
536 acm_set_control(acm, acm->ctrlout);
537error_set_control:
538 usb_kill_urb(acm->ctrlurb);
539error_submit_urb:
Oliver Neukum2b626dc2010-02-03 17:10:22 +0100540 usb_autopm_put_interface(acm->control);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800541error_get_interface:
542disconnected:
543 mutex_unlock(&acm->mutex);
544 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800547static void acm_port_destruct(struct tty_port *port)
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700548{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800549 struct acm *acm = container_of(port, struct acm, port);
550
551 dev_dbg(&acm->control->dev, "%s\n", __func__);
David Kubicek61a87ad2005-11-01 18:51:34 +0100552
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800553 acm_release_minor(acm);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700554 usb_put_intf(acm->control);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100555 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700556 kfree(acm);
557}
558
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800559static void acm_port_shutdown(struct tty_port *port)
Alan Cox10077d42009-06-11 12:36:09 +0100560{
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800561 struct acm *acm = container_of(port, struct acm, port);
Johan Hovolddab54c92011-03-22 11:12:21 +0100562 int i;
563
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800564 dev_dbg(&acm->control->dev, "%s\n", __func__);
565
566 mutex_lock(&acm->mutex);
567 if (!acm->disconnected) {
Alan Cox10077d42009-06-11 12:36:09 +0100568 usb_autopm_get_interface(acm->control);
569 acm_set_control(acm, acm->ctrlout = 0);
Alan Cox10077d42009-06-11 12:36:09 +0100570 usb_kill_urb(acm->ctrlurb);
571 for (i = 0; i < ACM_NW; i++)
572 usb_kill_urb(acm->wb[i].urb);
Johan Hovolddab54c92011-03-22 11:12:21 +0100573 for (i = 0; i < acm->rx_buflimit; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +0100574 usb_kill_urb(acm->read_urbs[i]);
Alan Cox10077d42009-06-11 12:36:09 +0100575 acm->control->needs_remote_wakeup = 0;
576 usb_autopm_put_interface(acm->control);
577 }
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800578 mutex_unlock(&acm->mutex);
579}
580
581static void acm_tty_cleanup(struct tty_struct *tty)
582{
583 struct acm *acm = tty->driver_data;
584 dev_dbg(&acm->control->dev, "%s\n", __func__);
585 tty_port_put(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100586}
587
588static void acm_tty_hangup(struct tty_struct *tty)
589{
590 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800591 dev_dbg(&acm->control->dev, "%s\n", __func__);
Alan Cox10077d42009-06-11 12:36:09 +0100592 tty_port_hangup(&acm->port);
Alan Cox10077d42009-06-11 12:36:09 +0100593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static void acm_tty_close(struct tty_struct *tty, struct file *filp)
596{
597 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800598 dev_dbg(&acm->control->dev, "%s\n", __func__);
599 tty_port_close(&acm->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Alan Cox6e47e062009-06-11 12:37:06 +0100602static int acm_tty_write(struct tty_struct *tty,
603 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct acm *acm = tty->driver_data;
606 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200607 unsigned long flags;
608 int wbn;
609 struct acm_wb *wb;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!count)
612 return 0;
613
Johan Hovold5e9e75f2011-03-22 11:12:17 +0100614 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100615
Oliver Neukum884b6002005-04-21 21:28:02 +0200616 spin_lock_irqsave(&acm->write_lock, flags);
Alan Cox6e47e062009-06-11 12:37:06 +0100617 wbn = acm_wb_alloc(acm);
618 if (wbn < 0) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200619 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200622 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700624 if (!acm->dev) {
625 wb->use = 0;
626 spin_unlock_irqrestore(&acm->write_lock, flags);
627 return -ENODEV;
628 }
629
Oliver Neukum884b6002005-04-21 21:28:02 +0200630 count = (count > acm->writesize) ? acm->writesize : count;
Johan Hovold5e9e75f2011-03-22 11:12:17 +0100631 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
Oliver Neukum884b6002005-04-21 21:28:02 +0200632 memcpy(wb->buf, buf, count);
633 wb->len = count;
Greg Kroah-Hartman71c2fb02013-06-07 11:42:08 -0700634
635 usb_autopm_get_interface_async(acm->control);
636 if (acm->susp_count) {
637 if (!acm->delayed_wb)
638 acm->delayed_wb = wb;
639 else
640 usb_autopm_put_interface_async(acm->control);
641 spin_unlock_irqrestore(&acm->write_lock, flags);
642 return count; /* A white lie */
643 }
644 usb_mark_last_busy(acm->dev);
645
646 stat = acm_start_wb(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200647 spin_unlock_irqrestore(&acm->write_lock, flags);
648
Alan Cox6e47e062009-06-11 12:37:06 +0100649 if (stat < 0)
Oliver Neukum884b6002005-04-21 21:28:02 +0200650 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return count;
652}
653
654static int acm_tty_write_room(struct tty_struct *tty)
655{
656 struct acm *acm = tty->driver_data;
Oliver Neukum884b6002005-04-21 21:28:02 +0200657 /*
658 * Do not let the line discipline to know that we have a reserve,
659 * or it might get too enthusiastic.
660 */
David Brownell934da462008-08-06 18:44:12 -0700661 return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static int acm_tty_chars_in_buffer(struct tty_struct *tty)
665{
666 struct acm *acm = tty->driver_data;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800667 /*
668 * if the device was unplugged then any remaining characters fell out
669 * of the connector ;)
670 */
671 if (acm->disconnected)
Alan Cox23198fd2009-07-20 16:05:27 +0100672 return 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200673 /*
674 * This is inaccurate (overcounts), but it works.
675 */
Oliver Neukum86478942006-05-13 22:50:47 +0200676 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static void acm_tty_throttle(struct tty_struct *tty)
680{
681 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100682
Johan Hovold088c64f2011-03-25 11:06:02 +0100683 spin_lock_irq(&acm->read_lock);
684 acm->throttle_req = 1;
685 spin_unlock_irq(&acm->read_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static void acm_tty_unthrottle(struct tty_struct *tty)
689{
690 struct acm *acm = tty->driver_data;
Johan Hovold088c64f2011-03-25 11:06:02 +0100691 unsigned int was_throttled;
692
Johan Hovold088c64f2011-03-25 11:06:02 +0100693 spin_lock_irq(&acm->read_lock);
694 was_throttled = acm->throttled;
695 acm->throttled = 0;
696 acm->throttle_req = 0;
697 spin_unlock_irq(&acm->read_lock);
698
699 if (was_throttled)
700 acm_submit_read_urbs(acm, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Alan Cox9e989662008-07-22 11:18:03 +0100703static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct acm *acm = tty->driver_data;
Alan Cox9e989662008-07-22 11:18:03 +0100706 int retval;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800707
Alan Cox9e989662008-07-22 11:18:03 +0100708 retval = acm_send_break(acm, state ? 0xffff : 0);
709 if (retval < 0)
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100710 dev_dbg(&acm->control->dev, "%s - send break failed\n",
711 __func__);
Alan Cox9e989662008-07-22 11:18:03 +0100712 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Alan Cox60b33c12011-02-14 16:26:14 +0000715static int acm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct acm *acm = tty->driver_data;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
720 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
721 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
722 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
723 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
724 TIOCM_CTS;
725}
726
Alan Cox20b9d172011-02-14 16:26:50 +0000727static int acm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned int set, unsigned int clear)
729{
730 struct acm *acm = tty->driver_data;
731 unsigned int newctrl;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 newctrl = acm->ctrlout;
Alan Cox6e47e062009-06-11 12:37:06 +0100734 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
735 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
736 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
737 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 newctrl = (newctrl & ~clear) | set;
740
741 if (acm->ctrlout == newctrl)
742 return 0;
743 return acm_set_control(acm, acm->ctrlout = newctrl);
744}
745
Oliver Neukum18c75722012-02-17 17:21:24 -0500746static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
747{
748 struct serial_struct tmp;
749
750 if (!info)
751 return -EINVAL;
752
753 memset(&tmp, 0, sizeof(tmp));
754 tmp.flags = ASYNC_LOW_LATENCY;
755 tmp.xmit_fifo_size = acm->writesize;
756 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600757 tmp.close_delay = acm->port.close_delay / 10;
758 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
759 ASYNC_CLOSING_WAIT_NONE :
760 acm->port.closing_wait / 10;
Oliver Neukum18c75722012-02-17 17:21:24 -0500761
762 if (copy_to_user(info, &tmp, sizeof(tmp)))
763 return -EFAULT;
764 else
765 return 0;
766}
767
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600768static int set_serial_info(struct acm *acm,
769 struct serial_struct __user *newinfo)
770{
771 struct serial_struct new_serial;
772 unsigned int closing_wait, close_delay;
773 int retval = 0;
774
775 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
776 return -EFAULT;
777
778 close_delay = new_serial.close_delay * 10;
779 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
780 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
781
782 mutex_lock(&acm->port.mutex);
783
784 if (!capable(CAP_SYS_ADMIN)) {
785 if ((close_delay != acm->port.close_delay) ||
786 (closing_wait != acm->port.closing_wait))
787 retval = -EPERM;
788 else
789 retval = -EOPNOTSUPP;
790 } else {
791 acm->port.close_delay = close_delay;
792 acm->port.closing_wait = closing_wait;
793 }
794
795 mutex_unlock(&acm->port.mutex);
796 return retval;
797}
798
Alan Cox6caa76b2011-02-14 16:27:22 +0000799static int acm_tty_ioctl(struct tty_struct *tty,
Alan Cox6e47e062009-06-11 12:37:06 +0100800 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Oliver Neukum18c75722012-02-17 17:21:24 -0500802 struct acm *acm = tty->driver_data;
803 int rv = -ENOIOCTLCMD;
804
805 switch (cmd) {
806 case TIOCGSERIAL: /* gets serial port data */
807 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
808 break;
Dan Williamsba2d8ce2012-11-08 12:47:41 -0600809 case TIOCSSERIAL:
810 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
811 break;
Oliver Neukum18c75722012-02-17 17:21:24 -0500812 }
813
814 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Alan Cox6e47e062009-06-11 12:37:06 +0100817static void acm_tty_set_termios(struct tty_struct *tty,
818 struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct acm *acm = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +0100821 struct ktermios *termios = &tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct usb_cdc_line_coding newline;
823 int newctrl = acm->ctrlout;
824
Alan Cox9b80fee2009-09-19 13:13:23 -0700825 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
827 newline.bParityType = termios->c_cflag & PARENB ?
Alan Cox6e47e062009-06-11 12:37:06 +0100828 (termios->c_cflag & PARODD ? 1 : 2) +
829 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
Nicolas Boullis301a29d2012-10-16 00:06:23 +0200830 switch (termios->c_cflag & CSIZE) {
831 case CS5:
832 newline.bDataBits = 5;
833 break;
834 case CS6:
835 newline.bDataBits = 6;
836 break;
837 case CS7:
838 newline.bDataBits = 7;
839 break;
840 case CS8:
841 default:
842 newline.bDataBits = 8;
843 break;
844 }
Alan Cox6e47e062009-06-11 12:37:06 +0100845 /* FIXME: Needs to clear unsupported bits in the termios */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
847
848 if (!newline.dwDTERate) {
849 newline.dwDTERate = acm->line.dwDTERate;
850 newctrl &= ~ACM_CTRL_DTR;
Alan Cox6e47e062009-06-11 12:37:06 +0100851 } else
852 newctrl |= ACM_CTRL_DTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (newctrl != acm->ctrlout)
855 acm_set_control(acm, acm->ctrlout = newctrl);
856
857 if (memcmp(&acm->line, &newline, sizeof newline)) {
858 memcpy(&acm->line, &newline, sizeof newline);
Johan Hovolda5cc7ef2011-03-22 11:12:15 +0100859 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
860 __func__,
861 le32_to_cpu(newline.dwDTERate),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 newline.bCharFormat, newline.bParityType,
863 newline.bDataBits);
864 acm_set_line(acm, &acm->line);
865 }
866}
867
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -0800868static const struct tty_port_operations acm_port_ops = {
869 .shutdown = acm_port_shutdown,
870 .activate = acm_port_activate,
871 .destruct = acm_port_destruct,
872};
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * USB probe and disconnect routines.
876 */
877
Oliver Neukum830f4022008-06-25 14:17:16 +0200878/* Little helpers: write/read buffers free */
Oliver Neukum884b6002005-04-21 21:28:02 +0200879static void acm_write_buffers_free(struct acm *acm)
880{
881 int i;
882 struct acm_wb *wb;
Oliver Neukuma496c642008-10-21 10:39:04 +0200883 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
Oliver Neukum884b6002005-04-21 21:28:02 +0200884
Alan Cox6e47e062009-06-11 12:37:06 +0100885 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
Daniel Mack997ea582010-04-12 13:17:25 +0200886 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
Oliver Neukum884b6002005-04-21 21:28:02 +0200887}
888
Oliver Neukum830f4022008-06-25 14:17:16 +0200889static void acm_read_buffers_free(struct acm *acm)
890{
891 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
Johan Hovolddab54c92011-03-22 11:12:21 +0100892 int i;
Oliver Neukum830f4022008-06-25 14:17:16 +0200893
Johan Hovolddab54c92011-03-22 11:12:21 +0100894 for (i = 0; i < acm->rx_buflimit; i++)
Daniel Mack997ea582010-04-12 13:17:25 +0200895 usb_free_coherent(usb_dev, acm->readsize,
Johan Hovold088c64f2011-03-25 11:06:02 +0100896 acm->read_buffers[i].base, acm->read_buffers[i].dma);
Oliver Neukum830f4022008-06-25 14:17:16 +0200897}
898
Oliver Neukum884b6002005-04-21 21:28:02 +0200899/* Little helper: write buffers allocate */
900static int acm_write_buffers_alloc(struct acm *acm)
901{
902 int i;
903 struct acm_wb *wb;
904
Oliver Neukum86478942006-05-13 22:50:47 +0200905 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Daniel Mack997ea582010-04-12 13:17:25 +0200906 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
Oliver Neukum884b6002005-04-21 21:28:02 +0200907 &wb->dmah);
908 if (!wb->buf) {
909 while (i != 0) {
910 --i;
911 --wb;
Daniel Mack997ea582010-04-12 13:17:25 +0200912 usb_free_coherent(acm->dev, acm->writesize,
Oliver Neukum884b6002005-04-21 21:28:02 +0200913 wb->buf, wb->dmah);
914 }
915 return -ENOMEM;
916 }
917 }
918 return 0;
919}
920
Alan Cox10077d42009-06-11 12:36:09 +0100921static int acm_probe(struct usb_interface *intf,
922 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100925 struct usb_cdc_country_functional_desc *cfd = NULL;
David Brownellc6dbf552008-04-13 14:00:44 -0700926 unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int buflen = intf->altsetting->extralen;
928 struct usb_interface *control_interface;
929 struct usb_interface *data_interface;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +0200930 struct usb_endpoint_descriptor *epctrl = NULL;
931 struct usb_endpoint_descriptor *epread = NULL;
932 struct usb_endpoint_descriptor *epwrite = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct usb_device *usb_dev = interface_to_usbdev(intf);
934 struct acm *acm;
935 int minor;
Alan Cox6e47e062009-06-11 12:37:06 +0100936 int ctrlsize, readsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 u8 *buf;
938 u8 ac_management_function = 0;
939 u8 call_management_function = 0;
940 int call_interface_num = -1;
Erik Slagterfd5054c2011-05-11 12:06:55 +0200941 int data_interface_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +0200943 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +0100944 int i;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +0200945 int combined_interfaces = 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +0400946 struct device *tty_dev;
947 int rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Oliver Neukum86478942006-05-13 22:50:47 +0200949 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 quirks = (unsigned long)id->driver_info;
Dmitry Torokhov16142652013-02-02 11:02:14 -0800951
952 if (quirks == IGNORE_DEVICE)
953 return -ENODEV;
954
Oliver Neukum86478942006-05-13 22:50:47 +0200955 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
956
957 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (quirks == NO_UNION_NORMAL) {
959 data_interface = usb_ifnum_to_if(usb_dev, 1);
960 control_interface = usb_ifnum_to_if(usb_dev, 0);
961 goto skip_normal_probe;
962 }
Alan Cox6e47e062009-06-11 12:37:06 +0100963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* normal probing*/
965 if (!buffer) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700966 dev_err(&intf->dev, "Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -EINVAL;
968 }
969
970 if (!buflen) {
Toby Gray577045c2010-09-02 10:46:20 +0100971 if (intf->cur_altsetting->endpoint &&
972 intf->cur_altsetting->endpoint->extralen &&
Alan Cox6e47e062009-06-11 12:37:06 +0100973 intf->cur_altsetting->endpoint->extra) {
974 dev_dbg(&intf->dev,
975 "Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 buflen = intf->cur_altsetting->endpoint->extralen;
977 buffer = intf->cur_altsetting->endpoint->extra;
978 } else {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700979 dev_err(&intf->dev,
980 "Zero length descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -EINVAL;
982 }
983 }
984
985 while (buflen > 0) {
Alan Cox6e47e062009-06-11 12:37:06 +0100986 if (buffer[1] != USB_DT_CS_INTERFACE) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700987 dev_err(&intf->dev, "skipping garbage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto next_desc;
989 }
990
Alan Cox6e47e062009-06-11 12:37:06 +0100991 switch (buffer[2]) {
992 case USB_CDC_UNION_TYPE: /* we've found it */
993 if (union_header) {
994 dev_err(&intf->dev, "More than one "
995 "union descriptor, skipping ...\n");
996 goto next_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Alan Cox6e47e062009-06-11 12:37:06 +0100998 union_header = (struct usb_cdc_union_desc *)buffer;
999 break;
1000 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1001 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1002 break;
1003 case USB_CDC_HEADER_TYPE: /* maybe check version */
1004 break; /* for now we ignore it */
1005 case USB_CDC_ACM_TYPE:
1006 ac_management_function = buffer[3];
1007 break;
1008 case USB_CDC_CALL_MANAGEMENT_TYPE:
1009 call_management_function = buffer[3];
1010 call_interface_num = buffer[4];
Jeffrin Jose02f824a2012-07-01 21:25:16 +05301011 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
Alan Cox6e47e062009-06-11 12:37:06 +01001012 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1013 break;
1014 default:
1015 /* there are LOTS more CDC descriptors that
1016 * could legitimately be found here.
1017 */
1018 dev_dbg(&intf->dev, "Ignoring descriptor: "
1019 "type %02x, length %d\n",
1020 buffer[2], buffer[0]);
1021 break;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023next_desc:
1024 buflen -= buffer[0];
1025 buffer += buffer[0];
1026 }
1027
1028 if (!union_header) {
1029 if (call_interface_num > 0) {
Alan Cox6e47e062009-06-11 12:37:06 +01001030 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
Erik Slagterfd5054c2011-05-11 12:06:55 +02001031 /* quirks for Droids MuIn LCD */
1032 if (quirks & NO_DATA_INTERFACE)
1033 data_interface = usb_ifnum_to_if(usb_dev, 0);
1034 else
1035 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 control_interface = intf;
1037 } else {
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001038 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1039 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1040 return -ENODEV;
1041 } else {
1042 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1043 combined_interfaces = 1;
1044 control_interface = data_interface = intf;
1045 goto look_for_collapsed_interface;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 } else {
1049 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1050 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1051 if (!control_interface || !data_interface) {
Alan Cox6e47e062009-06-11 12:37:06 +01001052 dev_dbg(&intf->dev, "no interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -ENODEV;
1054 }
1055 }
Alan Cox6e47e062009-06-11 12:37:06 +01001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (data_interface_num != call_interface_num)
Alan Cox6e47e062009-06-11 12:37:06 +01001058 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001060 if (control_interface == data_interface) {
1061 /* some broken devices designed for windows work this way */
1062 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1063 combined_interfaces = 1;
1064 /* a popular other OS doesn't use it */
1065 quirks |= NO_CAP_LINE;
1066 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1067 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1068 return -EINVAL;
1069 }
1070look_for_collapsed_interface:
1071 for (i = 0; i < 3; i++) {
1072 struct usb_endpoint_descriptor *ep;
1073 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1074
1075 if (usb_endpoint_is_int_in(ep))
1076 epctrl = ep;
1077 else if (usb_endpoint_is_bulk_out(ep))
1078 epwrite = ep;
1079 else if (usb_endpoint_is_bulk_in(ep))
1080 epread = ep;
1081 else
1082 return -EINVAL;
1083 }
1084 if (!epctrl || !epread || !epwrite)
1085 return -ENODEV;
1086 else
1087 goto made_compressed_probe;
1088 }
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090skip_normal_probe:
1091
1092 /*workaround for switched interfaces */
Alan Cox6e47e062009-06-11 12:37:06 +01001093 if (data_interface->cur_altsetting->desc.bInterfaceClass
1094 != CDC_DATA_INTERFACE_TYPE) {
1095 if (control_interface->cur_altsetting->desc.bInterfaceClass
1096 == CDC_DATA_INTERFACE_TYPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct usb_interface *t;
Alan Cox6e47e062009-06-11 12:37:06 +01001098 dev_dbg(&intf->dev,
1099 "Your device has switched interfaces.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 t = control_interface;
1101 control_interface = data_interface;
1102 data_interface = t;
1103 } else {
1104 return -EINVAL;
1105 }
1106 }
Alan Stern74da5d62007-08-02 13:29:10 -04001107
1108 /* Accept probe requests only for the control interface */
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001109 if (!combined_interfaces && intf != control_interface)
Alan Stern74da5d62007-08-02 13:29:10 -04001110 return -ENODEV;
Alan Cox6e47e062009-06-11 12:37:06 +01001111
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001112 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1113 /* valid in this context */
Alan Cox6e47e062009-06-11 12:37:06 +01001114 dev_dbg(&intf->dev, "The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -EBUSY;
1116 }
1117
1118
Sven Schnelle99f347c2012-08-17 21:43:43 +02001119 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1120 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -EINVAL;
1122
1123 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1124 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1125 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1126
1127
1128 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -03001129 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* descriptors are swapped */
1131 struct usb_endpoint_descriptor *t;
Alan Cox6e47e062009-06-11 12:37:06 +01001132 dev_dbg(&intf->dev,
1133 "The data interface has switched endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 t = epread;
1135 epread = epwrite;
1136 epwrite = t;
1137 }
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001138made_compressed_probe:
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001139 dev_dbg(&intf->dev, "interfaces are valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Alan Cox6e47e062009-06-11 12:37:06 +01001141 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1142 if (acm == NULL) {
Johan Hovold255ab562011-03-22 11:12:13 +01001143 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto alloc_fail;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001147 minor = acm_alloc_minor(acm);
1148 if (minor == ACM_TTY_MINORS) {
1149 dev_err(&intf->dev, "no more free acm devices\n");
1150 kfree(acm);
1151 return -ENODEV;
1152 }
1153
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001154 ctrlsize = usb_endpoint_maxp(epctrl);
1155 readsize = usb_endpoint_maxp(epread) *
Alan Cox6e47e062009-06-11 12:37:06 +01001156 (quirks == SINGLE_RX_URB ? 1 : 2);
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001157 acm->combined_interfaces = combined_interfaces;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001158 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 acm->control = control_interface;
1160 acm->data = data_interface;
1161 acm->minor = minor;
1162 acm->dev = usb_dev;
1163 acm->ctrl_caps = ac_management_function;
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001164 if (quirks & NO_CAP_LINE)
1165 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 acm->ctrlsize = ctrlsize;
1167 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +02001168 acm->rx_buflimit = num_rx_buf;
David Howellsc4028952006-11-22 14:57:56 +00001169 INIT_WORK(&acm->work, acm_softint);
Oliver Neukum884b6002005-04-21 21:28:02 +02001170 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +01001171 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001172 mutex_init(&acm->mutex);
David Kubicek61a87ad2005-11-01 18:51:34 +01001173 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
Oliver Neukumcf7fdd52009-08-04 23:52:09 +02001174 acm->is_int_ep = usb_endpoint_xfer_int(epread);
1175 if (acm->is_int_ep)
1176 acm->bInterval = epread->bInterval;
Alan Cox739e0282009-06-11 12:27:50 +01001177 tty_port_init(&acm->port);
1178 acm->port.ops = &acm_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Daniel Mack997ea582010-04-12 13:17:25 +02001180 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!buf) {
Johan Hovold255ab562011-03-22 11:12:13 +01001182 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto alloc_fail2;
1184 }
1185 acm->ctrl_buffer = buf;
1186
Oliver Neukum884b6002005-04-21 21:28:02 +02001187 if (acm_write_buffers_alloc(acm) < 0) {
Johan Hovold255ab562011-03-22 11:12:13 +01001188 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto alloc_fail4;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1193 if (!acm->ctrlurb) {
Johan Hovold255ab562011-03-22 11:12:13 +01001194 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto alloc_fail5;
1196 }
Oliver Neukum86478942006-05-13 22:50:47 +02001197 for (i = 0; i < num_rx_buf; i++) {
Johan Hovold088c64f2011-03-25 11:06:02 +01001198 struct acm_rb *rb = &(acm->read_buffers[i]);
1199 struct urb *urb;
David Kubicek61a87ad2005-11-01 18:51:34 +01001200
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001201 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1202 &rb->dma);
1203 if (!rb->base) {
1204 dev_err(&intf->dev, "out of memory "
1205 "(read bufs usb_alloc_coherent)\n");
1206 goto alloc_fail6;
1207 }
Johan Hovold088c64f2011-03-25 11:06:02 +01001208 rb->index = i;
1209 rb->instance = acm;
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001210
Johan Hovold088c64f2011-03-25 11:06:02 +01001211 urb = usb_alloc_urb(0, GFP_KERNEL);
1212 if (!urb) {
Johan Hovold255ab562011-03-22 11:12:13 +01001213 dev_err(&intf->dev,
Alan Cox6e47e062009-06-11 12:37:06 +01001214 "out of memory (read urbs usb_alloc_urb)\n");
Axel Linc2572b72010-05-31 08:04:47 +08001215 goto alloc_fail6;
David Kubicek61a87ad2005-11-01 18:51:34 +01001216 }
Johan Hovold088c64f2011-03-25 11:06:02 +01001217 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1218 urb->transfer_dma = rb->dma;
1219 if (acm->is_int_ep) {
1220 usb_fill_int_urb(urb, acm->dev,
1221 acm->rx_endpoint,
1222 rb->base,
1223 acm->readsize,
1224 acm_read_bulk_callback, rb,
1225 acm->bInterval);
1226 } else {
1227 usb_fill_bulk_urb(urb, acm->dev,
1228 acm->rx_endpoint,
1229 rb->base,
1230 acm->readsize,
1231 acm_read_bulk_callback, rb);
1232 }
David Kubicek61a87ad2005-11-01 18:51:34 +01001233
Johan Hovold088c64f2011-03-25 11:06:02 +01001234 acm->read_urbs[i] = urb;
1235 __set_bit(i, &acm->read_urbs_free);
David Kubicek61a87ad2005-11-01 18:51:34 +01001236 }
Alan Cox6e47e062009-06-11 12:37:06 +01001237 for (i = 0; i < ACM_NW; i++) {
David Engrafe4cf3aa2008-03-20 10:01:34 +01001238 struct acm_wb *snd = &(acm->wb[i]);
1239
Alan Cox6e47e062009-06-11 12:37:06 +01001240 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1241 if (snd->urb == NULL) {
Johan Hovold255ab562011-03-22 11:12:13 +01001242 dev_err(&intf->dev,
Johan Hovold59d7fec2011-03-22 11:12:12 +01001243 "out of memory (write urbs usb_alloc_urb)\n");
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001244 goto alloc_fail7;
David Engrafe4cf3aa2008-03-20 10:01:34 +01001245 }
1246
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001247 if (usb_endpoint_xfer_int(epwrite))
1248 usb_fill_int_urb(snd->urb, usb_dev,
Ming Leic5211182012-10-16 21:21:21 +08001249 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
Arseniy Lartsev5186ffe2009-07-01 16:27:26 +04001250 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1251 else
1252 usb_fill_bulk_urb(snd->urb, usb_dev,
1253 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1254 NULL, acm->writesize, acm_write_bulk, snd);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001255 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1256 snd->instance = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
Alan Cox6e47e062009-06-11 12:37:06 +01001259 usb_set_intfdata(intf, acm);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001260
1261 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1262 if (i < 0)
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001263 goto alloc_fail7;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001264
1265 if (cfd) { /* export the country data */
1266 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1267 if (!acm->country_codes)
1268 goto skip_countries;
1269 acm->country_code_size = cfd->bLength - 4;
Alan Cox6e47e062009-06-11 12:37:06 +01001270 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1271 cfd->bLength - 4);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001272 acm->country_rel_date = cfd->iCountryCodeRelDate;
1273
1274 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1275 if (i < 0) {
1276 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001277 acm->country_codes = NULL;
1278 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001279 goto skip_countries;
1280 }
1281
Alan Cox6e47e062009-06-11 12:37:06 +01001282 i = device_create_file(&intf->dev,
1283 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001284 if (i < 0) {
Axel Linc2572b72010-05-31 08:04:47 +08001285 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001286 kfree(acm->country_codes);
Julia Lawalle7c8e862011-12-23 14:02:55 +01001287 acm->country_codes = NULL;
1288 acm->country_code_size = 0;
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001289 goto skip_countries;
1290 }
1291 }
1292
1293skip_countries:
Alan Cox6e47e062009-06-11 12:37:06 +01001294 usb_fill_int_urb(acm->ctrlurb, usb_dev,
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001295 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1296 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1297 /* works around buggy devices */
Felipe Balbid102e782013-07-01 11:23:24 +03001298 epctrl->bInterval ? epctrl->bInterval : 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1300 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1303
1304 acm_set_control(acm, acm->ctrlout);
1305
1306 acm->line.dwDTERate = cpu_to_le32(9600);
1307 acm->line.bDataBits = 8;
1308 acm_set_line(acm, &acm->line);
1309
1310 usb_driver_claim_interface(&acm_driver, data_interface, acm);
David Brownell672c4e12008-08-06 18:41:12 -07001311 usb_set_intfdata(data_interface, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001313 usb_get_intf(control_interface);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001314 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
Jiri Slaby734cc172012-08-07 21:47:47 +02001315 &control_interface->dev);
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001316 if (IS_ERR(tty_dev)) {
1317 rv = PTR_ERR(tty_dev);
1318 goto alloc_fail8;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001321 return 0;
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001322alloc_fail8:
1323 if (acm->country_codes) {
1324 device_remove_file(&acm->control->dev,
1325 &dev_attr_wCountryCodes);
1326 device_remove_file(&acm->control->dev,
1327 &dev_attr_iCountryCodeRelDate);
1328 }
1329 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001330alloc_fail7:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001331 usb_set_intfdata(intf, NULL);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001332 for (i = 0; i < ACM_NW; i++)
1333 usb_free_urb(acm->wb[i].urb);
Axel Linc2572b72010-05-31 08:04:47 +08001334alloc_fail6:
Oliver Neukum86478942006-05-13 22:50:47 +02001335 for (i = 0; i < num_rx_buf; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +01001336 usb_free_urb(acm->read_urbs[i]);
Johan Hovold74f5e1b2011-03-22 11:12:23 +01001337 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 usb_free_urb(acm->ctrlurb);
1339alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001340 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341alloc_fail4:
Daniel Mack997ea582010-04-12 13:17:25 +02001342 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343alloc_fail2:
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001344 acm_release_minor(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 kfree(acm);
1346alloc_fail:
Alexey Khoroshilovc93d8192013-03-16 01:30:32 +04001347 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Oliver Neukum1365baf2007-10-12 17:24:28 +02001350static void stop_data_traffic(struct acm *acm)
1351{
1352 int i;
Johan Hovolda5cc7ef2011-03-22 11:12:15 +01001353
1354 dev_dbg(&acm->control->dev, "%s\n", __func__);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001355
Oliver Neukum1365baf2007-10-12 17:24:28 +02001356 usb_kill_urb(acm->ctrlurb);
Alan Cox6e47e062009-06-11 12:37:06 +01001357 for (i = 0; i < ACM_NW; i++)
David Engrafe4cf3aa2008-03-20 10:01:34 +01001358 usb_kill_urb(acm->wb[i].urb);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001359 for (i = 0; i < acm->rx_buflimit; i++)
Johan Hovold088c64f2011-03-25 11:06:02 +01001360 usb_kill_urb(acm->read_urbs[i]);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001361
1362 cancel_work_sync(&acm->work);
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static void acm_disconnect(struct usb_interface *intf)
1366{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001367 struct acm *acm = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct usb_device *usb_dev = interface_to_usbdev(intf);
Alan Cox10077d42009-06-11 12:36:09 +01001369 struct tty_struct *tty;
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001370 int i;
1371
1372 dev_dbg(&intf->dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David Brownell672c4e12008-08-06 18:41:12 -07001374 /* sibling interface is already cleaning up */
1375 if (!acm)
Oliver Neukum86067eea2006-01-08 12:39:13 +01001376 return;
David Brownell672c4e12008-08-06 18:41:12 -07001377
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001378 mutex_lock(&acm->mutex);
1379 acm->disconnected = true;
Alan Cox6e47e062009-06-11 12:37:06 +01001380 if (acm->country_codes) {
Alan Stern74da5d62007-08-02 13:29:10 -04001381 device_remove_file(&acm->control->dev,
1382 &dev_attr_wCountryCodes);
1383 device_remove_file(&acm->control->dev,
1384 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001385 }
Alan Stern74da5d62007-08-02 13:29:10 -04001386 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001387 usb_set_intfdata(acm->control, NULL);
1388 usb_set_intfdata(acm->data, NULL);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001389 mutex_unlock(&acm->mutex);
1390
1391 tty = tty_port_tty_get(&acm->port);
1392 if (tty) {
1393 tty_vhangup(tty);
1394 tty_kref_put(tty);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Oliver Neukum1365baf2007-10-12 17:24:28 +02001397 stop_data_traffic(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Johan Hovoldcb255052013-03-19 09:21:06 +01001399 tty_unregister_device(acm_tty_driver, acm->minor);
1400
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001401 usb_free_urb(acm->ctrlurb);
1402 for (i = 0; i < ACM_NW; i++)
1403 usb_free_urb(acm->wb[i].urb);
1404 for (i = 0; i < acm->rx_buflimit; i++)
1405 usb_free_urb(acm->read_urbs[i]);
Oliver Neukum884b6002005-04-21 21:28:02 +02001406 acm_write_buffers_free(acm);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001407 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001408 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Oliver Neukuma2bfb4a2009-05-16 21:13:19 +02001410 if (!acm->combined_interfaces)
1411 usb_driver_release_interface(&acm_driver, intf == acm->control ?
Oliver Neukum830f4022008-06-25 14:17:16 +02001412 acm->data : acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001414 tty_port_put(&acm->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Oliver Neukum35758582008-07-01 19:10:08 +02001417#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001418static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1419{
1420 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001421 int cnt;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001422
Alan Stern5b1b0b82011-08-19 23:49:48 +02001423 if (PMSG_IS_AUTO(message)) {
Oliver Neukum11ea8592008-06-20 11:25:57 +02001424 int b;
1425
Johan Hovold088c64f2011-03-25 11:06:02 +01001426 spin_lock_irq(&acm->write_lock);
1427 b = acm->transmitting;
1428 spin_unlock_irq(&acm->write_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001429 if (b)
1430 return -EBUSY;
1431 }
1432
1433 spin_lock_irq(&acm->read_lock);
1434 spin_lock(&acm->write_lock);
1435 cnt = acm->susp_count++;
1436 spin_unlock(&acm->write_lock);
1437 spin_unlock_irq(&acm->read_lock);
1438
1439 if (cnt)
Oliver Neukum1365baf2007-10-12 17:24:28 +02001440 return 0;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001441
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001442 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
Oliver Neukum1365baf2007-10-12 17:24:28 +02001443 stop_data_traffic(acm);
1444
Oliver Neukum1365baf2007-10-12 17:24:28 +02001445 return 0;
1446}
1447
1448static int acm_resume(struct usb_interface *intf)
1449{
1450 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001451 struct acm_wb *wb;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001452 int rv = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +02001453 int cnt;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001454
Oliver Neukum11ea8592008-06-20 11:25:57 +02001455 spin_lock_irq(&acm->read_lock);
1456 acm->susp_count -= 1;
1457 cnt = acm->susp_count;
1458 spin_unlock_irq(&acm->read_lock);
1459
1460 if (cnt)
Oliver Neukum1365baf2007-10-12 17:24:28 +02001461 return 0;
1462
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001463 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
Oliver Neukum1365baf2007-10-12 17:24:28 +02001464 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001465
1466 spin_lock_irq(&acm->write_lock);
1467 if (acm->delayed_wb) {
1468 wb = acm->delayed_wb;
1469 acm->delayed_wb = NULL;
1470 spin_unlock_irq(&acm->write_lock);
Oliver Neukumf0730922010-03-03 00:37:56 +01001471 acm_start_wb(acm, wb);
Oliver Neukum97d35f92009-12-16 17:05:57 +01001472 } else {
1473 spin_unlock_irq(&acm->write_lock);
1474 }
1475
1476 /*
1477 * delayed error checking because we must
1478 * do the write path at all cost
1479 */
Oliver Neukum1365baf2007-10-12 17:24:28 +02001480 if (rv < 0)
Oliver Neukum11ea8592008-06-20 11:25:57 +02001481 goto err_out;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001482
Johan Hovold088c64f2011-03-25 11:06:02 +01001483 rv = acm_submit_read_urbs(acm, GFP_NOIO);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001484 }
1485
1486err_out:
Oliver Neukum1365baf2007-10-12 17:24:28 +02001487 return rv;
1488}
Oliver Neukum35758582008-07-01 19:10:08 +02001489
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001490static int acm_reset_resume(struct usb_interface *intf)
1491{
1492 struct acm *acm = usb_get_intfdata(intf);
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001493
Jiri Slabyaa27a092013-03-07 13:12:30 +01001494 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1495 tty_port_tty_hangup(&acm->port, false);
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001496
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001497 return acm_resume(intf);
1498}
1499
Oliver Neukum35758582008-07-01 19:10:08 +02001500#endif /* CONFIG_PM */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001501
1502#define NOKIA_PCSUITE_ACM_INFO(x) \
1503 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1504 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1505 USB_CDC_ACM_PROTO_VENDOR)
1506
Toby Gray4035e452010-09-01 16:01:19 +01001507#define SAMSUNG_PCSUITE_ACM_INFO(x) \
1508 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1509 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1510 USB_CDC_ACM_PROTO_VENDOR)
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/*
1513 * USB driver structure.
1514 */
1515
Németh Márton6ef48522010-01-10 15:33:45 +01001516static const struct usb_device_id acm_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* quirky and broken devices */
1518 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1519 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1520 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001521 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1522 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1523 },
Andrew Lunn0f9c7b4a2008-12-23 17:31:23 +01001524 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1525 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1526 },
Masahito Omote8753e652005-07-29 12:17:25 -07001527 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1528 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1529 },
Chris Malley91a9c922006-10-03 10:08:28 +01001530 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1531 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1532 },
Alan Cox7abcf202009-04-06 17:35:01 +01001533 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1534 .driver_info = SINGLE_RX_URB,
1535 },
Oliver Neukum86478942006-05-13 22:50:47 +02001536 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1537 .driver_info = SINGLE_RX_URB, /* firmware bug */
1538 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001539 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1540 .driver_info = SINGLE_RX_URB, /* firmware bug */
1541 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001542 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1543 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1544 },
Iain McFarlane6149ed52008-05-04 00:13:49 +01001545 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1546 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1547 },
Eric Sandeenc8fd2c32008-08-14 08:25:40 -05001548 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1549 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1550 },
Alan Coxc89c60e2009-01-11 19:53:10 +00001551 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1552 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1553 },
Xiao Kaijiancab98a02009-05-08 00:48:23 +08001554 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1555 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1556 },
Dmitriy Taychenachev155df652009-02-25 12:36:51 +08001557 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1558 },
Krzysztof Hałasa6abff5d2011-12-12 14:51:00 +01001559 /* Motorola H24 HSPA module: */
1560 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
1561 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */
1562 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */
1563 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */
1564 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */
1565 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */
1566 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */
1567 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
1568
Adam Richterc332b4e2009-02-18 16:17:15 -08001569 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1570 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1571 data interface instead of
1572 communications interface.
1573 Maybe we should define a new
1574 quirk for this. */
1575 },
Jean-Christian de Rivaze7d491a2012-10-10 12:49:02 +00001576 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1577 .driver_info = NO_UNION_NORMAL,
1578 },
Denis N Ladin036915a2012-12-26 18:29:44 +05001579 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1580 .driver_info = NO_UNION_NORMAL,
1581 },
Kir Kolyshkin1f17c502009-05-28 20:33:58 +04001582 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1583 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1584 },
Russ Nelsonc3baa192010-04-21 23:07:03 -04001585 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1586 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1587 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001588
Adrian Taylorc1479a92009-11-19 10:35:33 +00001589 /* Nokia S60 phones expose two ACM channels. The first is
1590 * a modem and is picked up by the standard AT-command
1591 * information below. The second is 'vendor-specific' but
1592 * is treated as a serial device at the S60 end, so we want
1593 * to expose it on Linux too. */
1594 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1595 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1596 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1597 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1598 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1599 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1600 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1601 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1602 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1603 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1604 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1605 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1606 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1607 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1608 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1609 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1610 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1611 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1612 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1613 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1614 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1615 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1616 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1617 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1618 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1619 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1620 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1621 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1622 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1623 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1624 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1625 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1626 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1627 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1628 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1629 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1630 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1631 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1632 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1633 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1634 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1635 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1636 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
Przemo Firszt83a4eae2010-06-28 21:29:34 +01001637 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
Toby Gray4035e452010-09-01 16:01:19 +01001638 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1639 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1640 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1641 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1642 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1643 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1644 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1645 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1646 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1647 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
Arvid Ephraim Picciani721d92f2011-01-25 15:58:40 +01001648 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
Toby Gray4061fde2011-06-06 14:52:48 +01001649 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1650 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
Toby Gray4035e452010-09-01 16:01:19 +01001651 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
Adrian Taylorc1479a92009-11-19 10:35:33 +00001652
Denis Pershin65e52f42011-09-04 17:37:21 +07001653 /* Support for Owen devices */
1654 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1655
Adrian Taylorc1479a92009-11-19 10:35:33 +00001656 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1657
Julian Calaby7c5d8c32010-01-05 23:57:46 +11001658 /* Support Lego NXT using pbLua firmware */
Julian Calabyce126642010-01-05 23:58:20 +11001659 { USB_DEVICE(0x0694, 0xff00),
1660 .driver_info = NOT_A_MODEM,
Otavio Salvador7893afc2010-09-26 23:35:05 -03001661 },
Julian Calaby7c5d8c32010-01-05 23:57:46 +11001662
Erik Slagterfd5054c2011-05-11 12:06:55 +02001663 /* Support for Droids MuIn LCD */
1664 { USB_DEVICE(0x04d8, 0x000b),
1665 .driver_info = NO_DATA_INTERFACE,
1666 },
1667
Dmitry Torokhov16142652013-02-02 11:02:14 -08001668#if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1669 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1670 .driver_info = IGNORE_DEVICE,
1671 },
1672 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1673 .driver_info = IGNORE_DEVICE,
1674 },
1675#endif
1676
Philippe Corbes5b239f02010-08-31 19:31:32 +02001677 /* control interfaces without any protocol set */
1678 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1679 USB_CDC_PROTO_NONE) },
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* control interfaces with various AT-command sets */
1682 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1683 USB_CDC_ACM_PROTO_AT_V25TER) },
1684 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1685 USB_CDC_ACM_PROTO_AT_PCCA101) },
1686 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1687 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1688 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1689 USB_CDC_ACM_PROTO_AT_GSM) },
1690 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
Alan Cox6e47e062009-06-11 12:37:06 +01001691 USB_CDC_ACM_PROTO_AT_3G) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1693 USB_CDC_ACM_PROTO_AT_CDMA) },
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 { }
1696};
1697
Alan Cox6e47e062009-06-11 12:37:06 +01001698MODULE_DEVICE_TABLE(usb, acm_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 .name = "cdc_acm",
1702 .probe = acm_probe,
1703 .disconnect = acm_disconnect,
Oliver Neukum35758582008-07-01 19:10:08 +02001704#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001705 .suspend = acm_suspend,
1706 .resume = acm_resume,
Francesco Lavraa91b0c52009-12-08 09:54:11 +01001707 .reset_resume = acm_reset_resume,
Oliver Neukum35758582008-07-01 19:10:08 +02001708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .id_table = acm_ids,
Oliver Neukum35758582008-07-01 19:10:08 +02001710#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001711 .supports_autosuspend = 1,
Oliver Neukum35758582008-07-01 19:10:08 +02001712#endif
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001713 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714};
1715
1716/*
1717 * TTY driver structures.
1718 */
1719
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001720static const struct tty_operations acm_ops = {
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001721 .install = acm_tty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 .open = acm_tty_open,
1723 .close = acm_tty_close,
Havard Skinnemoen7fb57a02011-11-30 13:28:14 -08001724 .cleanup = acm_tty_cleanup,
Alan Cox10077d42009-06-11 12:36:09 +01001725 .hangup = acm_tty_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .write = acm_tty_write,
1727 .write_room = acm_tty_write_room,
1728 .ioctl = acm_tty_ioctl,
1729 .throttle = acm_tty_throttle,
1730 .unthrottle = acm_tty_unthrottle,
1731 .chars_in_buffer = acm_tty_chars_in_buffer,
1732 .break_ctl = acm_tty_break_ctl,
1733 .set_termios = acm_tty_set_termios,
1734 .tiocmget = acm_tty_tiocmget,
1735 .tiocmset = acm_tty_tiocmset,
1736};
1737
1738/*
1739 * Init / exit.
1740 */
1741
1742static int __init acm_init(void)
1743{
1744 int retval;
1745 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1746 if (!acm_tty_driver)
1747 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 acm_tty_driver->driver_name = "acm",
1749 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 acm_tty_driver->major = ACM_TTY_MAJOR,
1751 acm_tty_driver->minor_start = 0,
1752 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1753 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001754 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 acm_tty_driver->init_termios = tty_std_termios;
Alan Cox6e47e062009-06-11 12:37:06 +01001756 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1757 HUPCL | CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 tty_set_operations(acm_tty_driver, &acm_ops);
1759
1760 retval = tty_register_driver(acm_tty_driver);
1761 if (retval) {
1762 put_tty_driver(acm_tty_driver);
1763 return retval;
1764 }
1765
1766 retval = usb_register(&acm_driver);
1767 if (retval) {
1768 tty_unregister_driver(acm_tty_driver);
1769 put_tty_driver(acm_tty_driver);
1770 return retval;
1771 }
1772
Johan Hovolda2c7b932011-03-22 11:12:18 +01001773 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 return 0;
1776}
1777
1778static void __exit acm_exit(void)
1779{
1780 usb_deregister(&acm_driver);
1781 tty_unregister_driver(acm_tty_driver);
1782 put_tty_driver(acm_tty_driver);
1783}
1784
1785module_init(acm_init);
1786module_exit(acm_exit);
1787
Alan Cox6e47e062009-06-11 12:37:06 +01001788MODULE_AUTHOR(DRIVER_AUTHOR);
1789MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790MODULE_LICENSE("GPL");
Scott James Remnante766aeb2009-04-06 17:33:18 +01001791MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);