generic.c 13.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * USB Serial Converter Generic functions
 *
4
 * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com)
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License version
 *	2 as published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
16
#include <linux/sysrq.h>
L
Linus Torvalds 已提交
17 18 19 20 21
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/usb.h>
22
#include <linux/usb/serial.h>
A
Alan Cox 已提交
23
#include <linux/uaccess.h>
24
#include <linux/kfifo.h>
25
#include <linux/serial.h>
26

L
Linus Torvalds 已提交
27
#ifdef CONFIG_USB_SERIAL_GENERIC
28

L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40
static __u16 vendor  = 0x05f9;
static __u16 product = 0xffff;

module_param(vendor, ushort, 0);
MODULE_PARM_DESC(vendor, "User specified USB idVendor");

module_param(product, ushort, 0);
MODULE_PARM_DESC(product, "User specified USB idProduct");

static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */

/* All of the device info needed for the Generic Serial Converter */
41
struct usb_serial_driver usb_serial_generic_device = {
42 43
	.driver = {
		.owner =	THIS_MODULE,
44
		.name =		"generic",
45
	},
L
Linus Torvalds 已提交
46 47
	.id_table =		generic_device_ids,
	.num_ports =		1,
48 49
	.disconnect =		usb_serial_generic_disconnect,
	.release =		usb_serial_generic_release,
50 51
	.throttle =		usb_serial_generic_throttle,
	.unthrottle =		usb_serial_generic_unthrottle,
52
	.resume =		usb_serial_generic_resume,
L
Linus Torvalds 已提交
53 54
};

55 56 57 58
static struct usb_serial_driver * const serial_drivers[] = {
	&usb_serial_generic_device, NULL
};

L
Linus Torvalds 已提交
59 60
#endif

61
int usb_serial_generic_register(void)
L
Linus Torvalds 已提交
62 63 64 65 66 67
{
	int retval = 0;

#ifdef CONFIG_USB_SERIAL_GENERIC
	generic_device_ids[0].idVendor = vendor;
	generic_device_ids[0].idProduct = product;
A
Alan Cox 已提交
68 69
	generic_device_ids[0].match_flags =
		USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
L
Linus Torvalds 已提交
70 71

	/* register our generic driver with ourselves */
72 73
	retval = usb_serial_register_drivers(serial_drivers,
			"usbserial_generic", generic_device_ids);
L
Linus Torvalds 已提交
74 75 76 77
#endif
	return retval;
}

A
Alan Cox 已提交
78
void usb_serial_generic_deregister(void)
L
Linus Torvalds 已提交
79 80 81
{
#ifdef CONFIG_USB_SERIAL_GENERIC
	/* remove our generic driver */
82
	usb_serial_deregister_drivers(serial_drivers);
L
Linus Torvalds 已提交
83 84 85
#endif
}

86
int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
87 88
{
	int result = 0;
89
	unsigned long flags;
L
Linus Torvalds 已提交
90

91 92 93 94 95 96 97
	/* clear the throttle flags */
	spin_lock_irqsave(&port->lock, flags);
	port->throttled = 0;
	port->throttle_req = 0;
	spin_unlock_irqrestore(&port->lock, flags);

	/* if we have a bulk endpoint, start reading from it */
98
	if (port->bulk_in_size)
99
		result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
L
Linus Torvalds 已提交
100 101 102

	return result;
}
103
EXPORT_SYMBOL_GPL(usb_serial_generic_open);
L
Linus Torvalds 已提交
104

A
Alan Cox 已提交
105
static void generic_cleanup(struct usb_serial_port *port)
L
Linus Torvalds 已提交
106 107
{
	struct usb_serial *serial = port->serial;
J
Johan Hovold 已提交
108
	unsigned long flags;
109
	int i;
L
Linus Torvalds 已提交
110 111

	if (serial->dev) {
112
		/* shutdown any bulk transfers that might be going on */
J
Johan Hovold 已提交
113
		if (port->bulk_out_size) {
114 115
			for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
				usb_kill_urb(port->write_urbs[i]);
J
Johan Hovold 已提交
116 117 118 119 120

			spin_lock_irqsave(&port->lock, flags);
			kfifo_reset_out(&port->write_fifo);
			spin_unlock_irqrestore(&port->lock, flags);
		}
121 122 123 124
		if (port->bulk_in_size) {
			for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
				usb_kill_urb(port->read_urbs[i]);
		}
L
Linus Torvalds 已提交
125 126 127
	}
}

