at v3.9-rc2 526 lines 14 kB view raw
1/* 2 * USB Serial Converter Generic functions 3 * 4 * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com) 5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License version 9 * 2 as published by the Free Software Foundation. 10 * 11 */ 12 13#include <linux/kernel.h> 14#include <linux/errno.h> 15#include <linux/slab.h> 16#include <linux/sysrq.h> 17#include <linux/tty.h> 18#include <linux/tty_flip.h> 19#include <linux/module.h> 20#include <linux/moduleparam.h> 21#include <linux/usb.h> 22#include <linux/usb/serial.h> 23#include <linux/uaccess.h> 24#include <linux/kfifo.h> 25#include <linux/serial.h> 26 27#ifdef CONFIG_USB_SERIAL_GENERIC 28 29static __u16 vendor = 0x05f9; 30static __u16 product = 0xffff; 31 32module_param(vendor, ushort, 0); 33MODULE_PARM_DESC(vendor, "User specified USB idVendor"); 34 35module_param(product, ushort, 0); 36MODULE_PARM_DESC(product, "User specified USB idProduct"); 37 38static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */ 39 40/* All of the device info needed for the Generic Serial Converter */ 41struct usb_serial_driver usb_serial_generic_device = { 42 .driver = { 43 .owner = THIS_MODULE, 44 .name = "generic", 45 }, 46 .id_table = generic_device_ids, 47 .num_ports = 1, 48 .disconnect = usb_serial_generic_disconnect, 49 .release = usb_serial_generic_release, 50 .throttle = usb_serial_generic_throttle, 51 .unthrottle = usb_serial_generic_unthrottle, 52 .resume = usb_serial_generic_resume, 53}; 54 55static struct usb_serial_driver * const serial_drivers[] = { 56 &usb_serial_generic_device, NULL 57}; 58 59#endif 60 61int usb_serial_generic_register(void) 62{ 63 int retval = 0; 64 65#ifdef CONFIG_USB_SERIAL_GENERIC 66 generic_device_ids[0].idVendor = vendor; 67 generic_device_ids[0].idProduct = product; 68 generic_device_ids[0].match_flags = 69 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; 70 71 /* register our generic driver with ourselves */ 72 retval = usb_serial_register_drivers(serial_drivers, 73 "usbserial_generic", generic_device_ids); 74#endif 75 return retval; 76} 77 78void usb_serial_generic_deregister(void) 79{ 80#ifdef CONFIG_USB_SERIAL_GENERIC 81 /* remove our generic driver */ 82 usb_serial_deregister_drivers(serial_drivers); 83#endif 84} 85 86int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) 87{ 88 int result = 0; 89 unsigned long flags; 90 91 /* clear the throttle flags */ 92 spin_lock_irqsave(&port->lock, flags); 93 port->throttled = 0; 94 port->throttle_req = 0; 95 spin_unlock_irqrestore(&port->lock, flags); 96 97 /* 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); 100 101 return result; 102} 103EXPORT_SYMBOL_GPL(usb_serial_generic_open); 104 105static void generic_cleanup(struct usb_serial_port *port) 106{ 107 struct usb_serial *serial = port->serial; 108 unsigned long flags; 109 int i; 110 111 if (serial->dev) { 112 /* shutdown any bulk transfers that might be going on */ 113 if (port->bulk_out_size) { 114 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) 115 usb_kill_urb(port->write_urbs[i]); 116 117 spin_lock_irqsave(&port->lock, flags); 118 kfifo_reset_out(&port->write_fifo); 119 spin_unlock_irqrestore(&port->lock, flags); 120 } 121 if (port->bulk_in_size) { 122 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) 123 usb_kill_urb(port->read_urbs[i]); 124 } 125 } 126} 127 128void usb_serial_generic_close(struct usb_serial_port *port) 129{ 130 generic_cleanup(port); 131} 132EXPORT_SYMBOL_GPL(usb_serial_generic_close); 133 134int 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 * usb_serial_generic_write_start - kick off an URB write 142 * @port: Pointer to the &struct usb_serial_port data 143 * 144 * Returns zero on success, or a negative errno value 145 */ 146static int usb_serial_generic_write_start(struct usb_serial_port *port) 147{ 148 struct urb *urb; 149 int count, result; 150 unsigned long flags; 151 int i; 152 153 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags)) 154 return 0; 155retry: 156 spin_lock_irqsave(&port->lock, flags); 157 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) { 158 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); 159 spin_unlock_irqrestore(&port->lock, flags); 160 return 0; 161 } 162 i = (int)find_first_bit(&port->write_urbs_free, 163 ARRAY_SIZE(port->write_urbs)); 164 spin_unlock_irqrestore(&port->lock, flags); 165 166 urb = port->write_urbs[i]; 167 count = port->serial->type->prepare_write_buffer(port, 168 urb->transfer_buffer, 169 port->bulk_out_size); 170 urb->transfer_buffer_length = count; 171 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer); 172 spin_lock_irqsave(&port->lock, flags); 173 port->tx_bytes += count; 174 spin_unlock_irqrestore(&port->lock, flags); 175 176 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); 181 set_bit(i, &port->write_urbs_free); 182 spin_lock_irqsave(&port->lock, flags); 183 port->tx_bytes -= count; 184 spin_unlock_irqrestore(&port->lock, flags); 185 186 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); 187 return result; 188 } 189 190 /* Try sending off another urb, unless in irq context (in which case 191 * there will be no free urb). */ 192 if (!in_irq()) 193 goto retry; 194 195 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); 196 197 return 0; 198} 199 200/** 201 * usb_serial_generic_write - generic write function for serial USB devices 202 * @tty: Pointer to &struct tty_struct for the device 203 * @port: Pointer to the &usb_serial_port structure for the device 204 * @buf: Pointer to the data to write 205 * @count: Number of bytes to write 206 * 207 * Returns the number of characters actually written, which may be anything 208 * from zero to @count. If an error occurs, it returns the negative errno 209 * value. 210 */ 211int usb_serial_generic_write(struct tty_struct *tty, 212 struct usb_serial_port *port, const unsigned char *buf, int count) 213{ 214 int result; 215 216 /* only do something if we have a bulk out endpoint */ 217 if (!port->bulk_out_size) 218 return -ENODEV; 219 220 if (!count) 221 return 0; 222 223 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); 224 result = usb_serial_generic_write_start(port); 225 if (result) 226 return result; 227 228 return count; 229} 230EXPORT_SYMBOL_GPL(usb_serial_generic_write); 231 232int usb_serial_generic_write_room(struct tty_struct *tty) 233{ 234 struct usb_serial_port *port = tty->driver_data; 235 unsigned long flags; 236 int room; 237 238 if (!port->bulk_out_size) 239 return 0; 240 241 spin_lock_irqsave(&port->lock, flags); 242 room = kfifo_avail(&port->write_fifo); 243 spin_unlock_irqrestore(&port->lock, flags); 244 245 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 246 return room; 247} 248 249int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) 250{ 251 struct usb_serial_port *port = tty->driver_data; 252 unsigned long flags; 253 int chars; 254 255 if (!port->bulk_out_size) 256 return 0; 257 258 spin_lock_irqsave(&port->lock, flags); 259 chars = kfifo_len(&port->write_fifo) + port->tx_bytes; 260 spin_unlock_irqrestore(&port->lock, flags); 261 262 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 263 return chars; 264} 265EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer); 266 267static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, 268 int index, gfp_t mem_flags) 269{ 270 int res; 271 272 if (!test_and_clear_bit(index, &port->read_urbs_free)) 273 return 0; 274 275 dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__, 276 port->number, index); 277 278 res = usb_submit_urb(port->read_urbs[index], mem_flags); 279 if (res) { 280 if (res != -EPERM) { 281 dev_err(&port->dev, 282 "%s - usb_submit_urb failed: %d\n", 283 __func__, res); 284 } 285 set_bit(index, &port->read_urbs_free); 286 return res; 287 } 288 289 return 0; 290} 291 292int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port, 293 gfp_t mem_flags) 294{ 295 int res; 296 int i; 297 298 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) { 299 res = usb_serial_generic_submit_read_urb(port, i, mem_flags); 300 if (res) 301 goto err; 302 } 303 304 return 0; 305err: 306 for (; i >= 0; --i) 307 usb_kill_urb(port->read_urbs[i]); 308 309 return res; 310} 311EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs); 312 313void usb_serial_generic_process_read_urb(struct urb *urb) 314{ 315 struct usb_serial_port *port = urb->context; 316 char *ch = (char *)urb->transfer_buffer; 317 int i; 318 319 if (!urb->actual_length) 320 return; 321 322 /* The per character mucking around with sysrq path it too slow for 323 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases 324 where the USB serial is not a console anyway */ 325 if (!port->port.console || !port->sysrq) 326 tty_insert_flip_string(&port->port, ch, urb->actual_length); 327 else { 328 for (i = 0; i < urb->actual_length; i++, ch++) { 329 if (!usb_serial_handle_sysrq_char(port, *ch)) 330 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL); 331 } 332 } 333 tty_flip_buffer_push(&port->port); 334} 335EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb); 336 337void usb_serial_generic_read_bulk_callback(struct urb *urb) 338{ 339 struct usb_serial_port *port = urb->context; 340 unsigned char *data = urb->transfer_buffer; 341 unsigned long flags; 342 int i; 343 344 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) { 345 if (urb == port->read_urbs[i]) 346 break; 347 } 348 set_bit(i, &port->read_urbs_free); 349 350 dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n", 351 __func__, port->number, i, urb->actual_length); 352 353 if (urb->status) { 354 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n", 355 __func__, urb->status); 356 return; 357 } 358 359 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); 360 port->serial->type->process_read_urb(urb); 361 362 /* Throttle the device if requested by tty */ 363 spin_lock_irqsave(&port->lock, flags); 364 port->throttled = port->throttle_req; 365 if (!port->throttled) { 366 spin_unlock_irqrestore(&port->lock, flags); 367 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC); 368 } else 369 spin_unlock_irqrestore(&port->lock, flags); 370} 371EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); 372 373void usb_serial_generic_write_bulk_callback(struct urb *urb) 374{ 375 unsigned long flags; 376 struct usb_serial_port *port = urb->context; 377 int status = urb->status; 378 int i; 379 380 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) 381 if (port->write_urbs[i] == urb) 382 break; 383 384 spin_lock_irqsave(&port->lock, flags); 385 port->tx_bytes -= urb->transfer_buffer_length; 386 set_bit(i, &port->write_urbs_free); 387 spin_unlock_irqrestore(&port->lock, flags); 388 389 if (status) { 390 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n", 391 __func__, status); 392 393 spin_lock_irqsave(&port->lock, flags); 394 kfifo_reset_out(&port->write_fifo); 395 spin_unlock_irqrestore(&port->lock, flags); 396 } else { 397 usb_serial_generic_write_start(port); 398 } 399 400 usb_serial_port_softint(port); 401} 402EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); 403 404void usb_serial_generic_throttle(struct tty_struct *tty) 405{ 406 struct usb_serial_port *port = tty->driver_data; 407 unsigned long flags; 408 409 /* Set the throttle request flag. It will be picked up 410 * by usb_serial_generic_read_bulk_callback(). */ 411 spin_lock_irqsave(&port->lock, flags); 412 port->throttle_req = 1; 413 spin_unlock_irqrestore(&port->lock, flags); 414} 415EXPORT_SYMBOL_GPL(usb_serial_generic_throttle); 416 417void usb_serial_generic_unthrottle(struct tty_struct *tty) 418{ 419 struct usb_serial_port *port = tty->driver_data; 420 int was_throttled; 421 422 /* Clear the throttle flags */ 423 spin_lock_irq(&port->lock); 424 was_throttled = port->throttled; 425 port->throttled = port->throttle_req = 0; 426 spin_unlock_irq(&port->lock); 427 428 if (was_throttled) 429 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL); 430} 431EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle); 432 433#ifdef CONFIG_MAGIC_SYSRQ 434int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch) 435{ 436 if (port->sysrq && port->port.console) { 437 if (ch && time_before(jiffies, port->sysrq)) { 438 handle_sysrq(ch); 439 port->sysrq = 0; 440 return 1; 441 } 442 port->sysrq = 0; 443 } 444 return 0; 445} 446#else 447int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch) 448{ 449 return 0; 450} 451#endif 452EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); 453 454int usb_serial_handle_break(struct usb_serial_port *port) 455{ 456 if (!port->sysrq) { 457 port->sysrq = jiffies + HZ*5; 458 return 1; 459 } 460 port->sysrq = 0; 461 return 0; 462} 463EXPORT_SYMBOL_GPL(usb_serial_handle_break); 464 465/** 466 * usb_serial_handle_dcd_change - handle a change of carrier detect state 467 * @port: usb_serial_port structure for the open port 468 * @tty: tty_struct structure for the port 469 * @status: new carrier detect status, nonzero if active 470 */ 471void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port, 472 struct tty_struct *tty, unsigned int status) 473{ 474 struct tty_port *port = &usb_port->port; 475 476 dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__, 477 usb_port->number, status); 478 479 if (status) 480 wake_up_interruptible(&port->open_wait); 481 else if (tty && !C_CLOCAL(tty)) 482 tty_hangup(tty); 483} 484EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change); 485 486int usb_serial_generic_resume(struct usb_serial *serial) 487{ 488 struct usb_serial_port *port; 489 int i, c = 0, r; 490 491 for (i = 0; i < serial->num_ports; i++) { 492 port = serial->port[i]; 493 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) 494 continue; 495 496 if (port->bulk_in_size) { 497 r = usb_serial_generic_submit_read_urbs(port, 498 GFP_NOIO); 499 if (r < 0) 500 c++; 501 } 502 503 if (port->bulk_out_size) { 504 r = usb_serial_generic_write_start(port); 505 if (r < 0) 506 c++; 507 } 508 } 509 510 return c ? -EIO : 0; 511} 512EXPORT_SYMBOL_GPL(usb_serial_generic_resume); 513 514void usb_serial_generic_disconnect(struct usb_serial *serial) 515{ 516 int i; 517 518 /* stop reads and writes on all ports */ 519 for (i = 0; i < serial->num_ports; ++i) 520 generic_cleanup(serial->port[i]); 521} 522EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect); 523 524void usb_serial_generic_release(struct usb_serial *serial) 525{ 526}