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