128
void usb_serial_generic_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
129
{
A
Alan Cox 已提交
130
	generic_cleanup(port);
L
Linus Torvalds 已提交
131
}
132
EXPORT_SYMBOL_GPL(usb_serial_generic_close);
L
Linus Torvalds 已提交
133

134
int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
135
						void *dest, size_t size)
136
{
137
	return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
138 139
}

140 141 142 143
/**
 * usb_serial_generic_write_start - kick off an URB write
 * @port:	Pointer to the &struct usb_serial_port data
 *
144
 * Returns zero on success, or a negative errno value
145 146 147
 */
static int usb_serial_generic_write_start(struct usb_serial_port *port)
{
148 149
	struct urb *urb;
	int count, result;
150
	unsigned long flags;
151
	int i;
152

153 154 155
	if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
		return 0;
retry:
156
	spin_lock_irqsave(&port->lock, flags);
157 158
	if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
		clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
159 160
		spin_unlock_irqrestore(&port->lock, flags);
		return 0;
161
	}
162 163
	i = (int)find_first_bit(&port->write_urbs_free,
						ARRAY_SIZE(port->write_urbs));
164 165
	spin_unlock_irqrestore(&port->lock, flags);

166
	urb = port->write_urbs[i];
167
	count = port->serial->type->prepare_write_buffer(port,
168 169
						urb->transfer_buffer,
						port->bulk_out_size);
170
	urb->transfer_buffer_length = count;
171
	usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
J
Johan Hovold 已提交
172 173 174 175 176
	spin_lock_irqsave(&port->lock, flags);
	port->tx_bytes += count;
	spin_unlock_irqrestore(&port->lock, flags);

	clear_bit(i, &port->write_urbs_free);
177
	result = usb_submit_urb(urb, GFP_ATOMIC);
178
	if (result) {
179
		dev_err_console(port, "%s - error submitting urb: %d\n",
180
						__func__, result);
J
Johan Hovold 已提交
181 182 183 184 185
		set_bit(i, &port->write_urbs_free);
		spin_lock_irqsave(&port->lock, flags);
		port->tx_bytes -= count;
		spin_unlock_irqrestore(&port->lock, flags);

186
		clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
187 188 189
		return result;
	}

190 191 192 193 194 195 196 197
	/* Try sending off another urb, unless in irq context (in which case
	 * there will be no free urb). */
	if (!in_irq())
		goto retry;

	clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);

	return 0;
198 199 200 201 202 203 204 205 206 207 208 209 210
}

/**
 * usb_serial_generic_write - generic write function for serial USB devices
 * @tty:	Pointer to &struct tty_struct for the device
 * @port:	Pointer to the &usb_serial_port structure for the device
 * @buf:	Pointer to the data to write
 * @count:	Number of bytes to write
 *
 * Returns the number of characters actually written, which may be anything
 * from zero to @count. If an error occurs, it returns the negative errno
 * value.
 */
A
Alan Cox 已提交
211 212
int usb_serial_generic_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
213 214 215
{
	int result;

216 217 218 219
	/* only do something if we have a bulk out endpoint */
	if (!port->bulk_out_size)
		return -ENODEV;

220
	if (!count)
A
Alan Cox 已提交
221
		return 0;
L
Linus Torvalds 已提交
222

223
	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
224
	result = usb_serial_generic_write_start(port);
225 226
	if (result)
		return result;
L
Linus Torvalds 已提交
227

228
	return count;
L
Linus Torvalds 已提交
229
}
230
EXPORT_SYMBOL_GPL(usb_serial_generic_write);
L
Linus Torvalds 已提交
231

A
Alan Cox 已提交
232
int usb_serial_generic_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
233
{
A
Alan Cox 已提交
234
	struct usb_serial_port *port = tty->driver_data;
235
	unsigned long flags;
236
	int room;
L
Linus Torvalds 已提交
237

238 239 240
	if (!port->bulk_out_size)
		return 0;

241
	spin_lock_irqsave(&port->lock, flags);
242
	room = kfifo_avail(&port->write_fifo);
243
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
244

245
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
246
	return room;
L
Linus Torvalds 已提交
247 248
}

