Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB Serial Converter driver |
| 3 | * |
Greg Kroah-Hartman | 502b95c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4 | * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) |
| 6 | * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License version |
| 10 | * 2 as published by the Free Software Foundation. |
| 11 | * |
Greg Kroah-Hartman | 502b95c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 12 | * This driver was originally based on the ACM driver by Armin Fuerst (which was |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * based on a driver by Brad Keryan) |
| 14 | * |
| 15 | * See Documentation/usb/usb-serial.txt for more information on using this driver |
| 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/config.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/tty.h> |
| 25 | #include <linux/tty_driver.h> |
| 26 | #include <linux/tty_flip.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <linux/smp_lock.h> |
| 32 | #include <asm/uaccess.h> |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 33 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/usb.h> |
| 35 | #include "usb-serial.h" |
| 36 | #include "pl2303.h" |
| 37 | |
| 38 | /* |
| 39 | * Version Information |
| 40 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/" |
| 42 | #define DRIVER_DESC "USB Serial Driver core" |
| 43 | |
| 44 | /* Driver structure we register with the USB core */ |
| 45 | static struct usb_driver usb_serial_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | .name = "usbserial", |
| 47 | .probe = usb_serial_probe, |
| 48 | .disconnect = usb_serial_disconnect, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 49 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead |
| 53 | the MODULE_DEVICE_TABLE declarations in each serial driver |
| 54 | cause the "hotplug" program to pull in whatever module is necessary |
| 55 | via modprobe, and modprobe will load usbserial because the serial |
| 56 | drivers depend on it. |
| 57 | */ |
| 58 | |
| 59 | static int debug; |
| 60 | static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */ |
| 61 | static LIST_HEAD(usb_serial_driver_list); |
| 62 | |
| 63 | struct usb_serial *usb_serial_get_by_index(unsigned index) |
| 64 | { |
| 65 | struct usb_serial *serial = serial_table[index]; |
| 66 | |
| 67 | if (serial) |
| 68 | kref_get(&serial->kref); |
| 69 | return serial; |
| 70 | } |
| 71 | |
| 72 | static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor) |
| 73 | { |
| 74 | unsigned int i, j; |
| 75 | int good_spot; |
| 76 | |
| 77 | dbg("%s %d", __FUNCTION__, num_ports); |
| 78 | |
| 79 | *minor = 0; |
| 80 | for (i = 0; i < SERIAL_TTY_MINORS; ++i) { |
| 81 | if (serial_table[i]) |
| 82 | continue; |
| 83 | |
| 84 | good_spot = 1; |
| 85 | for (j = 1; j <= num_ports-1; ++j) |
| 86 | if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) { |
| 87 | good_spot = 0; |
| 88 | i += j; |
| 89 | break; |
| 90 | } |
| 91 | if (good_spot == 0) |
| 92 | continue; |
| 93 | |
| 94 | *minor = i; |
| 95 | dbg("%s - minor base = %d", __FUNCTION__, *minor); |
| 96 | for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) |
| 97 | serial_table[i] = serial; |
| 98 | return serial; |
| 99 | } |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | static void return_serial(struct usb_serial *serial) |
| 104 | { |
| 105 | int i; |
| 106 | |
| 107 | dbg("%s", __FUNCTION__); |
| 108 | |
| 109 | if (serial == NULL) |
| 110 | return; |
| 111 | |
| 112 | for (i = 0; i < serial->num_ports; ++i) { |
| 113 | serial_table[serial->minor + i] = NULL; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void destroy_serial(struct kref *kref) |
| 118 | { |
| 119 | struct usb_serial *serial; |
| 120 | struct usb_serial_port *port; |
| 121 | int i; |
| 122 | |
| 123 | serial = to_usb_serial(kref); |
| 124 | |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 125 | dbg("%s - %s", __FUNCTION__, serial->type->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | serial->type->shutdown(serial); |
| 128 | |
| 129 | /* return the minor range that this device had */ |
| 130 | return_serial(serial); |
| 131 | |
| 132 | for (i = 0; i < serial->num_ports; ++i) |
| 133 | serial->port[i]->open_count = 0; |
| 134 | |
| 135 | /* the ports are cleaned up and released in port_release() */ |
| 136 | for (i = 0; i < serial->num_ports; ++i) |
| 137 | if (serial->port[i]->dev.parent != NULL) { |
| 138 | device_unregister(&serial->port[i]->dev); |
| 139 | serial->port[i] = NULL; |
| 140 | } |
| 141 | |
| 142 | /* If this is a "fake" port, we have to clean it up here, as it will |
| 143 | * not get cleaned up in port_release() as it was never registered with |
| 144 | * the driver core */ |
| 145 | if (serial->num_ports < serial->num_port_pointers) { |
| 146 | for (i = serial->num_ports; i < serial->num_port_pointers; ++i) { |
| 147 | port = serial->port[i]; |
| 148 | if (!port) |
| 149 | continue; |
| 150 | usb_kill_urb(port->read_urb); |
| 151 | usb_free_urb(port->read_urb); |
| 152 | usb_kill_urb(port->write_urb); |
| 153 | usb_free_urb(port->write_urb); |
| 154 | usb_kill_urb(port->interrupt_in_urb); |
| 155 | usb_free_urb(port->interrupt_in_urb); |
| 156 | usb_kill_urb(port->interrupt_out_urb); |
| 157 | usb_free_urb(port->interrupt_out_urb); |
| 158 | kfree(port->bulk_in_buffer); |
| 159 | kfree(port->bulk_out_buffer); |
| 160 | kfree(port->interrupt_in_buffer); |
| 161 | kfree(port->interrupt_out_buffer); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | usb_put_dev(serial->dev); |
| 166 | |
| 167 | /* free up any memory that we allocated */ |
| 168 | kfree (serial); |
| 169 | } |
| 170 | |
| 171 | /***************************************************************************** |
| 172 | * Driver tty interface functions |
| 173 | *****************************************************************************/ |
| 174 | static int serial_open (struct tty_struct *tty, struct file * filp) |
| 175 | { |
| 176 | struct usb_serial *serial; |
| 177 | struct usb_serial_port *port; |
| 178 | unsigned int portNumber; |
| 179 | int retval; |
| 180 | |
| 181 | dbg("%s", __FUNCTION__); |
| 182 | |
| 183 | /* get the serial object associated with this tty pointer */ |
| 184 | serial = usb_serial_get_by_index(tty->index); |
| 185 | if (!serial) { |
| 186 | tty->driver_data = NULL; |
| 187 | return -ENODEV; |
| 188 | } |
| 189 | |
| 190 | portNumber = tty->index - serial->minor; |
| 191 | port = serial->port[portNumber]; |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 192 | if (!port) |
| 193 | return -ENODEV; |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 194 | |
| 195 | if (down_interruptible(&port->sem)) |
| 196 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | ++port->open_count; |
| 199 | |
| 200 | if (port->open_count == 1) { |
| 201 | |
| 202 | /* set up our port structure making the tty driver |
| 203 | * remember our port object, and us it */ |
| 204 | tty->driver_data = port; |
| 205 | port->tty = tty; |
| 206 | |
| 207 | /* lock this module before we call it |
| 208 | * this may fail, which means we must bail out, |
| 209 | * safe because we are called with BKL held */ |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 210 | if (!try_module_get(serial->type->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | retval = -ENODEV; |
| 212 | goto bailout_kref_put; |
| 213 | } |
| 214 | |
| 215 | /* only call the device specific open if this |
| 216 | * is the first time the port is opened */ |
| 217 | retval = serial->type->open(port, filp); |
| 218 | if (retval) |
| 219 | goto bailout_module_put; |
| 220 | } |
| 221 | |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 222 | up(&port->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | |
| 225 | bailout_module_put: |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 226 | module_put(serial->type->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | bailout_kref_put: |
| 228 | kref_put(&serial->kref, destroy_serial); |
| 229 | port->open_count = 0; |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 230 | up(&port->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return retval; |
| 232 | } |
| 233 | |
| 234 | static void serial_close(struct tty_struct *tty, struct file * filp) |
| 235 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 236 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | if (!port) |
| 239 | return; |
| 240 | |
| 241 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 242 | |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 243 | down(&port->sem); |
| 244 | |
Greg Kroah-Hartman | 91c0bce | 2006-03-06 13:25:52 -0800 | [diff] [blame] | 245 | if (port->open_count == 0) { |
| 246 | up(&port->sem); |
| 247 | return; |
| 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | --port->open_count; |
| 251 | if (port->open_count == 0) { |
| 252 | /* only call the device specific close if this |
| 253 | * port is being closed by the last owner */ |
| 254 | port->serial->type->close(port, filp); |
| 255 | |
| 256 | if (port->tty) { |
| 257 | if (port->tty->driver_data) |
| 258 | port->tty->driver_data = NULL; |
| 259 | port->tty = NULL; |
| 260 | } |
| 261 | |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 262 | module_put(port->serial->type->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 265 | up(&port->sem); |
Greg Kroah-Hartman | 91c0bce | 2006-03-06 13:25:52 -0800 | [diff] [blame] | 266 | kref_put(&port->serial->kref, destroy_serial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count) |
| 270 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 271 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | int retval = -EINVAL; |
| 273 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 274 | if (!port) |
| 275 | goto exit; |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count); |
| 278 | |
| 279 | if (!port->open_count) { |
| 280 | dbg("%s - port not opened", __FUNCTION__); |
| 281 | goto exit; |
| 282 | } |
| 283 | |
| 284 | /* pass on to the driver specific version of this function */ |
| 285 | retval = port->serial->type->write(port, buf, count); |
| 286 | |
| 287 | exit: |
| 288 | return retval; |
| 289 | } |
| 290 | |
| 291 | static int serial_write_room (struct tty_struct *tty) |
| 292 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 293 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | int retval = -EINVAL; |
| 295 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 296 | if (!port) |
| 297 | goto exit; |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 300 | |
| 301 | if (!port->open_count) { |
| 302 | dbg("%s - port not open", __FUNCTION__); |
| 303 | goto exit; |
| 304 | } |
| 305 | |
| 306 | /* pass on to the driver specific version of this function */ |
| 307 | retval = port->serial->type->write_room(port); |
| 308 | |
| 309 | exit: |
| 310 | return retval; |
| 311 | } |
| 312 | |
| 313 | static int serial_chars_in_buffer (struct tty_struct *tty) |
| 314 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 315 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | int retval = -EINVAL; |
| 317 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 318 | if (!port) |
| 319 | goto exit; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | dbg("%s = port %d", __FUNCTION__, port->number); |
| 322 | |
| 323 | if (!port->open_count) { |
| 324 | dbg("%s - port not open", __FUNCTION__); |
| 325 | goto exit; |
| 326 | } |
| 327 | |
| 328 | /* pass on to the driver specific version of this function */ |
| 329 | retval = port->serial->type->chars_in_buffer(port); |
| 330 | |
| 331 | exit: |
| 332 | return retval; |
| 333 | } |
| 334 | |
| 335 | static void serial_throttle (struct tty_struct * tty) |
| 336 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 337 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 339 | if (!port) |
| 340 | return; |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 343 | |
| 344 | if (!port->open_count) { |
| 345 | dbg ("%s - port not open", __FUNCTION__); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | /* pass on to the driver specific version of this function */ |
| 350 | if (port->serial->type->throttle) |
| 351 | port->serial->type->throttle(port); |
| 352 | } |
| 353 | |
| 354 | static void serial_unthrottle (struct tty_struct * tty) |
| 355 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 356 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 358 | if (!port) |
| 359 | return; |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 362 | |
| 363 | if (!port->open_count) { |
| 364 | dbg("%s - port not open", __FUNCTION__); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | /* pass on to the driver specific version of this function */ |
| 369 | if (port->serial->type->unthrottle) |
| 370 | port->serial->type->unthrottle(port); |
| 371 | } |
| 372 | |
| 373 | static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg) |
| 374 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 375 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | int retval = -ENODEV; |
| 377 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 378 | if (!port) |
| 379 | goto exit; |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); |
| 382 | |
| 383 | if (!port->open_count) { |
| 384 | dbg ("%s - port not open", __FUNCTION__); |
| 385 | goto exit; |
| 386 | } |
| 387 | |
| 388 | /* pass on to the driver specific version of this function if it is available */ |
| 389 | if (port->serial->type->ioctl) |
| 390 | retval = port->serial->type->ioctl(port, file, cmd, arg); |
| 391 | else |
| 392 | retval = -ENOIOCTLCMD; |
| 393 | |
| 394 | exit: |
| 395 | return retval; |
| 396 | } |
| 397 | |
| 398 | static void serial_set_termios (struct tty_struct *tty, struct termios * old) |
| 399 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 400 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 402 | if (!port) |
| 403 | return; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 406 | |
| 407 | if (!port->open_count) { |
| 408 | dbg("%s - port not open", __FUNCTION__); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | /* pass on to the driver specific version of this function if it is available */ |
| 413 | if (port->serial->type->set_termios) |
| 414 | port->serial->type->set_termios(port, old); |
| 415 | } |
| 416 | |
| 417 | static void serial_break (struct tty_struct *tty, int break_state) |
| 418 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 419 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 421 | if (!port) |
| 422 | return; |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 425 | |
| 426 | if (!port->open_count) { |
| 427 | dbg("%s - port not open", __FUNCTION__); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | /* pass on to the driver specific version of this function if it is available */ |
| 432 | if (port->serial->type->break_ctl) |
| 433 | port->serial->type->break_ctl(port, break_state); |
| 434 | } |
| 435 | |
| 436 | static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 437 | { |
| 438 | struct usb_serial *serial; |
| 439 | int length = 0; |
| 440 | int i; |
| 441 | off_t begin = 0; |
| 442 | char tmp[40]; |
| 443 | |
| 444 | dbg("%s", __FUNCTION__); |
Greg Kroah-Hartman | 17a882f | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 445 | length += sprintf (page, "usbserinfo:1.0 driver:2.0\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) { |
| 447 | serial = usb_serial_get_by_index(i); |
| 448 | if (serial == NULL) |
| 449 | continue; |
| 450 | |
| 451 | length += sprintf (page+length, "%d:", i); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 452 | if (serial->type->driver.owner) |
| 453 | length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner)); |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 454 | length += sprintf (page+length, " name:\"%s\"", serial->type->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | length += sprintf (page+length, " vendor:%04x product:%04x", |
| 456 | le16_to_cpu(serial->dev->descriptor.idVendor), |
| 457 | le16_to_cpu(serial->dev->descriptor.idProduct)); |
| 458 | length += sprintf (page+length, " num_ports:%d", serial->num_ports); |
| 459 | length += sprintf (page+length, " port:%d", i - serial->minor + 1); |
| 460 | |
| 461 | usb_make_path(serial->dev, tmp, sizeof(tmp)); |
| 462 | length += sprintf (page+length, " path:%s", tmp); |
| 463 | |
| 464 | length += sprintf (page+length, "\n"); |
| 465 | if ((length + begin) > (off + count)) |
| 466 | goto done; |
| 467 | if ((length + begin) < off) { |
| 468 | begin += length; |
| 469 | length = 0; |
| 470 | } |
| 471 | kref_put(&serial->kref, destroy_serial); |
| 472 | } |
| 473 | *eof = 1; |
| 474 | done: |
| 475 | if (off >= (length + begin)) |
| 476 | return 0; |
| 477 | *start = page + (off-begin); |
| 478 | return ((count < begin+length-off) ? count : begin+length-off); |
| 479 | } |
| 480 | |
| 481 | static int serial_tiocmget (struct tty_struct *tty, struct file *file) |
| 482 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 483 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 485 | if (!port) |
| 486 | goto exit; |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 489 | |
| 490 | if (!port->open_count) { |
| 491 | dbg("%s - port not open", __FUNCTION__); |
| 492 | goto exit; |
| 493 | } |
| 494 | |
| 495 | if (port->serial->type->tiocmget) |
| 496 | return port->serial->type->tiocmget(port, file); |
| 497 | |
| 498 | exit: |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
| 502 | static int serial_tiocmset (struct tty_struct *tty, struct file *file, |
| 503 | unsigned int set, unsigned int clear) |
| 504 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 505 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Luiz Fernando Capitulino | 487f9c6 | 2005-11-28 19:16:05 -0200 | [diff] [blame] | 507 | if (!port) |
| 508 | goto exit; |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 511 | |
| 512 | if (!port->open_count) { |
| 513 | dbg("%s - port not open", __FUNCTION__); |
| 514 | goto exit; |
| 515 | } |
| 516 | |
| 517 | if (port->serial->type->tiocmset) |
| 518 | return port->serial->type->tiocmset(port, file, set, clear); |
| 519 | |
| 520 | exit: |
| 521 | return -EINVAL; |
| 522 | } |
| 523 | |
| 524 | void usb_serial_port_softint(void *private) |
| 525 | { |
Tobias Klauser | 81671dd | 2005-07-04 19:32:51 +0200 | [diff] [blame] | 526 | struct usb_serial_port *port = private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | struct tty_struct *tty; |
| 528 | |
| 529 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 530 | |
| 531 | if (!port) |
| 532 | return; |
| 533 | |
| 534 | tty = port->tty; |
| 535 | if (!tty) |
| 536 | return; |
| 537 | |
| 538 | tty_wakeup(tty); |
| 539 | } |
| 540 | |
| 541 | static void port_release(struct device *dev) |
| 542 | { |
| 543 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 544 | |
| 545 | dbg ("%s - %s", __FUNCTION__, dev->bus_id); |
| 546 | usb_kill_urb(port->read_urb); |
| 547 | usb_free_urb(port->read_urb); |
| 548 | usb_kill_urb(port->write_urb); |
| 549 | usb_free_urb(port->write_urb); |
| 550 | usb_kill_urb(port->interrupt_in_urb); |
| 551 | usb_free_urb(port->interrupt_in_urb); |
| 552 | usb_kill_urb(port->interrupt_out_urb); |
| 553 | usb_free_urb(port->interrupt_out_urb); |
| 554 | kfree(port->bulk_in_buffer); |
| 555 | kfree(port->bulk_out_buffer); |
| 556 | kfree(port->interrupt_in_buffer); |
| 557 | kfree(port->interrupt_out_buffer); |
| 558 | kfree(port); |
| 559 | } |
| 560 | |
| 561 | static struct usb_serial * create_serial (struct usb_device *dev, |
| 562 | struct usb_interface *interface, |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 563 | struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | struct usb_serial *serial; |
| 566 | |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 567 | serial = kzalloc(sizeof(*serial), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (!serial) { |
| 569 | dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__); |
| 570 | return NULL; |
| 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | serial->dev = usb_get_dev(dev); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 573 | serial->type = driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | serial->interface = interface; |
| 575 | kref_init(&serial->kref); |
| 576 | |
| 577 | return serial; |
| 578 | } |
| 579 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 580 | static struct usb_serial_driver *search_serial_device(struct usb_interface *iface) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
| 582 | struct list_head *p; |
| 583 | const struct usb_device_id *id; |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 584 | struct usb_serial_driver *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Adrian Bunk | 93b1fae | 2006-01-10 00:13:33 +0100 | [diff] [blame] | 586 | /* Check if the usb id matches a known device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | list_for_each(p, &usb_serial_driver_list) { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 588 | t = list_entry(p, struct usb_serial_driver, driver_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | id = usb_match_id(iface, t->id_table); |
| 590 | if (id != NULL) { |
| 591 | dbg("descriptor matches"); |
| 592 | return t; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | int usb_serial_probe(struct usb_interface *interface, |
| 600 | const struct usb_device_id *id) |
| 601 | { |
| 602 | struct usb_device *dev = interface_to_usbdev (interface); |
| 603 | struct usb_serial *serial = NULL; |
| 604 | struct usb_serial_port *port; |
| 605 | struct usb_host_interface *iface_desc; |
| 606 | struct usb_endpoint_descriptor *endpoint; |
| 607 | struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS]; |
| 608 | struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS]; |
| 609 | struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS]; |
| 610 | struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS]; |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 611 | struct usb_serial_driver *type = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | int retval; |
| 613 | int minor; |
| 614 | int buffer_size; |
| 615 | int i; |
| 616 | int num_interrupt_in = 0; |
| 617 | int num_interrupt_out = 0; |
| 618 | int num_bulk_in = 0; |
| 619 | int num_bulk_out = 0; |
| 620 | int num_ports = 0; |
| 621 | int max_endpoints; |
| 622 | |
| 623 | type = search_serial_device(interface); |
| 624 | if (!type) { |
| 625 | dbg("none matched"); |
| 626 | return -ENODEV; |
| 627 | } |
| 628 | |
| 629 | serial = create_serial (dev, interface, type); |
| 630 | if (!serial) { |
| 631 | dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__); |
| 632 | return -ENOMEM; |
| 633 | } |
| 634 | |
| 635 | /* if this device type has a probe function, call it */ |
| 636 | if (type->probe) { |
| 637 | const struct usb_device_id *id; |
| 638 | |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 639 | if (!try_module_get(type->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | dev_err(&interface->dev, "module get failed, exiting\n"); |
| 641 | kfree (serial); |
| 642 | return -EIO; |
| 643 | } |
| 644 | |
| 645 | id = usb_match_id(interface, type->id_table); |
| 646 | retval = type->probe(serial, id); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 647 | module_put(type->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | if (retval) { |
| 650 | dbg ("sub driver rejected device"); |
| 651 | kfree (serial); |
| 652 | return retval; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* descriptor matches, let's find the endpoints needed */ |
| 657 | /* check out the endpoints */ |
| 658 | iface_desc = interface->cur_altsetting; |
| 659 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 660 | endpoint = &iface_desc->endpoint[i].desc; |
| 661 | |
| 662 | if ((endpoint->bEndpointAddress & 0x80) && |
| 663 | ((endpoint->bmAttributes & 3) == 0x02)) { |
| 664 | /* we found a bulk in endpoint */ |
| 665 | dbg("found bulk in on endpoint %d", i); |
| 666 | bulk_in_endpoint[num_bulk_in] = endpoint; |
| 667 | ++num_bulk_in; |
| 668 | } |
| 669 | |
| 670 | if (((endpoint->bEndpointAddress & 0x80) == 0x00) && |
| 671 | ((endpoint->bmAttributes & 3) == 0x02)) { |
| 672 | /* we found a bulk out endpoint */ |
| 673 | dbg("found bulk out on endpoint %d", i); |
| 674 | bulk_out_endpoint[num_bulk_out] = endpoint; |
| 675 | ++num_bulk_out; |
| 676 | } |
| 677 | |
| 678 | if ((endpoint->bEndpointAddress & 0x80) && |
| 679 | ((endpoint->bmAttributes & 3) == 0x03)) { |
| 680 | /* we found a interrupt in endpoint */ |
| 681 | dbg("found interrupt in on endpoint %d", i); |
| 682 | interrupt_in_endpoint[num_interrupt_in] = endpoint; |
| 683 | ++num_interrupt_in; |
| 684 | } |
| 685 | |
| 686 | if (((endpoint->bEndpointAddress & 0x80) == 0x00) && |
| 687 | ((endpoint->bmAttributes & 3) == 0x03)) { |
| 688 | /* we found an interrupt out endpoint */ |
| 689 | dbg("found interrupt out on endpoint %d", i); |
| 690 | interrupt_out_endpoint[num_interrupt_out] = endpoint; |
| 691 | ++num_interrupt_out; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE) |
| 696 | /* BEGIN HORRIBLE HACK FOR PL2303 */ |
| 697 | /* this is needed due to the looney way its endpoints are set up */ |
| 698 | if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) && |
| 699 | (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) || |
| 700 | ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) && |
| 701 | (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) { |
| 702 | if (interface != dev->actconfig->interface[0]) { |
| 703 | /* check out the endpoints of the other interface*/ |
| 704 | iface_desc = dev->actconfig->interface[0]->cur_altsetting; |
| 705 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 706 | endpoint = &iface_desc->endpoint[i].desc; |
| 707 | if ((endpoint->bEndpointAddress & 0x80) && |
| 708 | ((endpoint->bmAttributes & 3) == 0x03)) { |
| 709 | /* we found a interrupt in endpoint */ |
| 710 | dbg("found interrupt in for Prolific device on separate interface"); |
| 711 | interrupt_in_endpoint[num_interrupt_in] = endpoint; |
| 712 | ++num_interrupt_in; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /* Now make sure the PL-2303 is configured correctly. |
| 718 | * If not, give up now and hope this hack will work |
| 719 | * properly during a later invocation of usb_serial_probe |
| 720 | */ |
| 721 | if (num_bulk_in == 0 || num_bulk_out == 0) { |
| 722 | dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n"); |
| 723 | kfree (serial); |
| 724 | return -ENODEV; |
| 725 | } |
| 726 | } |
| 727 | /* END HORRIBLE HACK FOR PL2303 */ |
| 728 | #endif |
| 729 | |
| 730 | /* found all that we need */ |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 731 | dev_info(&interface->dev, "%s converter detected\n", type->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | #ifdef CONFIG_USB_SERIAL_GENERIC |
| 734 | if (type == &usb_serial_generic_device) { |
| 735 | num_ports = num_bulk_out; |
| 736 | if (num_ports == 0) { |
| 737 | dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n"); |
| 738 | kfree (serial); |
| 739 | return -EIO; |
| 740 | } |
| 741 | } |
| 742 | #endif |
| 743 | if (!num_ports) { |
| 744 | /* if this device type has a calc_num_ports function, call it */ |
| 745 | if (type->calc_num_ports) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 746 | if (!try_module_get(type->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | dev_err(&interface->dev, "module get failed, exiting\n"); |
| 748 | kfree (serial); |
| 749 | return -EIO; |
| 750 | } |
| 751 | num_ports = type->calc_num_ports (serial); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 752 | module_put(type->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | if (!num_ports) |
| 755 | num_ports = type->num_ports; |
| 756 | } |
| 757 | |
| 758 | if (get_free_serial (serial, num_ports, &minor) == NULL) { |
| 759 | dev_err(&interface->dev, "No more free serial devices\n"); |
| 760 | kfree (serial); |
| 761 | return -ENOMEM; |
| 762 | } |
| 763 | |
| 764 | serial->minor = minor; |
| 765 | serial->num_ports = num_ports; |
| 766 | serial->num_bulk_in = num_bulk_in; |
| 767 | serial->num_bulk_out = num_bulk_out; |
| 768 | serial->num_interrupt_in = num_interrupt_in; |
| 769 | serial->num_interrupt_out = num_interrupt_out; |
| 770 | |
| 771 | /* create our ports, we need as many as the max endpoints */ |
| 772 | /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */ |
| 773 | max_endpoints = max(num_bulk_in, num_bulk_out); |
| 774 | max_endpoints = max(max_endpoints, num_interrupt_in); |
| 775 | max_endpoints = max(max_endpoints, num_interrupt_out); |
| 776 | max_endpoints = max(max_endpoints, (int)serial->num_ports); |
| 777 | serial->num_port_pointers = max_endpoints; |
| 778 | dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints); |
| 779 | for (i = 0; i < max_endpoints; ++i) { |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 780 | port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (!port) |
| 782 | goto probe_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | port->number = i + serial->minor; |
| 784 | port->serial = serial; |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 785 | spin_lock_init(&port->lock); |
Luiz Fernando Capitulino | 8a4613f | 2005-11-28 19:16:07 -0200 | [diff] [blame] | 786 | sema_init(&port->sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | INIT_WORK(&port->work, usb_serial_port_softint, port); |
| 788 | serial->port[i] = port; |
| 789 | } |
| 790 | |
| 791 | /* set up the endpoint information */ |
| 792 | for (i = 0; i < num_bulk_in; ++i) { |
| 793 | endpoint = bulk_in_endpoint[i]; |
| 794 | port = serial->port[i]; |
| 795 | port->read_urb = usb_alloc_urb (0, GFP_KERNEL); |
| 796 | if (!port->read_urb) { |
| 797 | dev_err(&interface->dev, "No free urbs available\n"); |
| 798 | goto probe_error; |
| 799 | } |
| 800 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); |
| 801 | port->bulk_in_size = buffer_size; |
| 802 | port->bulk_in_endpointAddress = endpoint->bEndpointAddress; |
| 803 | port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL); |
| 804 | if (!port->bulk_in_buffer) { |
| 805 | dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n"); |
| 806 | goto probe_error; |
| 807 | } |
| 808 | usb_fill_bulk_urb (port->read_urb, dev, |
| 809 | usb_rcvbulkpipe (dev, |
| 810 | endpoint->bEndpointAddress), |
| 811 | port->bulk_in_buffer, buffer_size, |
| 812 | serial->type->read_bulk_callback, |
| 813 | port); |
| 814 | } |
| 815 | |
| 816 | for (i = 0; i < num_bulk_out; ++i) { |
| 817 | endpoint = bulk_out_endpoint[i]; |
| 818 | port = serial->port[i]; |
| 819 | port->write_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 820 | if (!port->write_urb) { |
| 821 | dev_err(&interface->dev, "No free urbs available\n"); |
| 822 | goto probe_error; |
| 823 | } |
| 824 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); |
| 825 | port->bulk_out_size = buffer_size; |
| 826 | port->bulk_out_endpointAddress = endpoint->bEndpointAddress; |
| 827 | port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL); |
| 828 | if (!port->bulk_out_buffer) { |
| 829 | dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n"); |
| 830 | goto probe_error; |
| 831 | } |
| 832 | usb_fill_bulk_urb (port->write_urb, dev, |
| 833 | usb_sndbulkpipe (dev, |
| 834 | endpoint->bEndpointAddress), |
| 835 | port->bulk_out_buffer, buffer_size, |
| 836 | serial->type->write_bulk_callback, |
| 837 | port); |
| 838 | } |
| 839 | |
| 840 | if (serial->type->read_int_callback) { |
| 841 | for (i = 0; i < num_interrupt_in; ++i) { |
| 842 | endpoint = interrupt_in_endpoint[i]; |
| 843 | port = serial->port[i]; |
| 844 | port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 845 | if (!port->interrupt_in_urb) { |
| 846 | dev_err(&interface->dev, "No free urbs available\n"); |
| 847 | goto probe_error; |
| 848 | } |
| 849 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); |
| 850 | port->interrupt_in_endpointAddress = endpoint->bEndpointAddress; |
| 851 | port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL); |
| 852 | if (!port->interrupt_in_buffer) { |
| 853 | dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); |
| 854 | goto probe_error; |
| 855 | } |
| 856 | usb_fill_int_urb (port->interrupt_in_urb, dev, |
| 857 | usb_rcvintpipe (dev, |
| 858 | endpoint->bEndpointAddress), |
| 859 | port->interrupt_in_buffer, buffer_size, |
| 860 | serial->type->read_int_callback, port, |
| 861 | endpoint->bInterval); |
| 862 | } |
| 863 | } else if (num_interrupt_in) { |
| 864 | dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined"); |
| 865 | } |
| 866 | |
| 867 | if (serial->type->write_int_callback) { |
| 868 | for (i = 0; i < num_interrupt_out; ++i) { |
| 869 | endpoint = interrupt_out_endpoint[i]; |
| 870 | port = serial->port[i]; |
| 871 | port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 872 | if (!port->interrupt_out_urb) { |
| 873 | dev_err(&interface->dev, "No free urbs available\n"); |
| 874 | goto probe_error; |
| 875 | } |
| 876 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); |
| 877 | port->interrupt_out_size = buffer_size; |
| 878 | port->interrupt_out_endpointAddress = endpoint->bEndpointAddress; |
| 879 | port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL); |
| 880 | if (!port->interrupt_out_buffer) { |
| 881 | dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); |
| 882 | goto probe_error; |
| 883 | } |
| 884 | usb_fill_int_urb (port->interrupt_out_urb, dev, |
| 885 | usb_sndintpipe (dev, |
| 886 | endpoint->bEndpointAddress), |
| 887 | port->interrupt_out_buffer, buffer_size, |
| 888 | serial->type->write_int_callback, port, |
| 889 | endpoint->bInterval); |
| 890 | } |
| 891 | } else if (num_interrupt_out) { |
| 892 | dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined"); |
| 893 | } |
| 894 | |
| 895 | /* if this device type has an attach function, call it */ |
| 896 | if (type->attach) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 897 | if (!try_module_get(type->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | dev_err(&interface->dev, "module get failed, exiting\n"); |
| 899 | goto probe_error; |
| 900 | } |
| 901 | retval = type->attach (serial); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 902 | module_put(type->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (retval < 0) |
| 904 | goto probe_error; |
| 905 | if (retval > 0) { |
| 906 | /* quietly accept this device, but don't bind to a serial port |
| 907 | * as it's about to disappear */ |
| 908 | goto exit; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | /* register all of the individual ports with the driver core */ |
| 913 | for (i = 0; i < num_ports; ++i) { |
| 914 | port = serial->port[i]; |
| 915 | port->dev.parent = &interface->dev; |
| 916 | port->dev.driver = NULL; |
| 917 | port->dev.bus = &usb_serial_bus_type; |
| 918 | port->dev.release = &port_release; |
| 919 | |
| 920 | snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number); |
| 921 | dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id); |
| 922 | device_register (&port->dev); |
| 923 | } |
| 924 | |
| 925 | usb_serial_console_init (debug, minor); |
| 926 | |
| 927 | exit: |
| 928 | /* success */ |
| 929 | usb_set_intfdata (interface, serial); |
| 930 | return 0; |
| 931 | |
| 932 | probe_error: |
| 933 | for (i = 0; i < num_bulk_in; ++i) { |
| 934 | port = serial->port[i]; |
| 935 | if (!port) |
| 936 | continue; |
| 937 | if (port->read_urb) |
| 938 | usb_free_urb (port->read_urb); |
| 939 | kfree(port->bulk_in_buffer); |
| 940 | } |
| 941 | for (i = 0; i < num_bulk_out; ++i) { |
| 942 | port = serial->port[i]; |
| 943 | if (!port) |
| 944 | continue; |
| 945 | if (port->write_urb) |
| 946 | usb_free_urb (port->write_urb); |
| 947 | kfree(port->bulk_out_buffer); |
| 948 | } |
| 949 | for (i = 0; i < num_interrupt_in; ++i) { |
| 950 | port = serial->port[i]; |
| 951 | if (!port) |
| 952 | continue; |
| 953 | if (port->interrupt_in_urb) |
| 954 | usb_free_urb (port->interrupt_in_urb); |
| 955 | kfree(port->interrupt_in_buffer); |
| 956 | } |
| 957 | for (i = 0; i < num_interrupt_out; ++i) { |
| 958 | port = serial->port[i]; |
| 959 | if (!port) |
| 960 | continue; |
| 961 | if (port->interrupt_out_urb) |
| 962 | usb_free_urb (port->interrupt_out_urb); |
| 963 | kfree(port->interrupt_out_buffer); |
| 964 | } |
| 965 | |
| 966 | /* return the minor range that this device had */ |
| 967 | return_serial (serial); |
| 968 | |
| 969 | /* free up any memory that we allocated */ |
| 970 | for (i = 0; i < serial->num_port_pointers; ++i) |
| 971 | kfree(serial->port[i]); |
| 972 | kfree (serial); |
| 973 | return -EIO; |
| 974 | } |
| 975 | |
| 976 | void usb_serial_disconnect(struct usb_interface *interface) |
| 977 | { |
| 978 | int i; |
| 979 | struct usb_serial *serial = usb_get_intfdata (interface); |
| 980 | struct device *dev = &interface->dev; |
| 981 | struct usb_serial_port *port; |
| 982 | |
| 983 | dbg ("%s", __FUNCTION__); |
| 984 | |
| 985 | usb_set_intfdata (interface, NULL); |
| 986 | if (serial) { |
| 987 | for (i = 0; i < serial->num_ports; ++i) { |
| 988 | port = serial->port[i]; |
| 989 | if (port && port->tty) |
| 990 | tty_hangup(port->tty); |
| 991 | } |
| 992 | /* let the last holder of this object |
| 993 | * cause it to be cleaned up */ |
| 994 | kref_put(&serial->kref, destroy_serial); |
| 995 | } |
| 996 | dev_info(dev, "device disconnected\n"); |
| 997 | } |
| 998 | |
| 999 | static struct tty_operations serial_ops = { |
| 1000 | .open = serial_open, |
| 1001 | .close = serial_close, |
| 1002 | .write = serial_write, |
| 1003 | .write_room = serial_write_room, |
| 1004 | .ioctl = serial_ioctl, |
| 1005 | .set_termios = serial_set_termios, |
| 1006 | .throttle = serial_throttle, |
| 1007 | .unthrottle = serial_unthrottle, |
| 1008 | .break_ctl = serial_break, |
| 1009 | .chars_in_buffer = serial_chars_in_buffer, |
| 1010 | .read_proc = serial_read_proc, |
| 1011 | .tiocmget = serial_tiocmget, |
| 1012 | .tiocmset = serial_tiocmset, |
| 1013 | }; |
| 1014 | |
| 1015 | struct tty_driver *usb_serial_tty_driver; |
| 1016 | |
| 1017 | static int __init usb_serial_init(void) |
| 1018 | { |
| 1019 | int i; |
| 1020 | int result; |
| 1021 | |
| 1022 | usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS); |
| 1023 | if (!usb_serial_tty_driver) |
| 1024 | return -ENOMEM; |
| 1025 | |
| 1026 | /* Initialize our global data */ |
| 1027 | for (i = 0; i < SERIAL_TTY_MINORS; ++i) { |
| 1028 | serial_table[i] = NULL; |
| 1029 | } |
| 1030 | |
| 1031 | result = bus_register(&usb_serial_bus_type); |
| 1032 | if (result) { |
| 1033 | err("%s - registering bus driver failed", __FUNCTION__); |
| 1034 | goto exit_bus; |
| 1035 | } |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | usb_serial_tty_driver->owner = THIS_MODULE; |
| 1038 | usb_serial_tty_driver->driver_name = "usbserial"; |
| 1039 | usb_serial_tty_driver->devfs_name = "usb/tts/"; |
| 1040 | usb_serial_tty_driver->name = "ttyUSB"; |
| 1041 | usb_serial_tty_driver->major = SERIAL_TTY_MAJOR; |
| 1042 | usb_serial_tty_driver->minor_start = 0; |
| 1043 | usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 1044 | usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 1045 | usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS; |
| 1046 | usb_serial_tty_driver->init_termios = tty_std_termios; |
| 1047 | usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 1048 | tty_set_operations(usb_serial_tty_driver, &serial_ops); |
| 1049 | result = tty_register_driver(usb_serial_tty_driver); |
| 1050 | if (result) { |
| 1051 | err("%s - tty_register_driver failed", __FUNCTION__); |
| 1052 | goto exit_reg_driver; |
| 1053 | } |
| 1054 | |
| 1055 | /* register the USB driver */ |
| 1056 | result = usb_register(&usb_serial_driver); |
| 1057 | if (result < 0) { |
| 1058 | err("%s - usb_register failed", __FUNCTION__); |
| 1059 | goto exit_tty; |
| 1060 | } |
| 1061 | |
Greg Kroah-Hartman | 06299db | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 1062 | /* register the generic driver, if we should */ |
| 1063 | result = usb_serial_generic_register(debug); |
| 1064 | if (result < 0) { |
| 1065 | err("%s - registering generic driver failed", __FUNCTION__); |
| 1066 | goto exit_generic; |
| 1067 | } |
| 1068 | |
Greg Kroah-Hartman | 17a882f | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1069 | info(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | return result; |
| 1072 | |
Greg Kroah-Hartman | 06299db | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 1073 | exit_generic: |
| 1074 | usb_deregister(&usb_serial_driver); |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | exit_tty: |
| 1077 | tty_unregister_driver(usb_serial_tty_driver); |
| 1078 | |
| 1079 | exit_reg_driver: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | bus_unregister(&usb_serial_bus_type); |
| 1081 | |
| 1082 | exit_bus: |
| 1083 | err ("%s - returning with error %d", __FUNCTION__, result); |
| 1084 | put_tty_driver(usb_serial_tty_driver); |
| 1085 | return result; |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | static void __exit usb_serial_exit(void) |
| 1090 | { |
| 1091 | usb_serial_console_exit(); |
| 1092 | |
| 1093 | usb_serial_generic_deregister(); |
| 1094 | |
| 1095 | usb_deregister(&usb_serial_driver); |
| 1096 | tty_unregister_driver(usb_serial_tty_driver); |
| 1097 | put_tty_driver(usb_serial_tty_driver); |
| 1098 | bus_unregister(&usb_serial_bus_type); |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | module_init(usb_serial_init); |
| 1103 | module_exit(usb_serial_exit); |
| 1104 | |
| 1105 | #define set_to_generic_if_null(type, function) \ |
| 1106 | do { \ |
| 1107 | if (!type->function) { \ |
| 1108 | type->function = usb_serial_generic_##function; \ |
| 1109 | dbg("Had to override the " #function \ |
| 1110 | " usb serial operation with the generic one.");\ |
| 1111 | } \ |
| 1112 | } while (0) |
| 1113 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1114 | static void fixup_generic(struct usb_serial_driver *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| 1116 | set_to_generic_if_null(device, open); |
| 1117 | set_to_generic_if_null(device, write); |
| 1118 | set_to_generic_if_null(device, close); |
| 1119 | set_to_generic_if_null(device, write_room); |
| 1120 | set_to_generic_if_null(device, chars_in_buffer); |
| 1121 | set_to_generic_if_null(device, read_bulk_callback); |
| 1122 | set_to_generic_if_null(device, write_bulk_callback); |
| 1123 | set_to_generic_if_null(device, shutdown); |
| 1124 | } |
| 1125 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1126 | int usb_serial_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
| 1128 | int retval; |
| 1129 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1130 | fixup_generic(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1132 | if (!driver->description) |
| 1133 | driver->description = driver->driver.name; |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | /* Add this device to our list of devices */ |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1136 | list_add(&driver->driver_list, &usb_serial_driver_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1138 | retval = usb_serial_bus_register(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | if (retval) { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1140 | err("problem %d when registering driver %s", retval, driver->description); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1141 | list_del(&driver->driver_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
| 1143 | else |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1144 | info("USB Serial support registered for %s", driver->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
| 1146 | return retval; |
| 1147 | } |
| 1148 | |
| 1149 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1150 | void usb_serial_deregister(struct usb_serial_driver *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1152 | info("USB Serial deregistering driver %s", device->description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | list_del(&device->driver_list); |
| 1154 | usb_serial_bus_deregister(device); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | /* If the usb-serial core is built into the core, the usb-serial drivers |
| 1160 | need these symbols to load properly as modules. */ |
| 1161 | EXPORT_SYMBOL_GPL(usb_serial_register); |
| 1162 | EXPORT_SYMBOL_GPL(usb_serial_deregister); |
| 1163 | EXPORT_SYMBOL_GPL(usb_serial_probe); |
| 1164 | EXPORT_SYMBOL_GPL(usb_serial_disconnect); |
| 1165 | EXPORT_SYMBOL_GPL(usb_serial_port_softint); |
| 1166 | |
| 1167 | |
| 1168 | /* Module information */ |
| 1169 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 1170 | MODULE_DESCRIPTION( DRIVER_DESC ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | MODULE_LICENSE("GPL"); |
| 1172 | |
| 1173 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1174 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |