cdc-acm.c 52.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
/*
 * cdc-acm.c
 *
 * Copyright (c) 1999 Armin Fuerst	<fuerst@in.tum.de>
Pavel Machek's avatar
Pavel Machek committed
6
 * Copyright (c) 1999 Pavel Machek	<pavel@ucw.cz>
Linus Torvalds's avatar
Linus Torvalds committed
7
8
9
 * Copyright (c) 1999 Johannes Erdfelt	<johannes@erdfelt.com>
 * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
 * Copyright (c) 2004 Oliver Neukum	<oliver@neukum.name>
10
 * Copyright (c) 2005 David Kubicek	<dave@awk.cz>
11
 * Copyright (c) 2011 Johan Hovold	<jhovold@gmail.com>
Linus Torvalds's avatar
Linus Torvalds committed
12
13
14
15
16
17
18
 *
 * USB Abstract Control Model driver for USB modems and ISDN adapters
 *
 * Sponsored by SuSE
 */

#undef DEBUG
19
#undef VERBOSE_DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
20
21

#include <linux/kernel.h>
22
#include <linux/sched/signal.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
24
25
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
26
#include <linux/log2.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
#include <linux/tty.h>
28
#include <linux/serial.h>
Linus Torvalds's avatar
Linus Torvalds committed
29
30
31
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
32
#include <linux/mutex.h>
Alan Cox's avatar
Alan Cox committed
33
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <linux/usb.h>
35
#include <linux/usb/cdc.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
37
#include <asm/byteorder.h>
#include <asm/unaligned.h>
38
#include <linux/idr.h>
39
#include <linux/list.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
41
42

#include "cdc-acm.h"

43

44
#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
Linus Torvalds's avatar
Linus Torvalds committed
45
46
47
48
49
#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"

static struct usb_driver acm_driver;
static struct tty_driver *acm_tty_driver;

50
51
static DEFINE_IDR(acm_minors);
static DEFINE_MUTEX(acm_minors_lock);
Linus Torvalds's avatar
Linus Torvalds committed
52

53
54
55
static void acm_tty_set_termios(struct tty_struct *tty,
				struct ktermios *termios_old);

56
/*
57
 * acm_minors accessors
58
 */
Linus Torvalds's avatar
Linus Torvalds committed
59

60
/*
61
 * Look up an ACM structure by minor. If found and not disconnected, increment
62
63
 * its refcount and return it with its mutex held.
 */
64
static struct acm *acm_get_by_minor(unsigned int minor)
65
66
67
{
	struct acm *acm;

68
69
	mutex_lock(&acm_minors_lock);
	acm = idr_find(&acm_minors, minor);
70
71
72
73
74
75
76
77
78
79
	if (acm) {
		mutex_lock(&acm->mutex);
		if (acm->disconnected) {
			mutex_unlock(&acm->mutex);
			acm = NULL;
		} else {
			tty_port_get(&acm->port);
			mutex_unlock(&acm->mutex);
		}
	}
80
	mutex_unlock(&acm_minors_lock);
81
82
83
84
85
86
87
88
89
90
	return acm;
}

/*
 * Try to find an available minor number and if found, associate it with 'acm'.
 */
static int acm_alloc_minor(struct acm *acm)
{
	int minor;

91
92
93
	mutex_lock(&acm_minors_lock);
	minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
	mutex_unlock(&acm_minors_lock);
94
95
96
97
98
99
100

	return minor;
}

/* Release the minor number associated with 'acm'.  */
static void acm_release_minor(struct acm *acm)
{
101
102
103
	mutex_lock(&acm_minors_lock);
	idr_remove(&acm_minors, acm->minor);
	mutex_unlock(&acm_minors_lock);
104
}
Alan Cox's avatar
Alan Cox committed
105

Linus Torvalds's avatar
Linus Torvalds committed
106
107
108
109
/*
 * Functions for ACM control messages.
 */

110
111
static int acm_ctrl_msg(struct acm *acm, int request, int value,
							void *buf, int len)
Linus Torvalds's avatar
Linus Torvalds committed
112
{
113
114
115
116
117
118
119
	int retval;

	retval = usb_autopm_get_interface(acm->control);
	if (retval)
		return retval;

	retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
Linus Torvalds's avatar
Linus Torvalds committed
120
121
122
		request, USB_RT_ACM, value,
		acm->control->altsetting[0].desc.bInterfaceNumber,
		buf, len, 5000);
123

124
	dev_dbg(&acm->control->dev,
125
126
		"%s - rq 0x%02x, val %#x, len %#x, result %d\n",
		__func__, request, value, len, retval);
127
128
129

	usb_autopm_put_interface(acm->control);

Linus Torvalds's avatar
Linus Torvalds committed
130
131
132
133
134
135
	return retval < 0 ? retval : 0;
}

/* devices aren't required to support these requests.
 * the cdc acm descriptor tells whether they do...
 */
136
137
138
139
140
141
142
143
144
static inline int acm_set_control(struct acm *acm, int control)
{
	if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
		return -EOPNOTSUPP;

	return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
			control, NULL, 0);
}