A
Alan Cox 已提交
249
int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
250
{
A
Alan Cox 已提交
251
	struct usb_serial_port *port = tty->driver_data;
252
	unsigned long flags;
253
	int chars;
L
Linus Torvalds 已提交
254

255 256 257
	if (!port->bulk_out_size)
		return 0;

258
	spin_lock_irqsave(&port->lock, flags);
259
	chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
260
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
261

262
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
A
Alan Cox 已提交
263
	return chars;
L
Linus Torvalds 已提交
264
}
265
EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
L
Linus Torvalds 已提交
266

267 268 269 270 271 272 273 274
static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
						int index, gfp_t mem_flags)
{
	int res;

	if (!test_and_clear_bit(index, &port->read_urbs_free))
		return 0;

275 276
	dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
		port->number, index);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	res = usb_submit_urb(port->read_urbs[index], mem_flags);
	if (res) {
		if (res != -EPERM) {
			dev_err(&port->dev,
					"%s - usb_submit_urb failed: %d\n",
					__func__, res);
		}
		set_bit(index, &port->read_urbs_free);
		return res;
	}

	return 0;
}

int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
293
					gfp_t mem_flags)
L
Linus Torvalds 已提交
294
{
295 296
	int res;
	int i;
L
Linus Torvalds 已提交
297

298 299 300 301
	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
		res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
		if (res)
			goto err;
302
	}
303 304 305 306 307 308 309

	return 0;
err:
	for (; i >= 0; --i)
		usb_kill_urb(port->read_urbs[i]);

	return res;
L
Linus Torvalds 已提交
310
}
311
EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
312

313
void usb_serial_generic_process_read_urb(struct urb *urb)
314
{
315 316
	struct usb_serial_port *port = urb->context;
	struct tty_struct *tty;
317 318 319
	char *ch = (char *)urb->transfer_buffer;
	int i;

320 321 322
	if (!urb->actual_length)
		return;

323
	tty = tty_port_tty_get(&port->port);
324
	if (!tty)
325
		return;
326

327 328 329
	/* The per character mucking around with sysrq path it too slow for
	   stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
	   where the USB serial is not a console anyway */
330
	if (!port->port.console || !port->sysrq)
J
Jiri Slaby 已提交
331
		tty_insert_flip_string(&port->port, ch, urb->actual_length);
332 333
	else {
		for (i = 0; i < urb->actual_length; i++, ch++) {
334
			if (!usb_serial_handle_sysrq_char(port, *ch))
J
Jiri Slaby 已提交
335
				tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
336
		}
337
	}
338
	tty_flip_buffer_push(tty);
A
Alan Cox 已提交
339
	tty_kref_put(tty);
340
}
341
EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
342

A
Alan Cox 已提交
343
void usb_serial_generic_read_bulk_callback(struct urb *urb)
344
{
345
	struct usb_serial_port *port = urb->context;
346
	unsigned char *data = urb->transfer_buffer;
347
	unsigned long flags;
348
	int i;
349

350 351 352 353 354
	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
		if (urb == port->read_urbs[i])
			break;
	}
	set_bit(i, &port->read_urbs_free);
355

356 357 358
	dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
		__func__, port->number, i, urb->actual_length);

359
	if (urb->status) {
360 361
		dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
			__func__, urb->status);
362 363 364
		return;
	}

365
	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
366
	port->serial->type->process_read_urb(urb);
367 368

	/* Throttle the device if requested by tty */
369
	spin_lock_irqsave(&port->lock, flags);
A
Alan Cox 已提交
370 371
	port->throttled = port->throttle_req;
	if (!port->throttled) {
372
		spin_unlock_irqrestore(&port->lock, flags);
373
		usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
A
Alan Cox 已提交
374
	} else
375
		spin_unlock_irqrestore(&port->lock, flags);
376
}
377
EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
L
Linus Torvalds 已提交
378

A
Alan Cox 已提交
379
void usb_serial_generic_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
380
{
381
	unsigned long flags;
382
	struct usb_serial_port *port = urb->context;
383
	int status = urb->status;
384
	int i;
L
Linus Torvalds 已提交
385

386 387 388
	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
		if (port->write_urbs[i] == urb)
			break;
389

390 391 392 393 394 395
	spin_lock_irqsave(&port->lock, flags);
	port->tx_bytes -= urb->transfer_buffer_length;
	set_bit(i, &port->write_urbs_free);
	spin_unlock_irqrestore(&port->lock, flags);

	if (status) {
396 397
		dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
			__func__, status);
398

399
		spin_lock_irqsave(&port->lock, flags);
400
		kfifo_reset_out(&port->write_fifo);
401
		spin_unlock_irqrestore(&port->lock, flags);
402 403
	} else {
		usb_serial_generic_write_start(port);
L
Linus Torvalds 已提交
404
	}
