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