Linus Torvalds's avatar
Linus Torvalds committed
145
146
147
148
149
#define acm_set_line(acm, line) \
	acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
#define acm_send_break(acm, ms) \
	acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)

150
151
152
153
154
155
156
157
158
159
160
static void acm_kill_urbs(struct acm *acm)
{
	int i;

	usb_kill_urb(acm->ctrlurb);
	for (i = 0; i < ACM_NW; i++)
		usb_kill_urb(acm->wb[i].urb);
	for (i = 0; i < acm->rx_buflimit; i++)
		usb_kill_urb(acm->read_urbs[i]);
}

161
162
163
164
165
166
167
168
169
170
/*
 * Write buffer management.
 * All of these assume proper locks taken by the caller.
 */

static int acm_wb_alloc(struct acm *acm)
{
	int i, wbn;
	struct acm_wb *wb;

171
	wbn = 0;
172
173
174
175
176
	i = 0;
	for (;;) {
		wb = &acm->wb[wbn];
		if (!wb->use) {
			wb->use = 1;
177
			wb->len = 0;
178
179
			return wbn;
		}
180
181
		wbn = (wbn + 1) % ACM_NW;
		if (++i >= ACM_NW)
182
183
184
185
186
187
188
			return -1;
	}
}

static int acm_wb_is_avail(struct acm *acm)
{
	int i, n;
189
	unsigned long flags;
190

191
	n = ACM_NW;
192
	spin_lock_irqsave(&acm->write_lock, flags);
193
	for (i = 0; i < ACM_NW; i++)
194
		n -= acm->wb[i].use;
195
	spin_unlock_irqrestore(&acm->write_lock, flags);
196
197
198
199
	return n;
}

/*
200
 * Finish write. Caller must hold acm->write_lock
201
 */
202
static void acm_write_done(struct acm *acm, struct acm_wb *wb)
203
{
204
	wb->use = 0;
205
	acm->transmitting--;
206
	usb_autopm_put_interface_async(acm->control);
207
208
209
210
}

/*
 * Poke write.
211
212
 *
 * the caller is responsible for locking
213
 */
214
215
216
217
218
219
220
221
222
223
224
225

static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
{
	int rc;

	acm->transmitting++;

	wb->urb->transfer_buffer = wb->buf;
	wb->urb->transfer_dma = wb->dmah;
	wb->urb->transfer_buffer_length = wb->len;
	wb->urb->dev = acm->dev;

226
227
	rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
	if (rc < 0) {
228
229
230
		dev_err(&acm->data->dev,
			"%s - usb_submit_urb(write bulk) failed: %d\n",
			__func__, rc);
231
232
233
234
235
		acm_write_done(acm, wb);
	}
	return rc;
}

236
237
238
/*
 * attributes exported through sysfs
 */
239
static ssize_t bmCapabilities_show
240
241
242
243
244
245
246
(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct usb_interface *intf = to_usb_interface(dev);
	struct acm *acm = usb_get_intfdata(intf);

	return sprintf(buf, "%d", acm->ctrl_caps);
}
247
static DEVICE_ATTR_RO(bmCapabilities);
248

249
static ssize_t wCountryCodes_show
250
251
252
253
254
255
256
257
258
(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct usb_interface *intf = to_usb_interface(dev);
	struct acm *acm = usb_get_intfdata(intf);

	memcpy(buf, acm->country_codes, acm->country_code_size);
	return acm->country_code_size;
}

259
static DEVICE_ATTR_RO(wCountryCodes);
260

261
static ssize_t iCountryCodeRelDate_show
262
263
264
265
266
267
268
(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct usb_interface *intf = to_usb_interface(dev);
	struct acm *acm = usb_get_intfdata(intf);

	return sprintf(buf, "%d", acm->country_rel_date);
}
269

270
static DEVICE_ATTR_RO(iCountryCodeRelDate);
Linus Torvalds's avatar
Linus Torvalds committed
271
272
273
274
/*
 * Interrupt handlers for various ACM device responses
 */