405

406
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
407
}
408
EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
L
Linus Torvalds 已提交
409

A
Alan Cox 已提交
410
void usb_serial_generic_throttle(struct tty_struct *tty)
411
{
A
Alan Cox 已提交
412
	struct usb_serial_port *port = tty->driver_data;
413 414 415 416 417 418 419 420
	unsigned long flags;

	/* Set the throttle request flag. It will be picked up
	 * by usb_serial_generic_read_bulk_callback(). */
	spin_lock_irqsave(&port->lock, flags);
	port->throttle_req = 1;
	spin_unlock_irqrestore(&port->lock, flags);
}
421
EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
422

A
Alan Cox 已提交
423
void usb_serial_generic_unthrottle(struct tty_struct *tty)
424
{
A
Alan Cox 已提交
425
	struct usb_serial_port *port = tty->driver_data;
426 427 428
	int was_throttled;

	/* Clear the throttle flags */
429
	spin_lock_irq(&port->lock);
430 431
	was_throttled = port->throttled;
	port->throttled = port->throttle_req = 0;
432
	spin_unlock_irq(&port->lock);
433

434
	if (was_throttled)
435
		usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
436
}
437
EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
438

439
#ifdef CONFIG_MAGIC_SYSRQ
440
int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
441
{
442
	if (port->sysrq && port->port.console) {
443
		if (ch && time_before(jiffies, port->sysrq)) {
444
			handle_sysrq(ch);
445 446 447 448 449 450 451
			port->sysrq = 0;
			return 1;
		}
		port->sysrq = 0;
	}
	return 0;
}
452
#else
453
int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
454 455 456 457
{
	return 0;
}
#endif
458 459 460 461 462 463 464 465 466 467 468 469 470
EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);

int usb_serial_handle_break(struct usb_serial_port *port)
{
	if (!port->sysrq) {
		port->sysrq = jiffies + HZ*5;
		return 1;
	}
	port->sysrq = 0;
	return 0;
}
EXPORT_SYMBOL_GPL(usb_serial_handle_break);

471 472 473 474 475 476 477 478 479 480 481
/**
 *	usb_serial_handle_dcd_change - handle a change of carrier detect state
 *	@port: usb_serial_port structure for the open port
 *	@tty: tty_struct structure for the port
 *	@status: new carrier detect status, nonzero if active
 */
void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
				struct tty_struct *tty, unsigned int status)
{
	struct tty_port *port = &usb_port->port;

482 483
	dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
		usb_port->number, status);
484 485 486 487 488 489 490 491

	if (status)
		wake_up_interruptible(&port->open_wait);
	else if (tty && !C_CLOCAL(tty))
		tty_hangup(tty);
}
EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);

492 493 494 495 496 497 498
int usb_serial_generic_resume(struct usb_serial *serial)
{
	struct usb_serial_port *port;
	int i, c = 0, r;

	for (i = 0; i < serial->num_ports; i++) {
		port = serial->port[i];
499
		if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
500 501
			continue;

502 503 504
		if (port->bulk_in_size) {
			r = usb_serial_generic_submit_read_urbs(port,
								GFP_NOIO);
505 506 507 508
			if (r < 0)
				c++;
		}

509
		if (port->bulk_out_size) {
510 511 512 513 514 515 516 517 518 519
			r = usb_serial_generic_write_start(port);
			if (r < 0)
				c++;
		}
	}

	return c ? -EIO : 0;
}
EXPORT_SYMBOL_GPL(usb_serial_generic_resume);

520
void usb_serial_generic_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
521 522 523 524
{
	int i;

	/* stop reads and writes on all ports */
A
Alan Cox 已提交
525
	for (i = 0; i < serial->num_ports; ++i)
L
Linus Torvalds 已提交
526 527
		generic_cleanup(serial->port[i]);
}
528
EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
L
Linus Torvalds 已提交
529

530 531 532
void usb_serial_generic_release(struct usb_serial *serial)
{
}