Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Prolific PL2303 USB to serial adaptor driver |
| 3 | * |
| 4 | * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) |
| 5 | * Copyright (C) 2003 IBM Corp. |
| 6 | * |
| 7 | * Original driver for 2.2.x by anonymous |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
Greg Kroah-Hartman | 502b95c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 11 | * the Free Software Foundation; either version 2 of the License. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 13 | * See Documentation/usb/usb-serial.txt for more information on using this driver |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/tty.h> |
| 22 | #include <linux/tty_driver.h> |
| 23 | #include <linux/tty_flip.h> |
| 24 | #include <linux/serial.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 30 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "pl2303.h" |
| 32 | |
| 33 | /* |
| 34 | * Version Information |
| 35 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver" |
| 37 | |
| 38 | static int debug; |
| 39 | |
| 40 | #define PL2303_CLOSING_WAIT (30*HZ) |
| 41 | |
| 42 | #define PL2303_BUF_SIZE 1024 |
| 43 | #define PL2303_TMP_BUF_SIZE 1024 |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct pl2303_buf { |
| 46 | unsigned int buf_size; |
| 47 | char *buf_buf; |
| 48 | char *buf_get; |
| 49 | char *buf_put; |
| 50 | }; |
| 51 | |
| 52 | static struct usb_device_id id_table [] = { |
| 53 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) }, |
| 54 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) }, |
Peter Moulder | 3d86149 | 2006-06-19 22:47:49 +1000 | [diff] [blame] | 55 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) }, |
| 57 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) }, |
| 58 | { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) }, |
| 59 | { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) }, |
| 60 | { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) }, |
| 61 | { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) }, |
| 62 | { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) }, |
| 63 | { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) }, |
Wang Jun | 5838171 | 2006-04-19 16:32:07 +0800 | [diff] [blame] | 64 | { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) }, |
| 66 | { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) }, |
| 67 | { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) }, |
| 68 | { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) }, |
| 69 | { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) }, |
| 70 | { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) }, |
| 71 | { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) }, |
| 72 | { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) }, |
Luiz Fernando Capitulino | a8310f3 | 2005-11-17 09:47:32 -0800 | [diff] [blame] | 73 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) }, |
Luiz Fernando Capitulino | a8310f3 | 2005-11-17 09:47:32 -0800 | [diff] [blame] | 75 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) }, |
Peter Favrholdt | acbb36f | 2005-04-18 17:39:32 -0700 | [diff] [blame] | 76 | { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) }, |
Christian Lindner | c6c2772 | 2006-02-01 14:10:52 +0100 | [diff] [blame] | 77 | { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) }, |
| 78 | { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) }, |
Denis MONTERRAT | 6cceb05 | 2006-01-19 14:52:38 +0100 | [diff] [blame] | 79 | { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) }, |
Christian Lindner | c6c2772 | 2006-02-01 14:10:52 +0100 | [diff] [blame] | 80 | { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) }, |
Dick Streefland | 491b04c | 2006-03-01 00:53:33 -0800 | [diff] [blame] | 81 | { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) }, |
Matthew Meno | 3b92847 | 2006-06-21 15:25:53 -0400 | [diff] [blame] | 82 | { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) }, |
Kim Oldfield | b7aa94b | 2006-07-25 15:54:59 +1000 | [diff] [blame] | 83 | { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { } /* Terminating entry */ |
| 85 | }; |
| 86 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 87 | MODULE_DEVICE_TABLE(usb, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | static struct usb_driver pl2303_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | .name = "pl2303", |
| 91 | .probe = usb_serial_probe, |
| 92 | .disconnect = usb_serial_disconnect, |
| 93 | .id_table = id_table, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 94 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #define SET_LINE_REQUEST_TYPE 0x21 |
| 98 | #define SET_LINE_REQUEST 0x20 |
| 99 | |
| 100 | #define SET_CONTROL_REQUEST_TYPE 0x21 |
| 101 | #define SET_CONTROL_REQUEST 0x22 |
| 102 | #define CONTROL_DTR 0x01 |
| 103 | #define CONTROL_RTS 0x02 |
| 104 | |
| 105 | #define BREAK_REQUEST_TYPE 0x21 |
| 106 | #define BREAK_REQUEST 0x23 |
| 107 | #define BREAK_ON 0xffff |
| 108 | #define BREAK_OFF 0x0000 |
| 109 | |
| 110 | #define GET_LINE_REQUEST_TYPE 0xa1 |
| 111 | #define GET_LINE_REQUEST 0x21 |
| 112 | |
| 113 | #define VENDOR_WRITE_REQUEST_TYPE 0x40 |
| 114 | #define VENDOR_WRITE_REQUEST 0x01 |
| 115 | |
| 116 | #define VENDOR_READ_REQUEST_TYPE 0xc0 |
| 117 | #define VENDOR_READ_REQUEST 0x01 |
| 118 | |
| 119 | #define UART_STATE 0x08 |
| 120 | #define UART_STATE_TRANSIENT_MASK 0x74 |
| 121 | #define UART_DCD 0x01 |
| 122 | #define UART_DSR 0x02 |
| 123 | #define UART_BREAK_ERROR 0x04 |
| 124 | #define UART_RING 0x08 |
| 125 | #define UART_FRAME_ERROR 0x10 |
| 126 | #define UART_PARITY_ERROR 0x20 |
| 127 | #define UART_OVERRUN_ERROR 0x40 |
| 128 | #define UART_CTS 0x80 |
| 129 | |
| 130 | /* function prototypes for a PL2303 serial converter */ |
| 131 | static int pl2303_open (struct usb_serial_port *port, struct file *filp); |
| 132 | static void pl2303_close (struct usb_serial_port *port, struct file *filp); |
| 133 | static void pl2303_set_termios (struct usb_serial_port *port, |
| 134 | struct termios *old); |
| 135 | static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, |
| 136 | unsigned int cmd, unsigned long arg); |
| 137 | static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs); |
| 138 | static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs); |
| 139 | static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs); |
| 140 | static int pl2303_write (struct usb_serial_port *port, |
| 141 | const unsigned char *buf, int count); |
| 142 | static void pl2303_send (struct usb_serial_port *port); |
| 143 | static int pl2303_write_room(struct usb_serial_port *port); |
| 144 | static int pl2303_chars_in_buffer(struct usb_serial_port *port); |
| 145 | static void pl2303_break_ctl(struct usb_serial_port *port,int break_state); |
| 146 | static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file); |
| 147 | static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file, |
| 148 | unsigned int set, unsigned int clear); |
| 149 | static int pl2303_startup (struct usb_serial *serial); |
| 150 | static void pl2303_shutdown (struct usb_serial *serial); |
| 151 | static struct pl2303_buf *pl2303_buf_alloc(unsigned int size); |
| 152 | static void pl2303_buf_free(struct pl2303_buf *pb); |
| 153 | static void pl2303_buf_clear(struct pl2303_buf *pb); |
| 154 | static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb); |
| 155 | static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb); |
| 156 | static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf, |
| 157 | unsigned int count); |
| 158 | static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf, |
| 159 | unsigned int count); |
| 160 | |
| 161 | |
| 162 | /* All of the device info needed for the PL2303 SIO serial converter */ |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 163 | static struct usb_serial_driver pl2303_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 164 | .driver = { |
| 165 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 166 | .name = "pl2303", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 167 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | .id_table = id_table, |
| 169 | .num_interrupt_in = NUM_DONT_CARE, |
| 170 | .num_bulk_in = 1, |
| 171 | .num_bulk_out = 1, |
| 172 | .num_ports = 1, |
| 173 | .open = pl2303_open, |
| 174 | .close = pl2303_close, |
| 175 | .write = pl2303_write, |
| 176 | .ioctl = pl2303_ioctl, |
| 177 | .break_ctl = pl2303_break_ctl, |
| 178 | .set_termios = pl2303_set_termios, |
| 179 | .tiocmget = pl2303_tiocmget, |
| 180 | .tiocmset = pl2303_tiocmset, |
| 181 | .read_bulk_callback = pl2303_read_bulk_callback, |
| 182 | .read_int_callback = pl2303_read_int_callback, |
| 183 | .write_bulk_callback = pl2303_write_bulk_callback, |
| 184 | .write_room = pl2303_write_room, |
| 185 | .chars_in_buffer = pl2303_chars_in_buffer, |
| 186 | .attach = pl2303_startup, |
| 187 | .shutdown = pl2303_shutdown, |
| 188 | }; |
| 189 | |
| 190 | enum pl2303_type { |
| 191 | type_0, /* don't know the difference between type 0 and */ |
| 192 | type_1, /* type 1, until someone from prolific tells us... */ |
| 193 | HX, /* HX version of the pl2303 chip */ |
| 194 | }; |
| 195 | |
| 196 | struct pl2303_private { |
| 197 | spinlock_t lock; |
| 198 | struct pl2303_buf *buf; |
| 199 | int write_urb_in_use; |
| 200 | wait_queue_head_t delta_msr_wait; |
| 201 | u8 line_control; |
| 202 | u8 line_status; |
| 203 | u8 termios_initialized; |
| 204 | enum pl2303_type type; |
| 205 | }; |
| 206 | |
| 207 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 208 | static int pl2303_startup(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct pl2303_private *priv; |
| 211 | enum pl2303_type type = type_0; |
| 212 | int i; |
| 213 | |
| 214 | if (serial->dev->descriptor.bDeviceClass == 0x02) |
| 215 | type = type_0; |
| 216 | else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40) |
| 217 | type = HX; |
| 218 | else if (serial->dev->descriptor.bDeviceClass == 0x00) |
| 219 | type = type_1; |
| 220 | else if (serial->dev->descriptor.bDeviceClass == 0xFF) |
| 221 | type = type_1; |
| 222 | dbg("device type: %d", type); |
| 223 | |
| 224 | for (i = 0; i < serial->num_ports; ++i) { |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 225 | priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (!priv) |
| 227 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | spin_lock_init(&priv->lock); |
| 229 | priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE); |
| 230 | if (priv->buf == NULL) { |
| 231 | kfree(priv); |
| 232 | goto cleanup; |
| 233 | } |
| 234 | init_waitqueue_head(&priv->delta_msr_wait); |
| 235 | priv->type = type; |
| 236 | usb_set_serial_port_data(serial->port[i], priv); |
| 237 | } |
| 238 | return 0; |
| 239 | |
| 240 | cleanup: |
| 241 | for (--i; i>=0; --i) { |
| 242 | priv = usb_get_serial_port_data(serial->port[i]); |
| 243 | pl2303_buf_free(priv->buf); |
| 244 | kfree(priv); |
| 245 | usb_set_serial_port_data(serial->port[i], NULL); |
| 246 | } |
| 247 | return -ENOMEM; |
| 248 | } |
| 249 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 250 | static int set_control_lines(struct usb_device *dev, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | int retval; |
| 253 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 254 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 255 | SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE, |
| 256 | value, 0, NULL, 0, 100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval); |
| 258 | return retval; |
| 259 | } |
| 260 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 261 | static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf, |
| 262 | int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
| 264 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 265 | unsigned long flags; |
| 266 | |
| 267 | dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count); |
| 268 | |
| 269 | if (!count) |
| 270 | return count; |
| 271 | |
| 272 | spin_lock_irqsave(&priv->lock, flags); |
| 273 | count = pl2303_buf_put(priv->buf, buf, count); |
| 274 | spin_unlock_irqrestore(&priv->lock, flags); |
| 275 | |
| 276 | pl2303_send(port); |
| 277 | |
| 278 | return count; |
| 279 | } |
| 280 | |
| 281 | static void pl2303_send(struct usb_serial_port *port) |
| 282 | { |
| 283 | int count, result; |
| 284 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 285 | unsigned long flags; |
| 286 | |
| 287 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 288 | |
| 289 | spin_lock_irqsave(&priv->lock, flags); |
| 290 | |
| 291 | if (priv->write_urb_in_use) { |
| 292 | spin_unlock_irqrestore(&priv->lock, flags); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer, |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 297 | port->bulk_out_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | if (count == 0) { |
| 300 | spin_unlock_irqrestore(&priv->lock, flags); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | priv->write_urb_in_use = 1; |
| 305 | |
| 306 | spin_unlock_irqrestore(&priv->lock, flags); |
| 307 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 308 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, |
| 309 | port->write_urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | port->write_urb->transfer_buffer_length = count; |
| 312 | port->write_urb->dev = port->serial->dev; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 313 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (result) { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 315 | dev_err(&port->dev, "%s - failed submitting write urb," |
| 316 | " error %d\n", __FUNCTION__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | priv->write_urb_in_use = 0; |
| 318 | // TODO: reschedule pl2303_send |
| 319 | } |
| 320 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 321 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static int pl2303_write_room(struct usb_serial_port *port) |
| 325 | { |
| 326 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 327 | int room = 0; |
| 328 | unsigned long flags; |
| 329 | |
| 330 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 331 | |
| 332 | spin_lock_irqsave(&priv->lock, flags); |
| 333 | room = pl2303_buf_space_avail(priv->buf); |
| 334 | spin_unlock_irqrestore(&priv->lock, flags); |
| 335 | |
| 336 | dbg("%s - returns %d", __FUNCTION__, room); |
| 337 | return room; |
| 338 | } |
| 339 | |
| 340 | static int pl2303_chars_in_buffer(struct usb_serial_port *port) |
| 341 | { |
| 342 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 343 | int chars = 0; |
| 344 | unsigned long flags; |
| 345 | |
| 346 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 347 | |
| 348 | spin_lock_irqsave(&priv->lock, flags); |
| 349 | chars = pl2303_buf_data_avail(priv->buf); |
| 350 | spin_unlock_irqrestore(&priv->lock, flags); |
| 351 | |
| 352 | dbg("%s - returns %d", __FUNCTION__, chars); |
| 353 | return chars; |
| 354 | } |
| 355 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 356 | static void pl2303_set_termios(struct usb_serial_port *port, |
| 357 | struct termios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | struct usb_serial *serial = port->serial; |
| 360 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 361 | unsigned long flags; |
| 362 | unsigned int cflag; |
| 363 | unsigned char *buf; |
| 364 | int baud; |
| 365 | int i; |
| 366 | u8 control; |
| 367 | |
| 368 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 369 | |
| 370 | if ((!port->tty) || (!port->tty->termios)) { |
| 371 | dbg("%s - no tty structures", __FUNCTION__); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | spin_lock_irqsave(&priv->lock, flags); |
| 376 | if (!priv->termios_initialized) { |
| 377 | *(port->tty->termios) = tty_std_termios; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 378 | port->tty->termios->c_cflag = B9600 | CS8 | CREAD | |
| 379 | HUPCL | CLOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | priv->termios_initialized = 1; |
| 381 | } |
| 382 | spin_unlock_irqrestore(&priv->lock, flags); |
| 383 | |
| 384 | cflag = port->tty->termios->c_cflag; |
| 385 | /* check that they really want us to change something */ |
| 386 | if (old_termios) { |
| 387 | if ((cflag == old_termios->c_cflag) && |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 388 | (RELEVANT_IFLAG(port->tty->termios->c_iflag) == |
| 389 | RELEVANT_IFLAG(old_termios->c_iflag))) { |
| 390 | dbg("%s - nothing to change...", __FUNCTION__); |
| 391 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 395 | buf = kzalloc(7, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (!buf) { |
| 397 | dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); |
| 398 | return; |
| 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 401 | i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 402 | GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE, |
| 403 | 0, 0, buf, 7, 100); |
| 404 | dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i, |
| 405 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | if (cflag & CSIZE) { |
| 408 | switch (cflag & CSIZE) { |
| 409 | case CS5: buf[6] = 5; break; |
| 410 | case CS6: buf[6] = 6; break; |
| 411 | case CS7: buf[6] = 7; break; |
| 412 | default: |
| 413 | case CS8: buf[6] = 8; break; |
| 414 | } |
| 415 | dbg("%s - data bits = %d", __FUNCTION__, buf[6]); |
| 416 | } |
| 417 | |
| 418 | baud = 0; |
| 419 | switch (cflag & CBAUD) { |
| 420 | case B0: baud = 0; break; |
| 421 | case B75: baud = 75; break; |
| 422 | case B150: baud = 150; break; |
| 423 | case B300: baud = 300; break; |
| 424 | case B600: baud = 600; break; |
| 425 | case B1200: baud = 1200; break; |
| 426 | case B1800: baud = 1800; break; |
| 427 | case B2400: baud = 2400; break; |
| 428 | case B4800: baud = 4800; break; |
| 429 | case B9600: baud = 9600; break; |
| 430 | case B19200: baud = 19200; break; |
| 431 | case B38400: baud = 38400; break; |
| 432 | case B57600: baud = 57600; break; |
| 433 | case B115200: baud = 115200; break; |
| 434 | case B230400: baud = 230400; break; |
| 435 | case B460800: baud = 460800; break; |
| 436 | default: |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 437 | dev_err(&port->dev, "pl2303 driver does not support" |
| 438 | " the baudrate requested (fix it)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | dbg("%s - baud = %d", __FUNCTION__, baud); |
| 442 | if (baud) { |
| 443 | buf[0] = baud & 0xff; |
| 444 | buf[1] = (baud >> 8) & 0xff; |
| 445 | buf[2] = (baud >> 16) & 0xff; |
| 446 | buf[3] = (baud >> 24) & 0xff; |
| 447 | } |
| 448 | |
| 449 | /* For reference buf[4]=0 is 1 stop bits */ |
| 450 | /* For reference buf[4]=1 is 1.5 stop bits */ |
| 451 | /* For reference buf[4]=2 is 2 stop bits */ |
| 452 | if (cflag & CSTOPB) { |
| 453 | buf[4] = 2; |
| 454 | dbg("%s - stop bits = 2", __FUNCTION__); |
| 455 | } else { |
| 456 | buf[4] = 0; |
| 457 | dbg("%s - stop bits = 1", __FUNCTION__); |
| 458 | } |
| 459 | |
| 460 | if (cflag & PARENB) { |
| 461 | /* For reference buf[5]=0 is none parity */ |
| 462 | /* For reference buf[5]=1 is odd parity */ |
| 463 | /* For reference buf[5]=2 is even parity */ |
| 464 | /* For reference buf[5]=3 is mark parity */ |
| 465 | /* For reference buf[5]=4 is space parity */ |
| 466 | if (cflag & PARODD) { |
| 467 | buf[5] = 1; |
| 468 | dbg("%s - parity = odd", __FUNCTION__); |
| 469 | } else { |
| 470 | buf[5] = 2; |
| 471 | dbg("%s - parity = even", __FUNCTION__); |
| 472 | } |
| 473 | } else { |
| 474 | buf[5] = 0; |
| 475 | dbg("%s - parity = none", __FUNCTION__); |
| 476 | } |
| 477 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 478 | i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 479 | SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE, |
| 480 | 0, 0, buf, 7, 100); |
| 481 | dbg("0x21:0x20:0:0 %d", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | /* change control lines if we are switching to or from B0 */ |
| 484 | spin_lock_irqsave(&priv->lock, flags); |
| 485 | control = priv->line_control; |
| 486 | if ((cflag & CBAUD) == B0) |
| 487 | priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); |
| 488 | else |
| 489 | priv->line_control |= (CONTROL_DTR | CONTROL_RTS); |
| 490 | if (control != priv->line_control) { |
| 491 | control = priv->line_control; |
| 492 | spin_unlock_irqrestore(&priv->lock, flags); |
| 493 | set_control_lines(serial->dev, control); |
| 494 | } else { |
| 495 | spin_unlock_irqrestore(&priv->lock, flags); |
| 496 | } |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0; |
| 499 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 500 | i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 501 | GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE, |
| 502 | 0, 0, buf, 7, 100); |
| 503 | dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); |
| 505 | |
| 506 | if (cflag & CRTSCTS) { |
| 507 | __u16 index; |
| 508 | if (priv->type == HX) |
| 509 | index = 0x61; |
| 510 | else |
| 511 | index = 0x41; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 512 | i = usb_control_msg(serial->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | usb_sndctrlpipe(serial->dev, 0), |
| 514 | VENDOR_WRITE_REQUEST, |
| 515 | VENDOR_WRITE_REQUEST_TYPE, |
| 516 | 0x0, index, NULL, 0, 100); |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 517 | dbg("0x40:0x1:0x0:0x%x %d", index, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 520 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 523 | static int pl2303_open(struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | struct termios tmp_termios; |
| 526 | struct usb_serial *serial = port->serial; |
| 527 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 528 | unsigned char *buf; |
| 529 | int result; |
| 530 | |
| 531 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 532 | |
Dariusz M | 1694899 | 2005-07-28 18:06:13 +0200 | [diff] [blame] | 533 | if (priv->type != HX) { |
| 534 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 535 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | buf = kmalloc(10, GFP_KERNEL); |
| 539 | if (buf==NULL) |
| 540 | return -ENOMEM; |
| 541 | |
| 542 | #define FISH(a,b,c,d) \ |
| 543 | result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \ |
| 544 | b, a, c, d, buf, 1, 100); \ |
| 545 | dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]); |
| 546 | |
| 547 | #define SOUP(a,b,c,d) \ |
| 548 | result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \ |
| 549 | b, a, c, d, NULL, 0, 100); \ |
| 550 | dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result); |
| 551 | |
| 552 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0); |
| 553 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0); |
| 554 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0); |
| 555 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0); |
| 556 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0); |
| 557 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1); |
| 558 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0); |
| 559 | FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0); |
| 560 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1); |
| 561 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0); |
| 562 | |
| 563 | if (priv->type == HX) { |
| 564 | /* HX chip */ |
| 565 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44); |
| 566 | /* reset upstream data pipes */ |
| 567 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0); |
| 568 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0); |
| 569 | } else { |
| 570 | SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24); |
| 571 | } |
| 572 | |
| 573 | kfree(buf); |
| 574 | |
| 575 | /* Setup termios */ |
| 576 | if (port->tty) { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 577 | pl2303_set_termios(port, &tmp_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | //FIXME: need to assert RTS and DTR if CRTSCTS off |
| 581 | |
| 582 | dbg("%s - submitting read urb", __FUNCTION__); |
| 583 | port->read_urb->dev = serial->dev; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 584 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (result) { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 586 | dev_err(&port->dev, "%s - failed submitting read urb," |
| 587 | " error %d\n", __FUNCTION__, result); |
| 588 | pl2303_close(port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return -EPROTO; |
| 590 | } |
| 591 | |
| 592 | dbg("%s - submitting interrupt urb", __FUNCTION__); |
| 593 | port->interrupt_in_urb->dev = serial->dev; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 594 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (result) { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 596 | dev_err(&port->dev, "%s - failed submitting interrupt urb," |
| 597 | " error %d\n", __FUNCTION__, result); |
| 598 | pl2303_close(port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | return -EPROTO; |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 604 | static void pl2303_close(struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
| 606 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 607 | unsigned long flags; |
| 608 | unsigned int c_cflag; |
| 609 | int bps; |
| 610 | long timeout; |
Pete Zaitcev | 80b4785 | 2006-05-22 22:05:17 -0700 | [diff] [blame] | 611 | wait_queue_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 614 | |
| 615 | /* wait for data to drain from the buffer */ |
| 616 | spin_lock_irqsave(&priv->lock, flags); |
| 617 | timeout = PL2303_CLOSING_WAIT; |
| 618 | init_waitqueue_entry(&wait, current); |
| 619 | add_wait_queue(&port->tty->write_wait, &wait); |
| 620 | for (;;) { |
| 621 | set_current_state(TASK_INTERRUPTIBLE); |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 622 | if (pl2303_buf_data_avail(priv->buf) == 0 || |
| 623 | timeout == 0 || signal_pending(current) || |
| 624 | !usb_get_intfdata(port->serial->interface)) /* disconnect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | break; |
| 626 | spin_unlock_irqrestore(&priv->lock, flags); |
| 627 | timeout = schedule_timeout(timeout); |
| 628 | spin_lock_irqsave(&priv->lock, flags); |
| 629 | } |
| 630 | set_current_state(TASK_RUNNING); |
| 631 | remove_wait_queue(&port->tty->write_wait, &wait); |
| 632 | /* clear out any remaining data in the buffer */ |
| 633 | pl2303_buf_clear(priv->buf); |
| 634 | spin_unlock_irqrestore(&priv->lock, flags); |
| 635 | |
| 636 | /* wait for characters to drain from the device */ |
| 637 | /* (this is long enough for the entire 256 byte */ |
| 638 | /* pl2303 hardware buffer to drain with no flow */ |
| 639 | /* control for data rates of 1200 bps or more, */ |
| 640 | /* for lower rates we should really know how much */ |
| 641 | /* data is in the buffer to compute a delay */ |
| 642 | /* that is not unnecessarily long) */ |
| 643 | bps = tty_get_baud_rate(port->tty); |
| 644 | if (bps > 1200) |
| 645 | timeout = max((HZ*2560)/bps,HZ/10); |
| 646 | else |
| 647 | timeout = 2*HZ; |
Nishanth Aravamudan | 22c4386 | 2005-08-15 11:30:11 -0700 | [diff] [blame] | 648 | schedule_timeout_interruptible(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | /* shutdown our urbs */ |
| 651 | dbg("%s - shutting down urbs", __FUNCTION__); |
| 652 | usb_kill_urb(port->write_urb); |
| 653 | usb_kill_urb(port->read_urb); |
| 654 | usb_kill_urb(port->interrupt_in_urb); |
| 655 | |
| 656 | if (port->tty) { |
| 657 | c_cflag = port->tty->termios->c_cflag; |
| 658 | if (c_cflag & HUPCL) { |
| 659 | /* drop DTR and RTS */ |
| 660 | spin_lock_irqsave(&priv->lock, flags); |
| 661 | priv->line_control = 0; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 662 | spin_unlock_irqrestore(&priv->lock, flags); |
| 663 | set_control_lines(port->serial->dev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 668 | static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file, |
| 669 | unsigned int set, unsigned int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
| 671 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 672 | unsigned long flags; |
| 673 | u8 control; |
| 674 | |
Flavio Leitner | 6fdd8e8 | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 675 | if (!usb_get_intfdata(port->serial->interface)) |
| 676 | return -ENODEV; |
| 677 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 678 | spin_lock_irqsave(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (set & TIOCM_RTS) |
| 680 | priv->line_control |= CONTROL_RTS; |
| 681 | if (set & TIOCM_DTR) |
| 682 | priv->line_control |= CONTROL_DTR; |
| 683 | if (clear & TIOCM_RTS) |
| 684 | priv->line_control &= ~CONTROL_RTS; |
| 685 | if (clear & TIOCM_DTR) |
| 686 | priv->line_control &= ~CONTROL_DTR; |
| 687 | control = priv->line_control; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 688 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 690 | return set_control_lines(port->serial->dev, control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 693 | static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 696 | unsigned long flags; |
| 697 | unsigned int mcr; |
| 698 | unsigned int status; |
| 699 | unsigned int result; |
| 700 | |
| 701 | dbg("%s (%d)", __FUNCTION__, port->number); |
| 702 | |
Flavio Leitner | 6fdd8e8 | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 703 | if (!usb_get_intfdata(port->serial->interface)) |
| 704 | return -ENODEV; |
| 705 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 706 | spin_lock_irqsave(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | mcr = priv->line_control; |
| 708 | status = priv->line_status; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 709 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0) |
| 712 | | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0) |
| 713 | | ((status & UART_CTS) ? TIOCM_CTS : 0) |
| 714 | | ((status & UART_DSR) ? TIOCM_DSR : 0) |
| 715 | | ((status & UART_RING) ? TIOCM_RI : 0) |
| 716 | | ((status & UART_DCD) ? TIOCM_CD : 0); |
| 717 | |
| 718 | dbg("%s - result = %x", __FUNCTION__, result); |
| 719 | |
| 720 | return result; |
| 721 | } |
| 722 | |
| 723 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) |
| 724 | { |
| 725 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 726 | unsigned long flags; |
| 727 | unsigned int prevstatus; |
| 728 | unsigned int status; |
| 729 | unsigned int changed; |
| 730 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 731 | spin_lock_irqsave(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | prevstatus = priv->line_status; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 733 | spin_unlock_irqrestore(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | while (1) { |
| 736 | interruptible_sleep_on(&priv->delta_msr_wait); |
| 737 | /* see if a signal did it */ |
| 738 | if (signal_pending(current)) |
| 739 | return -ERESTARTSYS; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 740 | |
| 741 | spin_lock_irqsave(&priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | status = priv->line_status; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 743 | spin_unlock_irqrestore(&priv->lock, flags); |
| 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | changed=prevstatus^status; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | if (((arg & TIOCM_RNG) && (changed & UART_RING)) || |
| 748 | ((arg & TIOCM_DSR) && (changed & UART_DSR)) || |
| 749 | ((arg & TIOCM_CD) && (changed & UART_DCD)) || |
| 750 | ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) { |
| 751 | return 0; |
| 752 | } |
| 753 | prevstatus = status; |
| 754 | } |
| 755 | /* NOTREACHED */ |
| 756 | return 0; |
| 757 | } |
| 758 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 759 | static int pl2303_ioctl(struct usb_serial_port *port, struct file *file, |
| 760 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
| 762 | dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd); |
| 763 | |
| 764 | switch (cmd) { |
| 765 | case TIOCMIWAIT: |
| 766 | dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number); |
| 767 | return wait_modem_info(port, arg); |
| 768 | |
| 769 | default: |
| 770 | dbg("%s not supported = 0x%04x", __FUNCTION__, cmd); |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | return -ENOIOCTLCMD; |
| 775 | } |
| 776 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 777 | static void pl2303_break_ctl(struct usb_serial_port *port, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
| 779 | struct usb_serial *serial = port->serial; |
| 780 | u16 state; |
| 781 | int result; |
| 782 | |
| 783 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 784 | |
| 785 | if (break_state == 0) |
| 786 | state = BREAK_OFF; |
| 787 | else |
| 788 | state = BREAK_ON; |
| 789 | dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on"); |
| 790 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 791 | result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 792 | BREAK_REQUEST, BREAK_REQUEST_TYPE, state, |
| 793 | 0, NULL, 0, 100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (result) |
| 795 | dbg("%s - error sending break = %d", __FUNCTION__, result); |
| 796 | } |
| 797 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 798 | static void pl2303_shutdown(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | { |
| 800 | int i; |
| 801 | struct pl2303_private *priv; |
| 802 | |
| 803 | dbg("%s", __FUNCTION__); |
| 804 | |
| 805 | for (i = 0; i < serial->num_ports; ++i) { |
| 806 | priv = usb_get_serial_port_data(serial->port[i]); |
| 807 | if (priv) { |
| 808 | pl2303_buf_free(priv->buf); |
| 809 | kfree(priv); |
| 810 | usb_set_serial_port_data(serial->port[i], NULL); |
| 811 | } |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 812 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 815 | static void pl2303_update_line_status(struct usb_serial_port *port, |
| 816 | unsigned char *data, |
| 817 | unsigned int actual_length) |
| 818 | { |
| 819 | |
| 820 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 821 | unsigned long flags; |
| 822 | u8 status_idx = UART_STATE; |
Horst Schirmeier | 95f209f | 2005-07-28 15:32:20 +0200 | [diff] [blame] | 823 | u8 length = UART_STATE + 1; |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 824 | |
| 825 | if ((le16_to_cpu(port->serial->dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) && |
Luiz Fernando Capitulino | b8f4c1d | 2005-11-17 09:47:36 -0800 | [diff] [blame] | 826 | (le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X65 || |
| 827 | le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_SX1 || |
| 828 | le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X75)) { |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 829 | length = 1; |
| 830 | status_idx = 0; |
| 831 | } |
| 832 | |
| 833 | if (actual_length < length) |
Luiz Fernando N. Capitulino | a009b75 | 2006-07-25 16:58:30 -0300 | [diff] [blame] | 834 | return; |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 835 | |
| 836 | /* Save off the uart status for others to look at */ |
| 837 | spin_lock_irqsave(&priv->lock, flags); |
| 838 | priv->line_status = data[status_idx]; |
| 839 | spin_unlock_irqrestore(&priv->lock, flags); |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 840 | wake_up_interruptible(&priv->delta_msr_wait); |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 843 | static void pl2303_read_int_callback(struct urb *urb, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
| 845 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | unsigned char *data = urb->transfer_buffer; |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 847 | unsigned int actual_length = urb->actual_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
| 850 | dbg("%s (%d)", __FUNCTION__, port->number); |
| 851 | |
| 852 | switch (urb->status) { |
| 853 | case 0: |
| 854 | /* success */ |
| 855 | break; |
| 856 | case -ECONNRESET: |
| 857 | case -ENOENT: |
| 858 | case -ESHUTDOWN: |
| 859 | /* this urb is terminated, clean up */ |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 860 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, |
| 861 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | return; |
| 863 | default: |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 864 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, |
| 865 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | goto exit; |
| 867 | } |
| 868 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 869 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, |
| 870 | urb->actual_length, urb->transfer_buffer); |
| 871 | |
Flavio Leitner | 97bb13e | 2005-04-18 17:39:31 -0700 | [diff] [blame] | 872 | pl2303_update_line_status(port, data, actual_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | exit: |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 875 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (status) |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 877 | dev_err(&urb->dev->dev, |
| 878 | "%s - usb_submit_urb failed with result %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | __FUNCTION__, status); |
| 880 | } |
| 881 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 882 | static void pl2303_read_bulk_callback(struct urb *urb, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
| 884 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; |
| 885 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 886 | struct tty_struct *tty; |
| 887 | unsigned char *data = urb->transfer_buffer; |
| 888 | unsigned long flags; |
| 889 | int i; |
| 890 | int result; |
| 891 | u8 status; |
| 892 | char tty_flag; |
| 893 | |
| 894 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 895 | |
| 896 | if (urb->status) { |
| 897 | dbg("%s - urb->status = %d", __FUNCTION__, urb->status); |
| 898 | if (!port->open_count) { |
| 899 | dbg("%s - port is closed, exiting.", __FUNCTION__); |
| 900 | return; |
| 901 | } |
| 902 | if (urb->status == -EPROTO) { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 903 | /* PL2303 mysteriously fails with -EPROTO reschedule |
| 904 | * the read */ |
| 905 | dbg("%s - caught -EPROTO, resubmitting the urb", |
| 906 | __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | urb->status = 0; |
| 908 | urb->dev = port->serial->dev; |
| 909 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 910 | if (result) |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 911 | dev_err(&urb->dev->dev, "%s - failed" |
| 912 | " resubmitting read urb, error %d\n", |
| 913 | __FUNCTION__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return; |
| 915 | } |
| 916 | dbg("%s - unable to handle the error, exiting.", __FUNCTION__); |
| 917 | return; |
| 918 | } |
| 919 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 920 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, |
| 921 | urb->actual_length, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | /* get tty_flag from status */ |
| 924 | tty_flag = TTY_NORMAL; |
| 925 | |
| 926 | spin_lock_irqsave(&priv->lock, flags); |
| 927 | status = priv->line_status; |
| 928 | priv->line_status &= ~UART_STATE_TRANSIENT_MASK; |
| 929 | spin_unlock_irqrestore(&priv->lock, flags); |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 930 | wake_up_interruptible(&priv->delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
| 932 | /* break takes precedence over parity, */ |
| 933 | /* which takes precedence over framing errors */ |
| 934 | if (status & UART_BREAK_ERROR ) |
| 935 | tty_flag = TTY_BREAK; |
| 936 | else if (status & UART_PARITY_ERROR) |
| 937 | tty_flag = TTY_PARITY; |
| 938 | else if (status & UART_FRAME_ERROR) |
| 939 | tty_flag = TTY_FRAME; |
| 940 | dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag); |
| 941 | |
| 942 | tty = port->tty; |
| 943 | if (tty && urb->actual_length) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 944 | tty_buffer_request_room(tty, urb->actual_length + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* overrun is special, not associated with a char */ |
| 946 | if (status & UART_OVERRUN_ERROR) |
| 947 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 948 | for (i = 0; i < urb->actual_length; ++i) |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 949 | tty_insert_flip_char(tty, data[i], tty_flag); |
| 950 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /* Schedule the next read _if_ we are still open */ |
| 954 | if (port->open_count) { |
| 955 | urb->dev = port->serial->dev; |
| 956 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 957 | if (result) |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 958 | dev_err(&urb->dev->dev, "%s - failed resubmitting" |
| 959 | " read urb, error %d\n", __FUNCTION__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | return; |
| 963 | } |
| 964 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 965 | static void pl2303_write_bulk_callback(struct urb *urb, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
| 967 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; |
| 968 | struct pl2303_private *priv = usb_get_serial_port_data(port); |
| 969 | int result; |
| 970 | |
| 971 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 972 | |
| 973 | switch (urb->status) { |
| 974 | case 0: |
| 975 | /* success */ |
| 976 | break; |
| 977 | case -ECONNRESET: |
| 978 | case -ENOENT: |
| 979 | case -ESHUTDOWN: |
| 980 | /* this urb is terminated, clean up */ |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 981 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, |
| 982 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | priv->write_urb_in_use = 0; |
| 984 | return; |
| 985 | default: |
| 986 | /* error in the urb, so we have to resubmit it */ |
| 987 | dbg("%s - Overflow in write", __FUNCTION__); |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 988 | dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, |
| 989 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | port->write_urb->transfer_buffer_length = 1; |
| 991 | port->write_urb->dev = port->serial->dev; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 992 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (result) |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 994 | dev_err(&urb->dev->dev, "%s - failed resubmitting write" |
| 995 | " urb, error %d\n", __FUNCTION__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | else |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | priv->write_urb_in_use = 0; |
| 1001 | |
| 1002 | /* send any buffered data */ |
| 1003 | pl2303_send(port); |
| 1004 | } |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | /* |
| 1007 | * pl2303_buf_alloc |
| 1008 | * |
| 1009 | * Allocate a circular buffer and all associated memory. |
| 1010 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | static struct pl2303_buf *pl2303_buf_alloc(unsigned int size) |
| 1012 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | struct pl2303_buf *pb; |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (size == 0) |
| 1016 | return NULL; |
| 1017 | |
| 1018 | pb = (struct pl2303_buf *)kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL); |
| 1019 | if (pb == NULL) |
| 1020 | return NULL; |
| 1021 | |
| 1022 | pb->buf_buf = kmalloc(size, GFP_KERNEL); |
| 1023 | if (pb->buf_buf == NULL) { |
| 1024 | kfree(pb); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | pb->buf_size = size; |
| 1029 | pb->buf_get = pb->buf_put = pb->buf_buf; |
| 1030 | |
| 1031 | return pb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * pl2303_buf_free |
| 1036 | * |
| 1037 | * Free the buffer and all associated memory. |
| 1038 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | static void pl2303_buf_free(struct pl2303_buf *pb) |
| 1040 | { |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1041 | if (pb) { |
| 1042 | kfree(pb->buf_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | kfree(pb); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* |
| 1048 | * pl2303_buf_clear |
| 1049 | * |
| 1050 | * Clear out all data in the circular buffer. |
| 1051 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | static void pl2303_buf_clear(struct pl2303_buf *pb) |
| 1053 | { |
| 1054 | if (pb != NULL) |
| 1055 | pb->buf_get = pb->buf_put; |
| 1056 | /* equivalent to a get of all data available */ |
| 1057 | } |
| 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | /* |
| 1060 | * pl2303_buf_data_avail |
| 1061 | * |
| 1062 | * Return the number of bytes of data available in the circular |
| 1063 | * buffer. |
| 1064 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb) |
| 1066 | { |
| 1067 | if (pb != NULL) |
| 1068 | return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size); |
| 1069 | else |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* |
| 1074 | * pl2303_buf_space_avail |
| 1075 | * |
| 1076 | * Return the number of bytes of space available in the circular |
| 1077 | * buffer. |
| 1078 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb) |
| 1080 | { |
| 1081 | if (pb != NULL) |
| 1082 | return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size); |
| 1083 | else |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | /* |
| 1088 | * pl2303_buf_put |
| 1089 | * |
| 1090 | * Copy data data from a user buffer and put it into the circular buffer. |
| 1091 | * Restrict to the amount of space available. |
| 1092 | * |
| 1093 | * Return the number of bytes copied. |
| 1094 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf, |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1096 | unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | unsigned int len; |
| 1099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | if (pb == NULL) |
| 1101 | return 0; |
| 1102 | |
| 1103 | len = pl2303_buf_space_avail(pb); |
| 1104 | if (count > len) |
| 1105 | count = len; |
| 1106 | |
| 1107 | if (count == 0) |
| 1108 | return 0; |
| 1109 | |
| 1110 | len = pb->buf_buf + pb->buf_size - pb->buf_put; |
| 1111 | if (count > len) { |
| 1112 | memcpy(pb->buf_put, buf, len); |
| 1113 | memcpy(pb->buf_buf, buf+len, count - len); |
| 1114 | pb->buf_put = pb->buf_buf + count - len; |
| 1115 | } else { |
| 1116 | memcpy(pb->buf_put, buf, count); |
| 1117 | if (count < len) |
| 1118 | pb->buf_put += count; |
| 1119 | else /* count == len */ |
| 1120 | pb->buf_put = pb->buf_buf; |
| 1121 | } |
| 1122 | |
| 1123 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | /* |
| 1127 | * pl2303_buf_get |
| 1128 | * |
| 1129 | * Get data from the circular buffer and copy to the given buffer. |
| 1130 | * Restrict to the amount of data available. |
| 1131 | * |
| 1132 | * Return the number of bytes copied. |
| 1133 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf, |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1135 | unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | unsigned int len; |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | if (pb == NULL) |
| 1140 | return 0; |
| 1141 | |
| 1142 | len = pl2303_buf_data_avail(pb); |
| 1143 | if (count > len) |
| 1144 | count = len; |
| 1145 | |
| 1146 | if (count == 0) |
| 1147 | return 0; |
| 1148 | |
| 1149 | len = pb->buf_buf + pb->buf_size - pb->buf_get; |
| 1150 | if (count > len) { |
| 1151 | memcpy(buf, pb->buf_get, len); |
| 1152 | memcpy(buf+len, pb->buf_buf, count - len); |
| 1153 | pb->buf_get = pb->buf_buf + count - len; |
| 1154 | } else { |
| 1155 | memcpy(buf, pb->buf_get, count); |
| 1156 | if (count < len) |
| 1157 | pb->buf_get += count; |
| 1158 | else /* count == len */ |
| 1159 | pb->buf_get = pb->buf_buf; |
| 1160 | } |
| 1161 | |
| 1162 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1165 | static int __init pl2303_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
| 1167 | int retval; |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | retval = usb_serial_register(&pl2303_device); |
| 1170 | if (retval) |
| 1171 | goto failed_usb_serial_register; |
| 1172 | retval = usb_register(&pl2303_driver); |
| 1173 | if (retval) |
| 1174 | goto failed_usb_register; |
Greg Kroah-Hartman | 17a882f | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1175 | info(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | return 0; |
| 1177 | failed_usb_register: |
| 1178 | usb_serial_deregister(&pl2303_device); |
| 1179 | failed_usb_serial_register: |
| 1180 | return retval; |
| 1181 | } |
| 1182 | |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1183 | static void __exit pl2303_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
Thiago Galesi | 372db8a | 2006-07-31 15:39:27 -0300 | [diff] [blame^] | 1185 | usb_deregister(&pl2303_driver); |
| 1186 | usb_serial_deregister(&pl2303_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | module_init(pl2303_init); |
| 1190 | module_exit(pl2303_exit); |
| 1191 | |
| 1192 | MODULE_DESCRIPTION(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | MODULE_LICENSE("GPL"); |
| 1194 | |
| 1195 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1196 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |
| 1197 | |