275
static void acm_process_notification(struct acm *acm, unsigned char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
276
277
{
	int newctrl;
Oliver Neukum's avatar
Oliver Neukum committed
278
	int difference;
279
	unsigned long flags;
280
281
	struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf;
	unsigned char *data = buf + sizeof(struct usb_cdc_notification);
Linus Torvalds's avatar
Linus Torvalds committed
282
283

	switch (dr->bNotificationType) {
284
	case USB_CDC_NOTIFY_NETWORK_CONNECTION:
285
286
		dev_dbg(&acm->control->dev,
			"%s - network connection: %d\n", __func__, dr->wValue);
287
		break;
Linus Torvalds's avatar
Linus Torvalds committed
288

289
	case USB_CDC_NOTIFY_SERIAL_STATE:
290
291
292
293
294
295
		if (le16_to_cpu(dr->wLength) != 2) {
			dev_dbg(&acm->control->dev,
				"%s - malformed serial state\n", __func__);
			break;
		}

296
		newctrl = get_unaligned_le16(data);
297
298
		dev_dbg(&acm->control->dev,
			"%s - serial state: 0x%x\n", __func__, newctrl);
Linus Torvalds's avatar
Linus Torvalds committed
299

300
		if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
301
302
			dev_dbg(&acm->control->dev,
				"%s - calling hangup\n", __func__);
303
			tty_port_tty_hangup(&acm->port, false);
304
		}
Linus Torvalds's avatar
Linus Torvalds committed
305

Oliver Neukum's avatar
Oliver Neukum committed
306
		difference = acm->ctrlin ^ newctrl;
307
		spin_lock_irqsave(&acm->read_lock, flags);
308
		acm->ctrlin = newctrl;
Oliver Neukum's avatar
Oliver Neukum committed
309
310
311
312
313
314
		acm->oldcount = acm->iocount;

		if (difference & ACM_CTRL_DSR)
			acm->iocount.dsr++;
		if (difference & ACM_CTRL_DCD)
			acm->iocount.dcd++;
315
316
317
318
319
		if (newctrl & ACM_CTRL_BRK)
			acm->iocount.brk++;
		if (newctrl & ACM_CTRL_RI)
			acm->iocount.rng++;
		if (newctrl & ACM_CTRL_FRAMING)
Oliver Neukum's avatar
Oliver Neukum committed
320
			acm->iocount.frame++;
321
		if (newctrl & ACM_CTRL_PARITY)
Oliver Neukum's avatar
Oliver Neukum committed
322
			acm->iocount.parity++;
323
		if (newctrl & ACM_CTRL_OVERRUN)
Oliver Neukum's avatar
Oliver Neukum committed
324
			acm->iocount.overrun++;
325
		spin_unlock_irqrestore(&acm->read_lock, flags);
Oliver Neukum's avatar
Oliver Neukum committed
326
327
328

		if (difference)
			wake_up_all(&acm->wioctl);
Linus Torvalds's avatar
Linus Torvalds committed
329

Oliver Neukum's avatar
Oliver Neukum committed
330
		break;
Linus Torvalds's avatar
Linus Torvalds committed
331

332
	default:
333
		dev_dbg(&acm->control->dev,
334
			"%s - unknown notification %d received: index %d len %d\n",
335
			__func__,
336
			dr->bNotificationType, dr->wIndex, dr->wLength);
337
338
	}
}
339

