Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
502b95c1 | 11 | * the Free Software Foundation; either version 2 of the License. |
1da177e4 LT |
12 | * |
13 | * See Documentation/usb/usb-serial.txt for more information on using this driver | |
14 | * | |
1da177e4 LT |
15 | */ |
16 | ||
1da177e4 LT |
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> | |
a969888c | 30 | #include <linux/usb/serial.h> |
1da177e4 LT |
31 | #include "pl2303.h" |
32 | ||
33 | /* | |
34 | * Version Information | |
35 | */ | |
1da177e4 LT |
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 | ||
1da177e4 LT |
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) }, | |
3d861494 | 55 | { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) }, |
1da177e4 LT |
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) }, | |
58381719 | 64 | { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) }, |
1da177e4 LT |
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) }, | |
a8310f3b | 73 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) }, |
1da177e4 | 74 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) }, |
a8310f3b | 75 | { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) }, |
acbb36f1 | 76 | { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) }, |
c6c27721 CL |
77 | { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) }, |
78 | { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) }, | |
6cceb05f | 79 | { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) }, |
c6c27721 | 80 | { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) }, |
491b04ce | 81 | { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) }, |
3b928474 | 82 | { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) }, |
b7aa94b6 | 83 | { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) }, |
1da177e4 LT |
84 | { } /* Terminating entry */ |
85 | }; | |
86 | ||
372db8a7 | 87 | MODULE_DEVICE_TABLE(usb, id_table); |
1da177e4 LT |
88 | |
89 | static struct usb_driver pl2303_driver = { | |
1da177e4 LT |
90 | .name = "pl2303", |
91 | .probe = usb_serial_probe, | |
92 | .disconnect = usb_serial_disconnect, | |
93 | .id_table = id_table, | |
ba9dc657 | 94 | .no_dynamic_id = 1, |
1da177e4 LT |
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 */ | |
ea65370d | 163 | static struct usb_serial_driver pl2303_device = { |
18fcac35 GKH |
164 | .driver = { |
165 | .owner = THIS_MODULE, | |
269bda1c | 166 | .name = "pl2303", |
18fcac35 | 167 | }, |
1da177e4 LT |
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 | ||
372db8a7 | 208 | static int pl2303_startup(struct usb_serial *serial) |
1da177e4 LT |
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) { | |
80b6ca48 | 225 | priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL); |
1da177e4 LT |
226 | if (!priv) |
227 | goto cleanup; | |
1da177e4 LT |
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 | ||
372db8a7 | 250 | static int set_control_lines(struct usb_device *dev, u8 value) |
1da177e4 LT |
251 | { |
252 | int retval; | |
253 | ||
372db8a7 TG |
254 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
255 | SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE, | |
256 | value, 0, NULL, 0, 100); | |
1da177e4 LT |
257 | dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval); |
258 | return retval; | |
259 | } | |
260 | ||
372db8a7 TG |
261 | static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf, |
262 | int count) | |
1da177e4 LT |
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, | |
372db8a7 | 297 | port->bulk_out_size); |
1da177e4 LT |
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 | ||
372db8a7 TG |
308 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, |
309 | port->write_urb->transfer_buffer); | |
1da177e4 LT |
310 | |
311 | port->write_urb->transfer_buffer_length = count; | |
312 | port->write_urb->dev = port->serial->dev; | |
372db8a7 | 313 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
1da177e4 | 314 | if (result) { |
372db8a7 TG |
315 | dev_err(&port->dev, "%s - failed submitting write urb," |
316 | " error %d\n", __FUNCTION__, result); | |
1da177e4 LT |
317 | priv->write_urb_in_use = 0; |
318 | // TODO: reschedule pl2303_send | |
319 | } | |
320 | ||
cf2c7481 | 321 | usb_serial_port_softint(port); |
1da177e4 LT |
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 | ||
372db8a7 TG |
356 | static void pl2303_set_termios(struct usb_serial_port *port, |
357 | struct termios *old_termios) | |
1da177e4 LT |
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; | |
372db8a7 TG |
378 | port->tty->termios->c_cflag = B9600 | CS8 | CREAD | |
379 | HUPCL | CLOCAL; | |
1da177e4 LT |
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) && | |
372db8a7 TG |
388 | (RELEVANT_IFLAG(port->tty->termios->c_iflag) == |
389 | RELEVANT_IFLAG(old_termios->c_iflag))) { | |
390 | dbg("%s - nothing to change...", __FUNCTION__); | |
391 | return; | |
1da177e4 LT |
392 | } |
393 | } | |
394 | ||
372db8a7 | 395 | buf = kzalloc(7, GFP_KERNEL); |
1da177e4 LT |
396 | if (!buf) { |
397 | dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); | |
398 | return; | |
399 | } | |
1da177e4 | 400 | |
372db8a7 TG |
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]); | |
1da177e4 LT |
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: | |
372db8a7 TG |
437 | dev_err(&port->dev, "pl2303 driver does not support" |
438 | " the baudrate requested (fix it)\n"); | |
1da177e4 LT |
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 | ||
372db8a7 TG |
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); | |
1da177e4 LT |
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 | } | |
372db8a7 | 497 | |
1da177e4 LT |
498 | buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0; |
499 | ||
372db8a7 TG |
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, | |
1da177e4 LT |
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; | |
372db8a7 | 512 | i = usb_control_msg(serial->dev, |
1da177e4 LT |
513 | usb_sndctrlpipe(serial->dev, 0), |
514 | VENDOR_WRITE_REQUEST, | |
515 | VENDOR_WRITE_REQUEST_TYPE, | |
516 | 0x0, index, NULL, 0, 100); | |
372db8a7 | 517 | dbg("0x40:0x1:0x0:0x%x %d", index, i); |
1da177e4 LT |
518 | } |
519 | ||
372db8a7 | 520 | kfree(buf); |
1da177e4 LT |
521 | } |
522 | ||
372db8a7 | 523 | static int pl2303_open(struct usb_serial_port *port, struct file *filp) |
1da177e4 LT |
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 | ||
1694899f D |
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 | } | |
1da177e4 LT |
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) { | |
372db8a7 | 577 | pl2303_set_termios(port, &tmp_termios); |
1da177e4 LT |
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; | |
372db8a7 | 584 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
1da177e4 | 585 | if (result) { |
372db8a7 TG |
586 | dev_err(&port->dev, "%s - failed submitting read urb," |
587 | " error %d\n", __FUNCTION__, result); | |
588 | pl2303_close(port, NULL); | |
1da177e4 LT |
589 | return -EPROTO; |
590 | } | |
591 | ||
592 | dbg("%s - submitting interrupt urb", __FUNCTION__); | |
593 | port->interrupt_in_urb->dev = serial->dev; | |
372db8a7 | 594 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
1da177e4 | 595 | if (result) { |
372db8a7 TG |
596 | dev_err(&port->dev, "%s - failed submitting interrupt urb," |
597 | " error %d\n", __FUNCTION__, result); | |
598 | pl2303_close(port, NULL); | |
1da177e4 LT |
599 | return -EPROTO; |
600 | } | |
601 | return 0; | |
602 | } | |
603 | ||
372db8a7 | 604 | static void pl2303_close(struct usb_serial_port *port, struct file *filp) |
1da177e4 LT |
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; | |
80b47853 | 611 | wait_queue_t wait; |
1da177e4 LT |
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); | |
372db8a7 TG |
622 | if (pl2303_buf_data_avail(priv->buf) == 0 || |
623 | timeout == 0 || signal_pending(current) || | |
624 | !usb_get_intfdata(port->serial->interface)) /* disconnect */ | |
1da177e4 LT |
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; | |
22c43863 | 648 | schedule_timeout_interruptible(timeout); |
1da177e4 LT |
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; | |
372db8a7 TG |
662 | spin_unlock_irqrestore(&priv->lock, flags); |
663 | set_control_lines(port->serial->dev, 0); | |
1da177e4 LT |
664 | } |
665 | } | |
666 | } | |
667 | ||
372db8a7 TG |
668 | static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file, |
669 | unsigned int set, unsigned int clear) | |
1da177e4 LT |
670 | { |
671 | struct pl2303_private *priv = usb_get_serial_port_data(port); | |
672 | unsigned long flags; | |
673 | u8 control; | |
674 | ||
6fdd8e8e FL |
675 | if (!usb_get_intfdata(port->serial->interface)) |
676 | return -ENODEV; | |
677 | ||
372db8a7 | 678 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 LT |
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; | |
372db8a7 | 688 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 | 689 | |
372db8a7 | 690 | return set_control_lines(port->serial->dev, control); |
1da177e4 LT |
691 | } |
692 | ||
372db8a7 | 693 | static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file) |
1da177e4 LT |
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 | ||
6fdd8e8e FL |
703 | if (!usb_get_intfdata(port->serial->interface)) |
704 | return -ENODEV; | |
705 | ||
372db8a7 | 706 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 LT |
707 | mcr = priv->line_control; |
708 | status = priv->line_status; | |
372db8a7 | 709 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
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 | ||
372db8a7 | 731 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 | 732 | prevstatus = priv->line_status; |
372db8a7 | 733 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
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; | |
372db8a7 TG |
740 | |
741 | spin_lock_irqsave(&priv->lock, flags); | |
1da177e4 | 742 | status = priv->line_status; |
372db8a7 TG |
743 | spin_unlock_irqrestore(&priv->lock, flags); |
744 | ||
1da177e4 | 745 | changed=prevstatus^status; |
372db8a7 | 746 | |
1da177e4 LT |
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 | ||
372db8a7 TG |
759 | static int pl2303_ioctl(struct usb_serial_port *port, struct file *file, |
760 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
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 | ||
372db8a7 | 777 | static void pl2303_break_ctl(struct usb_serial_port *port, int break_state) |
1da177e4 LT |
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 | ||
372db8a7 TG |
791 | result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
792 | BREAK_REQUEST, BREAK_REQUEST_TYPE, state, | |
793 | 0, NULL, 0, 100); | |
1da177e4 LT |
794 | if (result) |
795 | dbg("%s - error sending break = %d", __FUNCTION__, result); | |
796 | } | |
797 | ||
372db8a7 | 798 | static void pl2303_shutdown(struct usb_serial *serial) |
1da177e4 LT |
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 | } | |
372db8a7 | 812 | } |
1da177e4 LT |
813 | } |
814 | ||
97bb13ec FL |
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; | |
95f209f9 | 823 | u8 length = UART_STATE + 1; |
97bb13ec FL |
824 | |
825 | if ((le16_to_cpu(port->serial->dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) && | |
b8f4c1d6 LFC |
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)) { | |
97bb13ec FL |
829 | length = 1; |
830 | status_idx = 0; | |
831 | } | |
832 | ||
833 | if (actual_length < length) | |
a009b75a | 834 | return; |
97bb13ec FL |
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); | |
372db8a7 | 840 | wake_up_interruptible(&priv->delta_msr_wait); |
97bb13ec | 841 | } |
1da177e4 | 842 | |
372db8a7 | 843 | static void pl2303_read_int_callback(struct urb *urb, struct pt_regs *regs) |
1da177e4 LT |
844 | { |
845 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; | |
1da177e4 | 846 | unsigned char *data = urb->transfer_buffer; |
97bb13ec | 847 | unsigned int actual_length = urb->actual_length; |
1da177e4 | 848 | int status; |
1da177e4 LT |
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 */ | |
372db8a7 TG |
860 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, |
861 | urb->status); | |
1da177e4 LT |
862 | return; |
863 | default: | |
372db8a7 TG |
864 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, |
865 | urb->status); | |
1da177e4 LT |
866 | goto exit; |
867 | } | |
868 | ||
372db8a7 TG |
869 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, |
870 | urb->actual_length, urb->transfer_buffer); | |
871 | ||
97bb13ec | 872 | pl2303_update_line_status(port, data, actual_length); |
1da177e4 | 873 | |
1da177e4 | 874 | exit: |
372db8a7 | 875 | status = usb_submit_urb(urb, GFP_ATOMIC); |
1da177e4 | 876 | if (status) |
372db8a7 TG |
877 | dev_err(&urb->dev->dev, |
878 | "%s - usb_submit_urb failed with result %d\n", | |
1da177e4 LT |
879 | __FUNCTION__, status); |
880 | } | |
881 | ||
372db8a7 | 882 | static void pl2303_read_bulk_callback(struct urb *urb, struct pt_regs *regs) |
1da177e4 LT |
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) { | |
372db8a7 TG |
903 | /* PL2303 mysteriously fails with -EPROTO reschedule |
904 | * the read */ | |
905 | dbg("%s - caught -EPROTO, resubmitting the urb", | |
906 | __FUNCTION__); | |
1da177e4 LT |
907 | urb->status = 0; |
908 | urb->dev = port->serial->dev; | |
909 | result = usb_submit_urb(urb, GFP_ATOMIC); | |
910 | if (result) | |
372db8a7 TG |
911 | dev_err(&urb->dev->dev, "%s - failed" |
912 | " resubmitting read urb, error %d\n", | |
913 | __FUNCTION__, result); | |
1da177e4 LT |
914 | return; |
915 | } | |
916 | dbg("%s - unable to handle the error, exiting.", __FUNCTION__); | |
917 | return; | |
918 | } | |
919 | ||
372db8a7 TG |
920 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, |
921 | urb->actual_length, data); | |
1da177e4 LT |
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); | |
372db8a7 | 930 | wake_up_interruptible(&priv->delta_msr_wait); |
1da177e4 LT |
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) { | |
33f0f88f | 944 | tty_buffer_request_room(tty, urb->actual_length + 1); |
1da177e4 LT |
945 | /* overrun is special, not associated with a char */ |
946 | if (status & UART_OVERRUN_ERROR) | |
947 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | |
33f0f88f | 948 | for (i = 0; i < urb->actual_length; ++i) |
372db8a7 TG |
949 | tty_insert_flip_char(tty, data[i], tty_flag); |
950 | tty_flip_buffer_push(tty); | |
1da177e4 LT |
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) | |
372db8a7 TG |
958 | dev_err(&urb->dev->dev, "%s - failed resubmitting" |
959 | " read urb, error %d\n", __FUNCTION__, result); | |
1da177e4 LT |
960 | } |
961 | ||
962 | return; | |
963 | } | |
964 | ||
372db8a7 | 965 | static void pl2303_write_bulk_callback(struct urb *urb, struct pt_regs *regs) |
1da177e4 LT |
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 */ | |
372db8a7 TG |
981 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, |
982 | urb->status); | |
1da177e4 LT |
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__); | |
372db8a7 TG |
988 | dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, |
989 | urb->status); | |
1da177e4 LT |
990 | port->write_urb->transfer_buffer_length = 1; |
991 | port->write_urb->dev = port->serial->dev; | |
372db8a7 | 992 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
1da177e4 | 993 | if (result) |
372db8a7 TG |
994 | dev_err(&urb->dev->dev, "%s - failed resubmitting write" |
995 | " urb, error %d\n", __FUNCTION__, result); | |
1da177e4 LT |
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 | ||
1da177e4 LT |
1006 | /* |
1007 | * pl2303_buf_alloc | |
1008 | * | |
1009 | * Allocate a circular buffer and all associated memory. | |
1010 | */ | |
1da177e4 LT |
1011 | static struct pl2303_buf *pl2303_buf_alloc(unsigned int size) |
1012 | { | |
1da177e4 LT |
1013 | struct pl2303_buf *pb; |
1014 | ||
1da177e4 LT |
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; | |
1da177e4 LT |
1032 | } |
1033 | ||
1da177e4 LT |
1034 | /* |
1035 | * pl2303_buf_free | |
1036 | * | |
1037 | * Free the buffer and all associated memory. | |
1038 | */ | |
1da177e4 LT |
1039 | static void pl2303_buf_free(struct pl2303_buf *pb) |
1040 | { | |
1bc3c9e1 JJ |
1041 | if (pb) { |
1042 | kfree(pb->buf_buf); | |
1da177e4 LT |
1043 | kfree(pb); |
1044 | } | |
1045 | } | |
1046 | ||
1da177e4 LT |
1047 | /* |
1048 | * pl2303_buf_clear | |
1049 | * | |
1050 | * Clear out all data in the circular buffer. | |
1051 | */ | |
1da177e4 LT |
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 | ||
1da177e4 LT |
1059 | /* |
1060 | * pl2303_buf_data_avail | |
1061 | * | |
1062 | * Return the number of bytes of data available in the circular | |
1063 | * buffer. | |
1064 | */ | |
1da177e4 LT |
1065 | static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb) |
1066 | { | |
10a18cd1 | 1067 | if (pb == NULL) |
1da177e4 | 1068 | return 0; |
10a18cd1 TG |
1069 | |
1070 | return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size); | |
1da177e4 LT |
1071 | } |
1072 | ||
1da177e4 LT |
1073 | /* |
1074 | * pl2303_buf_space_avail | |
1075 | * | |
1076 | * Return the number of bytes of space available in the circular | |
1077 | * buffer. | |
1078 | */ | |
1da177e4 LT |
1079 | static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb) |
1080 | { | |
10a18cd1 | 1081 | if (pb == NULL) |
1da177e4 | 1082 | return 0; |
10a18cd1 TG |
1083 | |
1084 | return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size); | |
1da177e4 LT |
1085 | } |
1086 | ||
1da177e4 LT |
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 | */ | |
1da177e4 | 1095 | static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf, |
372db8a7 | 1096 | unsigned int count) |
1da177e4 | 1097 | { |
1da177e4 LT |
1098 | unsigned int len; |
1099 | ||
1da177e4 LT |
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; | |
1da177e4 LT |
1124 | } |
1125 | ||
1da177e4 LT |
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 | */ | |
1da177e4 | 1134 | static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf, |
372db8a7 | 1135 | unsigned int count) |
1da177e4 | 1136 | { |
1da177e4 LT |
1137 | unsigned int len; |
1138 | ||
1da177e4 LT |
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; | |
1da177e4 LT |
1163 | } |
1164 | ||
372db8a7 | 1165 | static int __init pl2303_init(void) |
1da177e4 LT |
1166 | { |
1167 | int retval; | |
372db8a7 | 1168 | |
1da177e4 LT |
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; | |
17a882fc | 1175 | info(DRIVER_DESC); |
1da177e4 LT |
1176 | return 0; |
1177 | failed_usb_register: | |
1178 | usb_serial_deregister(&pl2303_device); | |
1179 | failed_usb_serial_register: | |
1180 | return retval; | |
1181 | } | |
1182 | ||
372db8a7 | 1183 | static void __exit pl2303_exit(void) |
1da177e4 | 1184 | { |
372db8a7 TG |
1185 | usb_deregister(&pl2303_driver); |
1186 | usb_serial_deregister(&pl2303_device); | |
1da177e4 LT |
1187 | } |
1188 | ||
1da177e4 LT |
1189 | module_init(pl2303_init); |
1190 | module_exit(pl2303_exit); | |
1191 | ||
1192 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1da177e4 LT |
1193 | MODULE_LICENSE("GPL"); |
1194 | ||
1195 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
1196 | MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
1197 |