blob: cf8add91de0585a3f40fe150f6d03f9b9100f4dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Prolific PL2303 USB to serial adaptor driver
3 *
Greg Kroah-Hartman4d0dce32007-06-12 11:43:37 -07004 * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2003 IBM Corp.
6 *
7 * Original driver for 2.2.x by anonymous
8 *
Greg Kroah-Hartman4d0dce32007-06-12 11:43:37 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * See Documentation/usb/usb-serial.txt for more information on using this driver
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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-Hartmana9698882006-07-11 21:22:58 -070030#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "pl2303.h"
32
33/*
34 * Version Information
35 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
37
38static 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 Torvalds1da177e2005-04-16 15:20:36 -070045struct pl2303_buf {
46 unsigned int buf_size;
47 char *buf_buf;
48 char *buf_get;
49 char *buf_put;
50};
51
52static 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 Moulder3d861492006-06-19 22:47:49 +100055 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 { 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) },
Masakazu Mokuno8a28dea2007-10-23 13:51:57 +090059 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
61 { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
62 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
63 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
64 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
Wang Jun58381712006-04-19 16:32:07 +080065 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
67 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
68 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
69 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
70 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
71 { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
72 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
73 { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
Luiz Fernando Capitulinoa8310f32005-11-17 09:47:32 -080074 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
Luiz Fernando Capitulinoa8310f32005-11-17 09:47:32 -080076 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
Andreas Loible7beb662007-08-24 01:51:11 +020077 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
Peter Favrholdtacbb36f2005-04-18 17:39:32 -070078 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
Christian Lindnerc6c27722006-02-01 14:10:52 +010079 { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
80 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
Denis MONTERRAT6cceb052006-01-19 14:52:38 +010081 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
Christian Lindnerc6c27722006-02-01 14:10:52 +010082 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
Dick Streefland491b04c2006-03-01 00:53:33 -080083 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
Matthew Meno3b928472006-06-21 15:25:53 -040084 { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
Kim Oldfieldb7aa94b2006-07-25 15:54:59 +100085 { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
Johannes Steingraeber8fd80132006-09-16 16:17:34 +020086 { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
Wesley PA4WDHb697f702006-09-28 20:45:38 +020087 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_ID) },
YOSHIFUJI Hideaki2d94b982007-01-26 22:51:38 +090088 { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
Magnus Damm9e3285d2007-11-08 16:45:46 +090089 { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 { } /* Terminating entry */
91};
92
Thiago Galesi372db8a2006-07-31 15:39:27 -030093MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static struct usb_driver pl2303_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .name = "pl2303",
97 .probe = usb_serial_probe,
98 .disconnect = usb_serial_disconnect,
99 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800100 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103#define SET_LINE_REQUEST_TYPE 0x21
104#define SET_LINE_REQUEST 0x20
105
106#define SET_CONTROL_REQUEST_TYPE 0x21
107#define SET_CONTROL_REQUEST 0x22
108#define CONTROL_DTR 0x01
109#define CONTROL_RTS 0x02
110
111#define BREAK_REQUEST_TYPE 0x21
112#define BREAK_REQUEST 0x23
113#define BREAK_ON 0xffff
114#define BREAK_OFF 0x0000
115
116#define GET_LINE_REQUEST_TYPE 0xa1
117#define GET_LINE_REQUEST 0x21
118
119#define VENDOR_WRITE_REQUEST_TYPE 0x40
120#define VENDOR_WRITE_REQUEST 0x01
121
122#define VENDOR_READ_REQUEST_TYPE 0xc0
123#define VENDOR_READ_REQUEST 0x01
124
125#define UART_STATE 0x08
126#define UART_STATE_TRANSIENT_MASK 0x74
127#define UART_DCD 0x01
128#define UART_DSR 0x02
129#define UART_BREAK_ERROR 0x04
130#define UART_RING 0x08
131#define UART_FRAME_ERROR 0x10
132#define UART_PARITY_ERROR 0x20
133#define UART_OVERRUN_ERROR 0x40
134#define UART_CTS 0x80
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137enum pl2303_type {
138 type_0, /* don't know the difference between type 0 and */
139 type_1, /* type 1, until someone from prolific tells us... */
140 HX, /* HX version of the pl2303 chip */
141};
142
143struct pl2303_private {
144 spinlock_t lock;
145 struct pl2303_buf *buf;
146 int write_urb_in_use;
147 wait_queue_head_t delta_msr_wait;
148 u8 line_control;
149 u8 line_status;
150 u8 termios_initialized;
151 enum pl2303_type type;
152};
153
Thiago Galesi572d3132006-07-29 10:46:37 -0300154/*
155 * pl2303_buf_alloc
156 *
157 * Allocate a circular buffer and all associated memory.
158 */
159static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
160{
161 struct pl2303_buf *pb;
162
163 if (size == 0)
164 return NULL;
165
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800166 pb = kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
Thiago Galesi572d3132006-07-29 10:46:37 -0300167 if (pb == NULL)
168 return NULL;
169
170 pb->buf_buf = kmalloc(size, GFP_KERNEL);
171 if (pb->buf_buf == NULL) {
172 kfree(pb);
173 return NULL;
174 }
175
176 pb->buf_size = size;
177 pb->buf_get = pb->buf_put = pb->buf_buf;
178
179 return pb;
180}
181
182/*
183 * pl2303_buf_free
184 *
185 * Free the buffer and all associated memory.
186 */
187static void pl2303_buf_free(struct pl2303_buf *pb)
188{
189 if (pb) {
190 kfree(pb->buf_buf);
191 kfree(pb);
192 }
193}
194
195/*
196 * pl2303_buf_clear
197 *
198 * Clear out all data in the circular buffer.
199 */
200static void pl2303_buf_clear(struct pl2303_buf *pb)
201{
202 if (pb != NULL)
203 pb->buf_get = pb->buf_put;
204 /* equivalent to a get of all data available */
205}
206
207/*
208 * pl2303_buf_data_avail
209 *
210 * Return the number of bytes of data available in the circular
211 * buffer.
212 */
213static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
214{
215 if (pb == NULL)
216 return 0;
217
218 return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
219}
220
221/*
222 * pl2303_buf_space_avail
223 *
224 * Return the number of bytes of space available in the circular
225 * buffer.
226 */
227static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
228{
229 if (pb == NULL)
230 return 0;
231
232 return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
233}
234
235/*
236 * pl2303_buf_put
237 *
238 * Copy data data from a user buffer and put it into the circular buffer.
239 * Restrict to the amount of space available.
240 *
241 * Return the number of bytes copied.
242 */
243static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
244 unsigned int count)
245{
246 unsigned int len;
247
248 if (pb == NULL)
249 return 0;
250
251 len = pl2303_buf_space_avail(pb);
252 if (count > len)
253 count = len;
254
255 if (count == 0)
256 return 0;
257
258 len = pb->buf_buf + pb->buf_size - pb->buf_put;
259 if (count > len) {
260 memcpy(pb->buf_put, buf, len);
261 memcpy(pb->buf_buf, buf+len, count - len);
262 pb->buf_put = pb->buf_buf + count - len;
263 } else {
264 memcpy(pb->buf_put, buf, count);
265 if (count < len)
266 pb->buf_put += count;
267 else /* count == len */
268 pb->buf_put = pb->buf_buf;
269 }
270
271 return count;
272}
273
274/*
275 * pl2303_buf_get
276 *
277 * Get data from the circular buffer and copy to the given buffer.
278 * Restrict to the amount of data available.
279 *
280 * Return the number of bytes copied.
281 */
282static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
283 unsigned int count)
284{
285 unsigned int len;
286
287 if (pb == NULL)
288 return 0;
289
290 len = pl2303_buf_data_avail(pb);
291 if (count > len)
292 count = len;
293
294 if (count == 0)
295 return 0;
296
297 len = pb->buf_buf + pb->buf_size - pb->buf_get;
298 if (count > len) {
299 memcpy(buf, pb->buf_get, len);
300 memcpy(buf+len, pb->buf_buf, count - len);
301 pb->buf_get = pb->buf_buf + count - len;
302 } else {
303 memcpy(buf, pb->buf_get, count);
304 if (count < len)
305 pb->buf_get += count;
306 else /* count == len */
307 pb->buf_get = pb->buf_buf;
308 }
309
310 return count;
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Thiago Galesi372db8a2006-07-31 15:39:27 -0300313static int pl2303_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct pl2303_private *priv;
316 enum pl2303_type type = type_0;
317 int i;
318
319 if (serial->dev->descriptor.bDeviceClass == 0x02)
320 type = type_0;
321 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
322 type = HX;
323 else if (serial->dev->descriptor.bDeviceClass == 0x00)
324 type = type_1;
325 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
326 type = type_1;
327 dbg("device type: %d", type);
328
329 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100330 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!priv)
332 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 spin_lock_init(&priv->lock);
334 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
335 if (priv->buf == NULL) {
336 kfree(priv);
337 goto cleanup;
338 }
339 init_waitqueue_head(&priv->delta_msr_wait);
340 priv->type = type;
341 usb_set_serial_port_data(serial->port[i], priv);
342 }
343 return 0;
344
345cleanup:
346 for (--i; i>=0; --i) {
347 priv = usb_get_serial_port_data(serial->port[i]);
348 pl2303_buf_free(priv->buf);
349 kfree(priv);
350 usb_set_serial_port_data(serial->port[i], NULL);
351 }
352 return -ENOMEM;
353}
354
Thiago Galesi372db8a2006-07-31 15:39:27 -0300355static int set_control_lines(struct usb_device *dev, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 int retval;
358
Thiago Galesi372db8a2006-07-31 15:39:27 -0300359 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
360 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
361 value, 0, NULL, 0, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
363 return retval;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static void pl2303_send(struct usb_serial_port *port)
367{
368 int count, result;
369 struct pl2303_private *priv = usb_get_serial_port_data(port);
370 unsigned long flags;
371
372 dbg("%s - port %d", __FUNCTION__, port->number);
373
374 spin_lock_irqsave(&priv->lock, flags);
375
376 if (priv->write_urb_in_use) {
377 spin_unlock_irqrestore(&priv->lock, flags);
378 return;
379 }
380
381 count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
Thiago Galesi372db8a2006-07-31 15:39:27 -0300382 port->bulk_out_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (count == 0) {
385 spin_unlock_irqrestore(&priv->lock, flags);
386 return;
387 }
388
389 priv->write_urb_in_use = 1;
390
391 spin_unlock_irqrestore(&priv->lock, flags);
392
Thiago Galesi372db8a2006-07-31 15:39:27 -0300393 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
394 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 port->write_urb->transfer_buffer_length = count;
397 port->write_urb->dev = port->serial->dev;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300398 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (result) {
Thiago Galesi372db8a2006-07-31 15:39:27 -0300400 dev_err(&port->dev, "%s - failed submitting write urb,"
401 " error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 priv->write_urb_in_use = 0;
403 // TODO: reschedule pl2303_send
404 }
405
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700406 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Thiago Galesi572d3132006-07-29 10:46:37 -0300409static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf,
410 int count)
411{
412 struct pl2303_private *priv = usb_get_serial_port_data(port);
413 unsigned long flags;
414
415 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
416
417 if (!count)
418 return count;
419
420 spin_lock_irqsave(&priv->lock, flags);
421 count = pl2303_buf_put(priv->buf, buf, count);
422 spin_unlock_irqrestore(&priv->lock, flags);
423
424 pl2303_send(port);
425
426 return count;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static int pl2303_write_room(struct usb_serial_port *port)
430{
431 struct pl2303_private *priv = usb_get_serial_port_data(port);
432 int room = 0;
433 unsigned long flags;
434
435 dbg("%s - port %d", __FUNCTION__, port->number);
436
437 spin_lock_irqsave(&priv->lock, flags);
438 room = pl2303_buf_space_avail(priv->buf);
439 spin_unlock_irqrestore(&priv->lock, flags);
440
441 dbg("%s - returns %d", __FUNCTION__, room);
442 return room;
443}
444
445static int pl2303_chars_in_buffer(struct usb_serial_port *port)
446{
447 struct pl2303_private *priv = usb_get_serial_port_data(port);
448 int chars = 0;
449 unsigned long flags;
450
451 dbg("%s - port %d", __FUNCTION__, port->number);
452
453 spin_lock_irqsave(&priv->lock, flags);
454 chars = pl2303_buf_data_avail(priv->buf);
455 spin_unlock_irqrestore(&priv->lock, flags);
456
457 dbg("%s - returns %d", __FUNCTION__, chars);
458 return chars;
459}
460
Thiago Galesi372db8a2006-07-31 15:39:27 -0300461static void pl2303_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800462 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct usb_serial *serial = port->serial;
465 struct pl2303_private *priv = usb_get_serial_port_data(port);
466 unsigned long flags;
467 unsigned int cflag;
468 unsigned char *buf;
469 int baud;
470 int i;
471 u8 control;
472
473 dbg("%s - port %d", __FUNCTION__, port->number);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 spin_lock_irqsave(&priv->lock, flags);
476 if (!priv->termios_initialized) {
477 *(port->tty->termios) = tty_std_termios;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300478 port->tty->termios->c_cflag = B9600 | CS8 | CREAD |
479 HUPCL | CLOCAL;
Alan Coxdf64c472007-10-15 20:54:47 +0100480 port->tty->termios->c_ispeed = 9600;
481 port->tty->termios->c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 priv->termios_initialized = 1;
483 }
484 spin_unlock_irqrestore(&priv->lock, flags);
485
486 cflag = port->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Thiago Galesi372db8a2006-07-31 15:39:27 -0300488 buf = kzalloc(7, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (!buf) {
490 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
491 return;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Thiago Galesi372db8a2006-07-31 15:39:27 -0300494 i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
495 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
496 0, 0, buf, 7, 100);
497 dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
498 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (cflag & CSIZE) {
501 switch (cflag & CSIZE) {
502 case CS5: buf[6] = 5; break;
503 case CS6: buf[6] = 6; break;
504 case CS7: buf[6] = 7; break;
505 default:
506 case CS8: buf[6] = 8; break;
507 }
508 dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
509 }
510
Alan Coxe0c79f52007-07-09 12:03:10 -0700511 baud = tty_get_baud_rate(port->tty);;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dbg("%s - baud = %d", __FUNCTION__, baud);
513 if (baud) {
514 buf[0] = baud & 0xff;
515 buf[1] = (baud >> 8) & 0xff;
516 buf[2] = (baud >> 16) & 0xff;
517 buf[3] = (baud >> 24) & 0xff;
518 }
519
520 /* For reference buf[4]=0 is 1 stop bits */
521 /* For reference buf[4]=1 is 1.5 stop bits */
522 /* For reference buf[4]=2 is 2 stop bits */
523 if (cflag & CSTOPB) {
524 buf[4] = 2;
525 dbg("%s - stop bits = 2", __FUNCTION__);
526 } else {
527 buf[4] = 0;
528 dbg("%s - stop bits = 1", __FUNCTION__);
529 }
530
531 if (cflag & PARENB) {
532 /* For reference buf[5]=0 is none parity */
533 /* For reference buf[5]=1 is odd parity */
534 /* For reference buf[5]=2 is even parity */
535 /* For reference buf[5]=3 is mark parity */
536 /* For reference buf[5]=4 is space parity */
537 if (cflag & PARODD) {
538 buf[5] = 1;
539 dbg("%s - parity = odd", __FUNCTION__);
540 } else {
541 buf[5] = 2;
542 dbg("%s - parity = even", __FUNCTION__);
543 }
544 } else {
545 buf[5] = 0;
546 dbg("%s - parity = none", __FUNCTION__);
547 }
548
Thiago Galesi372db8a2006-07-31 15:39:27 -0300549 i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
550 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
551 0, 0, buf, 7, 100);
552 dbg("0x21:0x20:0:0 %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* change control lines if we are switching to or from B0 */
555 spin_lock_irqsave(&priv->lock, flags);
556 control = priv->line_control;
557 if ((cflag & CBAUD) == B0)
558 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
559 else
560 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
561 if (control != priv->line_control) {
562 control = priv->line_control;
563 spin_unlock_irqrestore(&priv->lock, flags);
564 set_control_lines(serial->dev, control);
565 } else {
566 spin_unlock_irqrestore(&priv->lock, flags);
567 }
Thiago Galesi372db8a2006-07-31 15:39:27 -0300568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
570
Thiago Galesi372db8a2006-07-31 15:39:27 -0300571 i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
572 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
573 0, 0, buf, 7, 100);
574 dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
576
577 if (cflag & CRTSCTS) {
578 __u16 index;
579 if (priv->type == HX)
580 index = 0x61;
581 else
582 index = 0x41;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300583 i = usb_control_msg(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 usb_sndctrlpipe(serial->dev, 0),
585 VENDOR_WRITE_REQUEST,
586 VENDOR_WRITE_REQUEST_TYPE,
587 0x0, index, NULL, 0, 100);
Thiago Galesi372db8a2006-07-31 15:39:27 -0300588 dbg("0x40:0x1:0x0:0x%x %d", index, i);
t.sefzick715f9522007-04-25 15:05:22 +0200589 } else {
590 i = usb_control_msg(serial->dev,
591 usb_sndctrlpipe(serial->dev, 0),
592 VENDOR_WRITE_REQUEST,
593 VENDOR_WRITE_REQUEST_TYPE,
594 0x0, 0x0, NULL, 0, 100);
595 dbg ("0x40:0x1:0x0:0x0 %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
Alan Coxdf64c472007-10-15 20:54:47 +0100598 /* FIXME: Need to read back resulting baud rate */
599 if (baud)
600 tty_encode_baud_rate(port->tty, baud, baud);
601
Thiago Galesi372db8a2006-07-31 15:39:27 -0300602 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Thiago Galesi572d3132006-07-29 10:46:37 -0300605static void pl2303_close(struct usb_serial_port *port, struct file *filp)
606{
607 struct pl2303_private *priv = usb_get_serial_port_data(port);
608 unsigned long flags;
609 unsigned int c_cflag;
610 int bps;
611 long timeout;
612 wait_queue_t wait;
613
614 dbg("%s - port %d", __FUNCTION__, port->number);
615
616 /* wait for data to drain from the buffer */
617 spin_lock_irqsave(&priv->lock, flags);
618 timeout = PL2303_CLOSING_WAIT;
619 init_waitqueue_entry(&wait, current);
620 add_wait_queue(&port->tty->write_wait, &wait);
621 for (;;) {
622 set_current_state(TASK_INTERRUPTIBLE);
623 if (pl2303_buf_data_avail(priv->buf) == 0 ||
624 timeout == 0 || signal_pending(current) ||
625 !usb_get_intfdata(port->serial->interface)) /* disconnect */
626 break;
627 spin_unlock_irqrestore(&priv->lock, flags);
628 timeout = schedule_timeout(timeout);
629 spin_lock_irqsave(&priv->lock, flags);
630 }
631 set_current_state(TASK_RUNNING);
632 remove_wait_queue(&port->tty->write_wait, &wait);
633 /* clear out any remaining data in the buffer */
634 pl2303_buf_clear(priv->buf);
635 spin_unlock_irqrestore(&priv->lock, flags);
636
637 /* wait for characters to drain from the device */
638 /* (this is long enough for the entire 256 byte */
639 /* pl2303 hardware buffer to drain with no flow */
640 /* control for data rates of 1200 bps or more, */
641 /* for lower rates we should really know how much */
642 /* data is in the buffer to compute a delay */
643 /* that is not unnecessarily long) */
644 bps = tty_get_baud_rate(port->tty);
645 if (bps > 1200)
646 timeout = max((HZ*2560)/bps,HZ/10);
647 else
648 timeout = 2*HZ;
649 schedule_timeout_interruptible(timeout);
650
651 /* shutdown our urbs */
652 dbg("%s - shutting down urbs", __FUNCTION__);
653 usb_kill_urb(port->write_urb);
654 usb_kill_urb(port->read_urb);
655 usb_kill_urb(port->interrupt_in_urb);
656
657 if (port->tty) {
658 c_cflag = port->tty->termios->c_cflag;
659 if (c_cflag & HUPCL) {
660 /* drop DTR and RTS */
661 spin_lock_irqsave(&priv->lock, flags);
662 priv->line_control = 0;
663 spin_unlock_irqrestore(&priv->lock, flags);
664 set_control_lines(port->serial->dev, 0);
665 }
666 }
667}
668
Thiago Galesi372db8a2006-07-31 15:39:27 -0300669static int pl2303_open(struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Alan Cox606d0992006-12-08 02:38:45 -0800671 struct ktermios tmp_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct usb_serial *serial = port->serial;
673 struct pl2303_private *priv = usb_get_serial_port_data(port);
674 unsigned char *buf;
675 int result;
676
677 dbg("%s - port %d", __FUNCTION__, port->number);
678
Dariusz M16948992005-07-28 18:06:13 +0200679 if (priv->type != HX) {
680 usb_clear_halt(serial->dev, port->write_urb->pipe);
681 usb_clear_halt(serial->dev, port->read_urb->pipe);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 buf = kmalloc(10, GFP_KERNEL);
685 if (buf==NULL)
686 return -ENOMEM;
687
688#define FISH(a,b,c,d) \
689 result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
690 b, a, c, d, buf, 1, 100); \
691 dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
692
693#define SOUP(a,b,c,d) \
694 result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
695 b, a, c, d, NULL, 0, 100); \
696 dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
697
698 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
699 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
700 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
701 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
702 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
703 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
704 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
705 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
706 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
707 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
708
709 if (priv->type == HX) {
710 /* HX chip */
711 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
712 /* reset upstream data pipes */
713 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
714 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
715 } else {
716 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
717 }
718
719 kfree(buf);
720
721 /* Setup termios */
722 if (port->tty) {
Thiago Galesi372db8a2006-07-31 15:39:27 -0300723 pl2303_set_termios(port, &tmp_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 //FIXME: need to assert RTS and DTR if CRTSCTS off
727
728 dbg("%s - submitting read urb", __FUNCTION__);
729 port->read_urb->dev = serial->dev;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300730 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (result) {
Thiago Galesi372db8a2006-07-31 15:39:27 -0300732 dev_err(&port->dev, "%s - failed submitting read urb,"
733 " error %d\n", __FUNCTION__, result);
734 pl2303_close(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EPROTO;
736 }
737
738 dbg("%s - submitting interrupt urb", __FUNCTION__);
739 port->interrupt_in_urb->dev = serial->dev;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300740 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (result) {
Thiago Galesi372db8a2006-07-31 15:39:27 -0300742 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
743 " error %d\n", __FUNCTION__, result);
744 pl2303_close(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -EPROTO;
746 }
747 return 0;
748}
749
Thiago Galesi372db8a2006-07-31 15:39:27 -0300750static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file,
751 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct pl2303_private *priv = usb_get_serial_port_data(port);
754 unsigned long flags;
755 u8 control;
756
Flavio Leitner6fdd8e82005-04-18 17:39:31 -0700757 if (!usb_get_intfdata(port->serial->interface))
758 return -ENODEV;
759
Thiago Galesi372db8a2006-07-31 15:39:27 -0300760 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (set & TIOCM_RTS)
762 priv->line_control |= CONTROL_RTS;
763 if (set & TIOCM_DTR)
764 priv->line_control |= CONTROL_DTR;
765 if (clear & TIOCM_RTS)
766 priv->line_control &= ~CONTROL_RTS;
767 if (clear & TIOCM_DTR)
768 priv->line_control &= ~CONTROL_DTR;
769 control = priv->line_control;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300770 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Thiago Galesi372db8a2006-07-31 15:39:27 -0300772 return set_control_lines(port->serial->dev, control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Thiago Galesi372db8a2006-07-31 15:39:27 -0300775static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct pl2303_private *priv = usb_get_serial_port_data(port);
778 unsigned long flags;
779 unsigned int mcr;
780 unsigned int status;
781 unsigned int result;
782
783 dbg("%s (%d)", __FUNCTION__, port->number);
784
Flavio Leitner6fdd8e82005-04-18 17:39:31 -0700785 if (!usb_get_intfdata(port->serial->interface))
786 return -ENODEV;
787
Thiago Galesi372db8a2006-07-31 15:39:27 -0300788 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 mcr = priv->line_control;
790 status = priv->line_status;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300791 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
794 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
795 | ((status & UART_CTS) ? TIOCM_CTS : 0)
796 | ((status & UART_DSR) ? TIOCM_DSR : 0)
797 | ((status & UART_RING) ? TIOCM_RI : 0)
798 | ((status & UART_DCD) ? TIOCM_CD : 0);
799
800 dbg("%s - result = %x", __FUNCTION__, result);
801
802 return result;
803}
804
805static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
806{
807 struct pl2303_private *priv = usb_get_serial_port_data(port);
808 unsigned long flags;
809 unsigned int prevstatus;
810 unsigned int status;
811 unsigned int changed;
812
Thiago Galesi372db8a2006-07-31 15:39:27 -0300813 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 prevstatus = priv->line_status;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300815 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 while (1) {
818 interruptible_sleep_on(&priv->delta_msr_wait);
819 /* see if a signal did it */
820 if (signal_pending(current))
821 return -ERESTARTSYS;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300822
823 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 status = priv->line_status;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300825 spin_unlock_irqrestore(&priv->lock, flags);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 changed=prevstatus^status;
Thiago Galesi372db8a2006-07-31 15:39:27 -0300828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
830 ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
831 ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
832 ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) {
833 return 0;
834 }
835 prevstatus = status;
836 }
837 /* NOTREACHED */
838 return 0;
839}
840
Thiago Galesi372db8a2006-07-31 15:39:27 -0300841static int pl2303_ioctl(struct usb_serial_port *port, struct file *file,
842 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
845
846 switch (cmd) {
847 case TIOCMIWAIT:
848 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
849 return wait_modem_info(port, arg);
850
851 default:
852 dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
853 break;
854 }
855
856 return -ENOIOCTLCMD;
857}
858
Thiago Galesi372db8a2006-07-31 15:39:27 -0300859static void pl2303_break_ctl(struct usb_serial_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct usb_serial *serial = port->serial;
862 u16 state;
863 int result;
864
865 dbg("%s - port %d", __FUNCTION__, port->number);
866
867 if (break_state == 0)
868 state = BREAK_OFF;
869 else
870 state = BREAK_ON;
871 dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
872
Thiago Galesi372db8a2006-07-31 15:39:27 -0300873 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
874 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
875 0, NULL, 0, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (result)
877 dbg("%s - error sending break = %d", __FUNCTION__, result);
878}
879
Thiago Galesi372db8a2006-07-31 15:39:27 -0300880static void pl2303_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int i;
883 struct pl2303_private *priv;
884
885 dbg("%s", __FUNCTION__);
886
887 for (i = 0; i < serial->num_ports; ++i) {
888 priv = usb_get_serial_port_data(serial->port[i]);
889 if (priv) {
890 pl2303_buf_free(priv->buf);
891 kfree(priv);
892 usb_set_serial_port_data(serial->port[i], NULL);
893 }
Thiago Galesi372db8a2006-07-31 15:39:27 -0300894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700897static void pl2303_update_line_status(struct usb_serial_port *port,
898 unsigned char *data,
899 unsigned int actual_length)
900{
901
902 struct pl2303_private *priv = usb_get_serial_port_data(port);
903 unsigned long flags;
904 u8 status_idx = UART_STATE;
Horst Schirmeier95f209f2005-07-28 15:32:20 +0200905 u8 length = UART_STATE + 1;
Thiago Galesi9c537612006-07-29 10:47:12 -0300906 u16 idv, idp;
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700907
Thiago Galesi9c537612006-07-29 10:47:12 -0300908 idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
909 idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
910
911
912 if (idv == SIEMENS_VENDOR_ID) {
913 if (idp == SIEMENS_PRODUCT_ID_X65 ||
914 idp == SIEMENS_PRODUCT_ID_SX1 ||
915 idp == SIEMENS_PRODUCT_ID_X75) {
916
917 length = 1;
918 status_idx = 0;
919 }
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700920 }
921
922 if (actual_length < length)
Luiz Fernando N. Capitulinoa009b752006-07-25 16:58:30 -0300923 return;
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700924
925 /* Save off the uart status for others to look at */
926 spin_lock_irqsave(&priv->lock, flags);
927 priv->line_status = data[status_idx];
928 spin_unlock_irqrestore(&priv->lock, flags);
Thiago Galesi372db8a2006-07-31 15:39:27 -0300929 wake_up_interruptible(&priv->delta_msr_wait);
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
David Howells7d12e782006-10-05 14:55:46 +0100932static void pl2303_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 unsigned char *data = urb->transfer_buffer;
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700936 unsigned int actual_length = urb->actual_length;
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700937 int status = urb->status;
938 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 dbg("%s (%d)", __FUNCTION__, port->number);
941
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700942 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 case 0:
944 /* success */
945 break;
946 case -ECONNRESET:
947 case -ENOENT:
948 case -ESHUTDOWN:
949 /* this urb is terminated, clean up */
Thiago Galesi372db8a2006-07-31 15:39:27 -0300950 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700951 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 default:
Thiago Galesi372db8a2006-07-31 15:39:27 -0300954 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700955 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto exit;
957 }
958
Thiago Galesi372db8a2006-07-31 15:39:27 -0300959 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
960 urb->actual_length, urb->transfer_buffer);
961
Flavio Leitner97bb13e2005-04-18 17:39:31 -0700962 pl2303_update_line_status(port, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964exit:
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700965 retval = usb_submit_urb(urb, GFP_ATOMIC);
966 if (retval)
Thiago Galesi372db8a2006-07-31 15:39:27 -0300967 dev_err(&urb->dev->dev,
968 "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700969 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
David Howells7d12e782006-10-05 14:55:46 +0100972static void pl2303_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
975 struct pl2303_private *priv = usb_get_serial_port_data(port);
976 struct tty_struct *tty;
977 unsigned char *data = urb->transfer_buffer;
978 unsigned long flags;
979 int i;
980 int result;
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700981 int status = urb->status;
982 u8 line_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 char tty_flag;
984
985 dbg("%s - port %d", __FUNCTION__, port->number);
986
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700987 if (status) {
988 dbg("%s - urb status = %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!port->open_count) {
990 dbg("%s - port is closed, exiting.", __FUNCTION__);
991 return;
992 }
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -0700993 if (status == -EPROTO) {
Thiago Galesi372db8a2006-07-31 15:39:27 -0300994 /* PL2303 mysteriously fails with -EPROTO reschedule
995 * the read */
996 dbg("%s - caught -EPROTO, resubmitting the urb",
997 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 urb->dev = port->serial->dev;
999 result = usb_submit_urb(urb, GFP_ATOMIC);
1000 if (result)
Thiago Galesi372db8a2006-07-31 15:39:27 -03001001 dev_err(&urb->dev->dev, "%s - failed"
1002 " resubmitting read urb, error %d\n",
1003 __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
1007 return;
1008 }
1009
Thiago Galesi372db8a2006-07-31 15:39:27 -03001010 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1011 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /* get tty_flag from status */
1014 tty_flag = TTY_NORMAL;
1015
1016 spin_lock_irqsave(&priv->lock, flags);
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001017 line_status = priv->line_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1019 spin_unlock_irqrestore(&priv->lock, flags);
Thiago Galesi372db8a2006-07-31 15:39:27 -03001020 wake_up_interruptible(&priv->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* break takes precedence over parity, */
1023 /* which takes precedence over framing errors */
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001024 if (line_status & UART_BREAK_ERROR )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 tty_flag = TTY_BREAK;
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001026 else if (line_status & UART_PARITY_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tty_flag = TTY_PARITY;
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001028 else if (line_status & UART_FRAME_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 tty_flag = TTY_FRAME;
1030 dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
1031
1032 tty = port->tty;
1033 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -08001034 tty_buffer_request_room(tty, urb->actual_length + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* overrun is special, not associated with a char */
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001036 if (line_status & UART_OVERRUN_ERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Alan Cox33f0f882006-01-09 20:54:13 -08001038 for (i = 0; i < urb->actual_length; ++i)
Thiago Galesi372db8a2006-07-31 15:39:27 -03001039 tty_insert_flip_char(tty, data[i], tty_flag);
1040 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 /* Schedule the next read _if_ we are still open */
1044 if (port->open_count) {
1045 urb->dev = port->serial->dev;
1046 result = usb_submit_urb(urb, GFP_ATOMIC);
1047 if (result)
Thiago Galesi372db8a2006-07-31 15:39:27 -03001048 dev_err(&urb->dev->dev, "%s - failed resubmitting"
1049 " read urb, error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
1052 return;
1053}
1054
David Howells7d12e782006-10-05 14:55:46 +01001055static void pl2303_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
1058 struct pl2303_private *priv = usb_get_serial_port_data(port);
1059 int result;
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001060 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 dbg("%s - port %d", __FUNCTION__, port->number);
1063
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001064 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 case 0:
1066 /* success */
1067 break;
1068 case -ECONNRESET:
1069 case -ENOENT:
1070 case -ESHUTDOWN:
1071 /* this urb is terminated, clean up */
Thiago Galesi372db8a2006-07-31 15:39:27 -03001072 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001073 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 priv->write_urb_in_use = 0;
1075 return;
1076 default:
1077 /* error in the urb, so we have to resubmit it */
1078 dbg("%s - Overflow in write", __FUNCTION__);
Thiago Galesi372db8a2006-07-31 15:39:27 -03001079 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
Greg Kroah-Hartman461d6962007-06-15 15:44:13 -07001080 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 port->write_urb->transfer_buffer_length = 1;
1082 port->write_urb->dev = port->serial->dev;
Thiago Galesi372db8a2006-07-31 15:39:27 -03001083 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (result)
Thiago Galesi372db8a2006-07-31 15:39:27 -03001085 dev_err(&urb->dev->dev, "%s - failed resubmitting write"
1086 " urb, error %d\n", __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 else
1088 return;
1089 }
1090
1091 priv->write_urb_in_use = 0;
1092
1093 /* send any buffered data */
1094 pl2303_send(port);
1095}
1096
Thiago Galesi572d3132006-07-29 10:46:37 -03001097/* All of the device info needed for the PL2303 SIO serial converter */
1098static struct usb_serial_driver pl2303_device = {
1099 .driver = {
1100 .owner = THIS_MODULE,
1101 .name = "pl2303",
1102 },
1103 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001104 .usb_driver = &pl2303_driver,
Thiago Galesi572d3132006-07-29 10:46:37 -03001105 .num_interrupt_in = NUM_DONT_CARE,
1106 .num_bulk_in = 1,
1107 .num_bulk_out = 1,
1108 .num_ports = 1,
1109 .open = pl2303_open,
1110 .close = pl2303_close,
1111 .write = pl2303_write,
1112 .ioctl = pl2303_ioctl,
1113 .break_ctl = pl2303_break_ctl,
1114 .set_termios = pl2303_set_termios,
1115 .tiocmget = pl2303_tiocmget,
1116 .tiocmset = pl2303_tiocmset,
1117 .read_bulk_callback = pl2303_read_bulk_callback,
1118 .read_int_callback = pl2303_read_int_callback,
1119 .write_bulk_callback = pl2303_write_bulk_callback,
1120 .write_room = pl2303_write_room,
1121 .chars_in_buffer = pl2303_chars_in_buffer,
1122 .attach = pl2303_startup,
1123 .shutdown = pl2303_shutdown,
1124};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Thiago Galesi372db8a2006-07-31 15:39:27 -03001126static int __init pl2303_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 int retval;
Thiago Galesi372db8a2006-07-31 15:39:27 -03001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 retval = usb_serial_register(&pl2303_device);
1131 if (retval)
1132 goto failed_usb_serial_register;
1133 retval = usb_register(&pl2303_driver);
1134 if (retval)
1135 goto failed_usb_register;
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -07001136 info(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
1138failed_usb_register:
1139 usb_serial_deregister(&pl2303_device);
1140failed_usb_serial_register:
1141 return retval;
1142}
1143
Thiago Galesi372db8a2006-07-31 15:39:27 -03001144static void __exit pl2303_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Thiago Galesi372db8a2006-07-31 15:39:27 -03001146 usb_deregister(&pl2303_driver);
1147 usb_serial_deregister(&pl2303_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150module_init(pl2303_init);
1151module_exit(pl2303_exit);
1152
1153MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154MODULE_LICENSE("GPL");
1155
1156module_param(debug, bool, S_IRUGO | S_IWUSR);
1157MODULE_PARM_DESC(debug, "Debug enabled or not");
1158