340
341
342
343
344
345
346
347
348
349
350
351
352
/* control interface reports status changes with "interrupt" transfers */
static void acm_ctrl_irq(struct urb *urb)
{
	struct acm *acm = urb->context;
	struct usb_cdc_notification *dr = urb->transfer_buffer;
	unsigned int current_size = urb->actual_length;
	unsigned int expected_size, copy_size, alloc_size;
	int retval;
	int status = urb->status;

	switch (status) {
	case 0:
		/* success */
353
		break;
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dev_dbg(&acm->control->dev,
			"%s - urb shutting down with status: %d\n",
			__func__, status);
		return;
	default:
		dev_dbg(&acm->control->dev,
			"%s - nonzero urb status received: %d\n",
			__func__, status);
		goto exit;
	}

	usb_mark_last_busy(acm->dev);

	if (acm->nb_index)
		dr = (struct usb_cdc_notification *)acm->notification_buffer;

	/* size = notification-header + (optional) data */
	expected_size = sizeof(struct usb_cdc_notification) +
					le16_to_cpu(dr->wLength);

	if (current_size < expected_size) {
		/* notification is transmitted fragmented, reassemble */
		if (acm->nb_size < expected_size) {
			if (acm->nb_size) {
				kfree(acm->notification_buffer);
				acm->nb_size = 0;
			}
			alloc_size = roundup_pow_of_two(expected_size);
			/*
			 * kmalloc ensures a valid notification_buffer after a
			 * use of kfree in case the previous allocation was too
			 * small. Final freeing is done on disconnect.
			 */
			acm->notification_buffer =
				kmalloc(alloc_size, GFP_ATOMIC);
			if (!acm->notification_buffer)
				goto exit;
			acm->nb_size = alloc_size;
		}

		copy_size = min(current_size,
				expected_size - acm->nb_index);

		memcpy(&acm->notification_buffer[acm->nb_index],
		       urb->transfer_buffer, copy_size);
		acm->nb_index += copy_size;
		current_size = acm->nb_index;
Linus Torvalds's avatar
Linus Torvalds committed
405
	}
406
407
408
409
410
411
412

	if (current_size >= expected_size) {
		/* notification complete */
		acm_process_notification(acm, (unsigned char *)dr);
		acm->nb_index = 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
413
exit:
414
	retval = usb_submit_urb(urb, GFP_ATOMIC);
415
	if (retval && retval != -EPERM)
416
417
		dev_err(&acm->control->dev,
			"%s - usb_submit_urb failed: %d\n", __func__, retval);
Linus Torvalds's avatar
Linus Torvalds committed
418
419
}

420
static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
Linus Torvalds's avatar
Linus Torvalds committed
421
{
422
	int res;
423

424
425
	if (!test_and_clear_bit(index, &acm->read_urbs_free))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
426

427
428
	res = usb_submit_urb(acm->read_urbs[index], mem_flags);
	if (res) {
429
		if (res != -EPERM && res != -ENODEV) {
430
			dev_err(&acm->data->dev,
431
432
				"urb %d failed submission with %d\n",
				index, res);
433
434
435
		}
		set_bit(index, &acm->read_urbs_free);
		return res;
436
437
	} else {
		dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
438
	}
Linus Torvalds's avatar
Linus Torvalds committed
439

440
441
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
442

443
444
445
446
static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
{
	int res;
	int i;
447

448
449
450
451
	for (i = 0; i < acm->rx_buflimit; ++i) {
		res = acm_submit_read_urb(acm, i, mem_flags);
		if (res)
			return res;
452
	}
453
454

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
455
456
}

457
static void acm_process_read_urb(struct acm *acm, struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
458
{
459
	if (!urb->actual_length)
460
		return;
461

462
463
	tty_insert_flip_string(&acm->port, urb->transfer_buffer,
			urb->actual_length);
Jiri Slaby's avatar
Jiri Slaby committed
464
	tty_flip_buffer_push(&acm->port);
465
}
Alan Cox's avatar
Alan Cox committed
466

467
468
469
470
static void acm_read_bulk_callback(struct urb *urb)
{
	struct acm_rb *rb = urb->context;
	struct acm *acm = rb->instance;
471
	int status = urb->status;
472
473
	bool stopped = false;
	bool stalled = false;
474

475
	dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
476
		rb->index, urb->actual_length, status);
477

478
479
480
481
	if (!acm->dev) {
		dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
		return;
	}
482

483
484
485
486
487
488
489
	switch (status) {
	case 0:
		usb_mark_last_busy(acm->dev);
		acm_process_read_urb(acm, urb);
		break;
	case -EPIPE:
		set_bit(EVENT_RX_STALL, &acm->flags);
490
491
		stalled = true;
		break;
492
493
494
495
496
497
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
		dev_dbg(&acm->data->dev,
			"%s - urb shutting down with status: %d\n",
			__func__, status);
498
499
		stopped = true;
		break;
500
501
502
503
504
	default:
		dev_dbg(&acm->data->dev,
			"%s - nonzero urb status received: %d\n",
			__func__, status);
		break;
505
	}
506

507
	/*
508
509
510
	 * Make sure URB processing is done before marking as free to avoid
	 * racing with unthrottle() on another CPU. Matches the barriers
	 * implied by the test_and_clear_bit() in acm_submit_read_urb().
511
512
	 */
	smp_mb__before_atomic();
513
514
515
516
517
518
519
520
521
522
523
524
525
	set_bit(rb->index, &acm->read_urbs_free);
	/*
	 * Make sure URB is marked as free before checking the throttled flag
	 * to avoid racing with unthrottle() on another CPU. Matches the
	 * smp_mb() in unthrottle().
	 */
	smp_mb__after_atomic();

	if (stopped || stalled) {
		if (stalled)
			schedule_work(&acm->work);
		return;
	}
526

527
528
529
530
	if (test_bit(ACM_THROTTLED, &acm->flags))
		return;

	acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
531
532
533
}

/* data interface wrote those outgoing bytes */
534
static void acm_write_bulk(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
535
{
536
	struct acm_wb *wb = urb->context;
537
	struct acm *acm = wb->instance;
538
	unsigned long flags;
539
	int status = urb->status;
Linus Torvalds's avatar
Linus Torvalds committed
540

541
	if (status || (urb->actual_length != urb->transfer_buffer_length))
542
		dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
543
544
			urb->actual_length,
			urb->transfer_buffer_length,
545
			status);
Linus Torvalds's avatar
Linus Torvalds committed
546

547
	spin_lock_irqsave(&acm->write_lock, flags);
548
	acm_write_done(acm, wb);
549
	spin_unlock_irqrestore(&acm->write_lock, flags);
550
	set_bit(EVENT_TTY_WAKEUP, &acm->flags);
551
	schedule_work(&acm->work);
Linus Torvalds's avatar
Linus Torvalds committed
552
553
}

David Howells's avatar
David Howells committed
554
static void acm_softint(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
555
{
556
	int i;
David Howells's avatar
David Howells committed
557
	struct acm *acm = container_of(work, struct acm, work);
558

559
560
561
562
563
564
565
566
567
568
569
	if (test_bit(EVENT_RX_STALL, &acm->flags)) {
		if (!(usb_autopm_get_interface(acm->data))) {
			for (i = 0; i < acm->rx_buflimit; i++)
				usb_kill_urb(acm->read_urbs[i]);
			usb_clear_halt(acm->dev, acm->in);
			acm_submit_read_urbs(acm, GFP_KERNEL);
			usb_autopm_put_interface(acm->data);
		}
		clear_bit(EVENT_RX_STALL, &acm->flags);
	}

570
	if (test_and_clear_bit(EVENT_TTY_WAKEUP, &acm->flags))
571
		tty_port_tty_wakeup(&acm->port);
Linus Torvalds's avatar
Linus Torvalds committed
572
573
574
575
576
577
}

/*
 * TTY handlers
 */

578
static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
579
580
{
	struct acm *acm;
581
	int retval;
Linus Torvalds's avatar
Linus Torvalds committed
582

583
	acm = acm_get_by_minor(tty->index);
584
585
	if (!acm)
		return -ENODEV;
586

587
	retval = tty_standard_install(driver, tty);
588
589
	if (retval)
		goto error_init_termios;
Alan Cox's avatar
Alan Cox committed
590

591
592
593
594
595
596
597
	/*
	 * Suppress initial echoing for some devices which might send data
	 * immediately after acm driver has been installed.
	 */
	if (acm->quirks & DISABLE_ECHO)
		tty->termios.c_lflag &= ~ECHO;

Linus Torvalds's avatar
Linus Torvalds committed
598
599
	tty->driver_data = acm;

600
601
602
603
604
605
606
607
608
609
610
611
612
613
	return 0;

error_init_termios:
	tty_port_put(&acm->port);
	return retval;
}

static int acm_tty_open(struct tty_struct *tty, struct file *filp)
{
	struct acm *acm = tty->driver_data;

	return tty_port_open(&acm->port, tty, filp);
}

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
static void acm_port_dtr_rts(struct tty_port *port, int raise)
{
	struct acm *acm = container_of(port, struct acm, port);
	int val;
	int res;

	if (raise)
		val = ACM_CTRL_DTR | ACM_CTRL_RTS;
	else
		val = 0;

	/* FIXME: add missing ctrlout locking throughout driver */
	acm->ctrlout = val;

	res = acm_set_control(acm, val);
	if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
		dev_err(&acm->control->dev, "failed to set dtr/rts\n");
}

633
634
635
636
static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
{
	struct acm *acm = container_of(port, struct acm, port);
	int retval = -ENODEV;
637
	int i;
638

Oliver Neukum's avatar
Oliver Neukum committed
639
	mutex_lock(&acm->mutex);
640
641
642
643
644
645
646
647
648
649
650
651
652
	if (acm->disconnected)
		goto disconnected;

	retval = usb_autopm_get_interface(acm->control);
	if (retval)
		goto error_get_interface;

	/*
	 * FIXME: Why do we need this? Allocating 64K of physically contiguous
	 * memory is really nasty...
	 */
	set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
	acm->control->needs_remote_wakeup = 1;
Oliver Neukum's avatar
Oliver Neukum committed
653

Linus Torvalds's avatar
Linus Torvalds committed
654
	acm->ctrlurb->dev = acm->dev;
655
656
	retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
	if (retval) {
657
658
		dev_err(&acm->control->dev,
			"%s - usb_submit_urb(ctrl irq) failed\n", __func__);
659
		goto error_submit_urb;
Linus Torvalds's avatar
Linus Torvalds committed
660
661
	}

662
663
	acm_tty_set_termios(tty, NULL);

664
665
666
	/*
	 * Unthrottle device in case the TTY was closed while throttled.
	 */
667
	clear_bit(ACM_THROTTLED, &acm->flags);
668

669
670
	retval = acm_submit_read_urbs(acm, GFP_KERNEL);
	if (retval)
671
		goto error_submit_read_urbs;
672

673
674
	usb_autopm_put_interface(acm->control);

Oliver Neukum's avatar
Oliver Neukum committed
675
	mutex_unlock(&acm->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
676

677
678
679
	return 0;

error_submit_read_urbs:
680
681
	for (i = 0; i < acm->rx_buflimit; i++)
		usb_kill_urb(acm->read_urbs[i]);
682
683
	usb_kill_urb(acm->ctrlurb);
error_submit_urb:
684
	usb_autopm_put_interface(acm->control);
685
686
687
error_get_interface:
disconnected:
	mutex_unlock(&acm->mutex);
688
689

	return usb_translate_errors(retval);
Linus Torvalds's avatar
Linus Torvalds committed
690
691
}

692
static void acm_port_destruct(struct tty_port *port)
693
{
694
695
696
	struct acm *acm = container_of(port, struct acm, port);

	acm_release_minor(acm);
697
	usb_put_intf(acm->control);
698
	kfree(acm->country_codes);
699
700
701
	kfree(acm);
}

702
static void acm_port_shutdown(struct tty_port *port)
Alan Cox's avatar
Alan Cox committed
703
{
704
	struct acm *acm = container_of(port, struct acm, port);
705
706
	struct urb *urb;
	struct acm_wb *wb;
707

708
709
710
711
712
713
714
715
716
717
718
	/*
	 * Need to grab write_lock to prevent race with resume, but no need to
	 * hold it due to the tty-port initialised flag.
	 */
	spin_lock_irq(&acm->write_lock);
	spin_unlock_irq(&acm->write_lock);

	usb_autopm_get_interface_no_resume(acm->control);
	acm->control->needs_remote_wakeup = 0;
	usb_autopm_put_interface(acm->control);

719
720
721
722
723
724
725
	for (;;) {
		urb = usb_get_from_anchor(&acm->delayed);
		if (!urb)
			break;
		wb = urb->context;
		wb->use = 0;
		usb_autopm_put_interface_async(acm->control);
Alan Cox's avatar
Alan Cox committed
726
	}
727

728
	acm_kill_urbs(acm);
729
730
731
732
733
}

static void acm_tty_cleanup(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
734

735
	tty_port_put(&acm->port);
Alan Cox's avatar
Alan Cox committed
736
737
738
739
740
}

static void acm_tty_hangup(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
741

Alan Cox's avatar
Alan Cox committed
742
743
744
	tty_port_hangup(&acm->port);
}

Linus Torvalds's avatar
Linus Torvalds committed
745
746
747
static void acm_tty_close(struct tty_struct *tty, struct file *filp)
{
	struct acm *acm = tty->driver_data;
748

749
	tty_port_close(&acm->port, tty, filp);
Linus Torvalds's avatar
Linus Torvalds committed
750
751
}

752
753
static int acm_tty_write(struct tty_struct *tty,
					const unsigned char *buf, int count)
Linus Torvalds's avatar
Linus Torvalds committed
754
755
756
{
	struct acm *acm = tty->driver_data;
	int stat;
757
758
759
760
	unsigned long flags;
	int wbn;
	struct acm_wb *wb;

Linus Torvalds's avatar
Linus Torvalds committed
761
762
763
	if (!count)
		return 0;

764
	dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
765

766
	spin_lock_irqsave(&acm->write_lock, flags);
767
768
	wbn = acm_wb_alloc(acm);
	if (wbn < 0) {
769
770
771
772
		spin_unlock_irqrestore(&acm->write_lock, flags);
		return 0;
	}
	wb = &acm->wb[wbn];
Linus Torvalds's avatar
Linus Torvalds committed
773

774
775
776
777
778
779
	if (!acm->dev) {
		wb->use = 0;
		spin_unlock_irqrestore(&acm->write_lock, flags);
		return -ENODEV;
	}

780
	count = (count > acm->writesize) ? acm->writesize : count;
781
	dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
782
783
	memcpy(wb->buf, buf, count);
	wb->len = count;
784

785
786
787
788
789
790
791
	stat = usb_autopm_get_interface_async(acm->control);
	if (stat) {
		wb->use = 0;
		spin_unlock_irqrestore(&acm->write_lock, flags);
		return stat;
	}

792
	if (acm->susp_count) {
793
		usb_anchor_urb(wb->urb, &acm->delayed);
794
		spin_unlock_irqrestore(&acm->write_lock, flags);
795
		return count;
796
797
798
	}

	stat = acm_start_wb(acm, wb);
799
	spin_unlock_irqrestore(&acm->write_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
800

801
	if (stat < 0)
Linus Torvalds's avatar
Linus Torvalds committed
802
803
804
805
806
807
808
		return stat;
	return count;
}

static int acm_tty_write_room(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
809
810
811
812
	/*
	 * Do not let the line discipline to know that we have a reserve,
	 * or it might get too enthusiastic.
	 */
813
	return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds's avatar
Linus Torvalds committed
814
815
816
817
818
}

static int acm_tty_chars_in_buffer(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
819
820
821
822
823
	/*
	 * if the device was unplugged then any remaining characters fell out
	 * of the connector ;)
	 */
	if (acm->disconnected)
Alan Cox's avatar
Alan Cox committed
824
		return 0;
825
826
827
	/*
	 * This is inaccurate (overcounts), but it works.
	 */
828
	return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds's avatar
Linus Torvalds committed
829
830
831
832
833
}

static void acm_tty_throttle(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
834

835
	set_bit(ACM_THROTTLED, &acm->flags);
Linus Torvalds's avatar
Linus Torvalds committed
836
837
838
839
840
}

static void acm_tty_unthrottle(struct tty_struct *tty)
{
	struct acm *acm = tty->driver_data;
841

842
	clear_bit(ACM_THROTTLED, &acm->flags);
843

844
845
846
	/* Matches the smp_mb__after_atomic() in acm_read_bulk_callback(). */
	smp_mb();

847
	acm_submit_read_urbs(acm, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
848
849
}

Alan Cox's avatar
Alan Cox committed
850
static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds's avatar
Linus Torvalds committed
851
852
{
	struct acm *acm = tty->driver_data;
Alan Cox's avatar
Alan Cox committed
853
	int retval;
854

Alan Cox's avatar
Alan Cox committed
855
856
	retval = acm_send_break(acm, state ? 0xffff : 0);
	if (retval < 0)
857
858
		dev_dbg(&acm->control->dev,
			"%s - send break failed\n", __func__);
Alan Cox's avatar
Alan Cox committed
859
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
860
861
}

862
static int acm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
863
864
865
866
867
868
869
870
871
872
873
{
	struct acm *acm = tty->driver_data;

	return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
	       (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
	       (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
	       (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
	       (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
	       TIOCM_CTS;
}

874
static int acm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds's avatar
Linus Torvalds committed
875
876
877
878
879
880
			    unsigned int set, unsigned int clear)
{
	struct acm *acm = tty->driver_data;
	unsigned int newctrl;

	newctrl = acm->ctrlout;
881
882
883
884
	set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
					(set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
	clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
					(clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
Linus Torvalds's avatar
Linus Torvalds committed
885
886
887
888
889
890
891
892

	newctrl = (newctrl & ~clear) | set;

	if (acm->ctrlout == newctrl)
		return 0;
	return acm_set_control(acm, acm->ctrlout = newctrl);
}

Al Viro's avatar
Al Viro committed
893
static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
894
{
Al Viro's avatar
Al Viro committed
895
	struct acm *acm = tty->driver_data;
896

Al Viro's avatar
Al Viro committed
897
898
899
900
	ss->xmit_fifo_size = acm->writesize;
	ss->baud_base = le32_to_cpu(acm->line.dwDTERate);
	ss->close_delay	= acm->port.close_delay / 10;
	ss->closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
901
902
				ASYNC_CLOSING_WAIT_NONE :
				acm->port.closing_wait / 10;
Al Viro's avatar
Al Viro committed
903
	return 0;
904
905
}

Al Viro's avatar
Al Viro committed
906
static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
907
{
Al Viro's avatar
Al Viro committed
908
	struct acm *acm = tty->driver_data;
909
910
911
	unsigned int closing_wait, close_delay;
	int retval = 0;

Al Viro's avatar
Al Viro committed
912
913
914
	close_delay = ss->close_delay * 10;
	closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
			ASYNC_CLOSING_WAIT_NONE : ss->closing_wait * 10;
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932

	mutex_lock(&acm->port.mutex);

	if (!capable(CAP_SYS_ADMIN)) {
		if ((close_delay != acm->port.close_delay) ||
		    (closing_wait != acm->port.closing_wait))
			retval = -EPERM;
		else
			retval = -EOPNOTSUPP;
	} else {
		acm->port.close_delay  = close_delay;
		acm->port.closing_wait = closing_wait;
	}

	mutex_unlock(&acm->port.mutex);
	return retval;
}

Oliver Neukum's avatar
Oliver Neukum committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
static int wait_serial_change(struct acm *acm, unsigned long arg)
{
	int rv = 0;
	DECLARE_WAITQUEUE(wait, current);
	struct async_icount old, new;

	do {
		spin_lock_irq(&acm->read_lock);
		old = acm->oldcount;
		new = acm->iocount;
		acm->oldcount = new;
		spin_unlock_irq(&acm->read_lock);

		if ((arg & TIOCM_DSR) &&
			old.dsr != new.dsr)
			break;
		if ((arg & TIOCM_CD)  &&
			old.dcd != new.dcd)
			break;
		if ((arg & TIOCM_RI) &&
			old.rng != new.rng)
			break;

		add_wait_queue(&acm->wioctl, &wait);
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
		remove_wait_queue(&acm->wioctl, &wait);
		if (acm->disconnected) {
			if (arg & TIOCM_CD)
				break;
			else
				rv = -ENODEV;
		} else {
			if (signal_pending(current))
				rv = -ERESTARTSYS;
		}
	} while (!rv);

	

	return rv;
}

976
977
static int acm_tty_get_icount(struct tty_struct *tty,
					struct serial_icounter_struct *icount)
Oliver Neukum's avatar
Oliver Neukum committed
978
{
979
	struct acm *acm = tty->driver_data;
Oliver Neukum's avatar
Oliver Neukum committed
980

981
982
983
984
985
986
987
	icount->dsr = acm->iocount.dsr;
	icount->rng = acm->iocount.rng;
	icount->dcd = acm->iocount.dcd;
	icount->frame = acm->iocount.frame;
	icount->overrun = acm->iocount.overrun;
	icount->parity = acm->iocount.parity;
	icount->brk = acm->iocount.brk;
Oliver Neukum's avatar
Oliver Neukum committed
988

989
	return 0;
Oliver Neukum's avatar
Oliver Neukum committed
990
991
}

992
static int acm_tty_ioctl(struct tty_struct *tty,
993
					unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
994
{
995
996
997
998
	struct acm *acm = tty->driver_data;
	int rv = -ENOIOCTLCMD;

	switch (cmd) {
Oliver Neukum's avatar
Oliver Neukum committed
999
	case TIOCMIWAIT:
1000
1001
1002
1003
1004
		rv = usb_autopm_get_interface(acm->control);
		if (rv < 0) {
			rv = -EIO;
			break;
		}
Oliver Neukum's avatar
Oliver Neukum committed
1005
		rv = wait_serial_change(acm, arg);
1006
		usb_autopm_put_interface(acm->control);
Oliver Neukum's avatar
Oliver Neukum committed
1007
		break;
1008
1009
1010
	}

	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
1011
1012
}

1013
1014
static void acm_tty_set_termios(struct tty_struct *tty,
						struct ktermios *termios_old)
Linus Torvalds's avatar
Linus Torvalds committed
1015
1016
{
	struct acm *acm = tty->driver_data;
1017
	struct ktermios *termios = &tty->termios;
Linus Torvalds's avatar
Linus Torvalds committed
1018
1019
1020
	struct usb_cdc_line_coding newline;
	int newctrl = acm->ctrlout;

1021
	newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
Linus Torvalds's avatar
Linus Torvalds committed
1022
1023
	newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
	newline.bParityType = termios->c_cflag & PARENB ?
1024
1025
				(termios->c_cflag & PARODD ? 1 : 2) +
				(termios->c_cflag & CMSPAR ? 2 : 0) : 0;
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
	switch (termios->c_cflag & CSIZE) {
	case CS5:
		newline.bDataBits = 5;
		break;
	case CS6:
		newline.bDataBits = 6;
		break;
	case CS7:
		newline.bDataBits = 7;
		break;
	case CS8:
	default:
		newline.bDataBits = 8;
		break;
	}
1041
	/* FIXME: Needs to clear unsupported bits in the termios */
Linus Torvalds's avatar
Linus Torvalds committed
1042
1043
	acm->clocal = ((termios->c_cflag & CLOCAL) != 0);

1044
	if (C_BAUD(tty) == B0) {
Linus Torvalds's avatar
Linus Torvalds committed
1045
1046
		newline.dwDTERate = acm->line.dwDTERate;
		newctrl &= ~ACM_CTRL_DTR;
1047
	} else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
1048
		newctrl |=  ACM_CTRL_DTR;
1049
	}
Linus Torvalds's avatar
Linus Torvalds committed
1050
1051
1052
1053
1054
1055

	if (newctrl != acm->ctrlout)
		acm_set_control(acm, acm->ctrlout = newctrl);

	if (memcmp(&acm->line, &newline, sizeof newline)) {
		memcpy(&acm->line, &newline, sizeof newline);
1056
1057
1058
		dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
			__func__,
			le32_to_cpu(newline.dwDTERate),
Linus Torvalds's avatar
Linus Torvalds committed
1059
1060
1061
1062
1063
1064
			newline.bCharFormat, newline.bParityType,
			newline.bDataBits);
		acm_set_line(acm, &acm->line);
	}
}

1065
static const struct tty_port_operations acm_port_ops = {
1066
	.dtr_rts = acm_port_dtr_rts,
1067
1068
1069
1070
1071
	.shutdown = acm_port_shutdown,
	.activate = acm_port_activate,
	.destruct = acm_port_destruct,
};

Linus Torvalds's avatar
Linus Torvalds committed
1072
1073
1074
1075
/*
 * USB probe and disconnect routines.
 */

1076
/* Little helpers: write/read buffers free */
1077
1078
1079
1080
1081
static void acm_write_buffers_free(struct acm *acm)
{
	int i;
	struct acm_wb *wb;

1082
	for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
1083
		usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
1084
1085
}

1086
1087
static void acm_read_buffers_free(struct acm *acm)
{
1088
	int i;
1089

1090
	for (i = 0; i < acm->rx_buflimit; i++)
1091
		usb_free_coherent(acm->dev, acm->readsize,
1092
			  acm->read_buffers[i].base, acm->read_buffers[i].dma);
1093
1094
}

1095
1096
1097
1098
1099
1100
/* Little helper: write buffers allocate */
static int acm_write_buffers_alloc(struct acm *acm)
{
	int i;
	struct acm_wb *wb;

1101
	for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
1102
		wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
1103
1104
1105
1106
1107
		    &wb->dmah);
		if (!wb->buf) {
			while (i != 0) {
				--i;
				--wb;
1108
				usb_free_coherent(acm->dev, acm->writesize,
1109
1110
1111
1112
1113
1114
1115
1116
				    wb->buf, wb->dmah);
			}
			return -ENOMEM;
		}
	}
	return 0;
}

Alan Cox's avatar
Alan Cox committed
1117
1118
static int acm_probe(struct usb_interface *intf,
		     const struct usb_device_id *id)
Linus Torvalds's avatar
Linus Torvalds committed
1119
1120
{
	struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukum's avatar
Oliver Neukum committed
1121
	struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
David Brownell's avatar
David Brownell committed
1122
	unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds's avatar
Linus Torvalds committed
1123
1124
1125
	int buflen = intf->altsetting->extralen;
	struct usb_interface *control_interface;
	struct usb_interface *data_interface;
1126
1127
1128
	struct usb_endpoint_descriptor *epctrl = NULL;
	struct usb_endpoint_descriptor *epread = NULL;
	struct usb_endpoint_descriptor *epwrite = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1129
	struct usb_device *usb_dev = interface_to_usbdev(intf);
Oliver Neukum's avatar
Oliver Neukum committed
1130
	struct usb_cdc_parsed_header h;
Linus Torvalds's avatar
Linus Torvalds committed
1131
1132
	struct acm *acm;
	int minor;
1133
	int ctrlsize, readsize;
Linus Torvalds's avatar
Linus Torvalds committed
1134
	u8 *buf;
Oliver Neukum's avatar
Oliver Neukum committed
1135
1