blob: 2ea32c561d02ef933ae47658ba7780c52918c80e [file] [log] [blame]
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +01001/*
2 * spcp8x5 USB to serial adaptor driver
3 *
4 * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn)
5 * Copyright (C) 2006 S1 Corp.
6 *
7 * Original driver for 2.6.10 pl2303 driver by
8 * Greg Kroah-Hartman (greg@kroah.com)
9 * Changes for 2.6.20 by Harald Klein <hari@vt100.at>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 *
17 */
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/tty.h>
23#include <linux/tty_driver.h>
24#include <linux/tty_flip.h>
25#include <linux/module.h>
26#include <linux/spinlock.h>
27#include <linux/usb.h>
28#include <linux/usb/serial.h>
29
30
31/* Version Information */
32#define DRIVER_VERSION "v0.04"
33#define DRIVER_DESC "SPCP8x5 USB to serial adaptor driver"
34
35static int debug;
36
37#define SPCP8x5_007_VID 0x04FC
38#define SPCP8x5_007_PID 0x0201
39#define SPCP8x5_008_VID 0x04fc
40#define SPCP8x5_008_PID 0x0235
41#define SPCP8x5_PHILIPS_VID 0x0471
42#define SPCP8x5_PHILIPS_PID 0x081e
43#define SPCP8x5_INTERMATIC_VID 0x04FC
44#define SPCP8x5_INTERMATIC_PID 0x0204
45#define SPCP8x5_835_VID 0x04fc
46#define SPCP8x5_835_PID 0x0231
47
Németh Márton7d40d7e2010-01-10 15:34:24 +010048static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +010049 { USB_DEVICE(SPCP8x5_PHILIPS_VID , SPCP8x5_PHILIPS_PID)},
50 { USB_DEVICE(SPCP8x5_INTERMATIC_VID, SPCP8x5_INTERMATIC_PID)},
51 { USB_DEVICE(SPCP8x5_835_VID, SPCP8x5_835_PID)},
52 { USB_DEVICE(SPCP8x5_008_VID, SPCP8x5_008_PID)},
53 { USB_DEVICE(SPCP8x5_007_VID, SPCP8x5_007_PID)},
54 { } /* Terminating entry */
55};
56MODULE_DEVICE_TABLE(usb, id_table);
57
58struct spcp8x5_usb_ctrl_arg {
59 u8 type;
60 u8 cmd;
61 u8 cmd_type;
62 u16 value;
63 u16 index;
64 u16 length;
65};
66
67/* wait 30s before close */
68#define SPCP8x5_CLOSING_WAIT (30*HZ)
69
70#define SPCP8x5_BUF_SIZE 1024
71
72
73/* spcp8x5 spec register define */
74#define MCR_CONTROL_LINE_RTS 0x02
75#define MCR_CONTROL_LINE_DTR 0x01
76#define MCR_DTR 0x01
77#define MCR_RTS 0x02
78
79#define MSR_STATUS_LINE_DCD 0x80
80#define MSR_STATUS_LINE_RI 0x40
81#define MSR_STATUS_LINE_DSR 0x20
82#define MSR_STATUS_LINE_CTS 0x10
83
84/* verdor command here , we should define myself */
85#define SET_DEFAULT 0x40
86#define SET_DEFAULT_TYPE 0x20
87
88#define SET_UART_FORMAT 0x40
89#define SET_UART_FORMAT_TYPE 0x21
90#define SET_UART_FORMAT_SIZE_5 0x00
91#define SET_UART_FORMAT_SIZE_6 0x01
92#define SET_UART_FORMAT_SIZE_7 0x02
93#define SET_UART_FORMAT_SIZE_8 0x03
94#define SET_UART_FORMAT_STOP_1 0x00
95#define SET_UART_FORMAT_STOP_2 0x04
96#define SET_UART_FORMAT_PAR_NONE 0x00
97#define SET_UART_FORMAT_PAR_ODD 0x10
98#define SET_UART_FORMAT_PAR_EVEN 0x30
99#define SET_UART_FORMAT_PAR_MASK 0xD0
100#define SET_UART_FORMAT_PAR_SPACE 0x90
101
102#define GET_UART_STATUS_TYPE 0xc0
103#define GET_UART_STATUS 0x22
104#define GET_UART_STATUS_MSR 0x06
105
106#define SET_UART_STATUS 0x40
107#define SET_UART_STATUS_TYPE 0x23
108#define SET_UART_STATUS_MCR 0x0004
109#define SET_UART_STATUS_MCR_DTR 0x01
110#define SET_UART_STATUS_MCR_RTS 0x02
111#define SET_UART_STATUS_MCR_LOOP 0x10
112
113#define SET_WORKING_MODE 0x40
114#define SET_WORKING_MODE_TYPE 0x24
115#define SET_WORKING_MODE_U2C 0x00
116#define SET_WORKING_MODE_RS485 0x01
117#define SET_WORKING_MODE_PDMA 0x02
118#define SET_WORKING_MODE_SPP 0x03
119
120#define SET_FLOWCTL_CHAR 0x40
121#define SET_FLOWCTL_CHAR_TYPE 0x25
122
123#define GET_VERSION 0xc0
124#define GET_VERSION_TYPE 0x26
125
126#define SET_REGISTER 0x40
127#define SET_REGISTER_TYPE 0x27
128
129#define GET_REGISTER 0xc0
130#define GET_REGISTER_TYPE 0x28
131
132#define SET_RAM 0x40
133#define SET_RAM_TYPE 0x31
134
135#define GET_RAM 0xc0
136#define GET_RAM_TYPE 0x32
137
138/* how come ??? */
139#define UART_STATE 0x08
140#define UART_STATE_TRANSIENT_MASK 0x74
141#define UART_DCD 0x01
142#define UART_DSR 0x02
143#define UART_BREAK_ERROR 0x04
144#define UART_RING 0x08
145#define UART_FRAME_ERROR 0x10
146#define UART_PARITY_ERROR 0x20
147#define UART_OVERRUN_ERROR 0x40
148#define UART_CTS 0x80
149
150enum spcp8x5_type {
151 SPCP825_007_TYPE,
152 SPCP825_008_TYPE,
153 SPCP825_PHILIP_TYPE,
154 SPCP825_INTERMATIC_TYPE,
155 SPCP835_TYPE,
156};
157
158/* 1st in 1st out buffer 4 driver */
159struct ringbuf {
160 unsigned int buf_size;
161 char *buf_buf;
162 char *buf_get;
163 char *buf_put;
164};
165
166/* alloc the ring buf and alloc the buffer itself */
167static inline struct ringbuf *alloc_ringbuf(unsigned int size)
168{
169 struct ringbuf *pb;
170
171 if (size == 0)
172 return NULL;
173
174 pb = kmalloc(sizeof(*pb), GFP_KERNEL);
175 if (pb == NULL)
176 return NULL;
177
178 pb->buf_buf = kmalloc(size, GFP_KERNEL);
179 if (pb->buf_buf == NULL) {
180 kfree(pb);
181 return NULL;
182 }
183
184 pb->buf_size = size;
185 pb->buf_get = pb->buf_put = pb->buf_buf;
186
187 return pb;
188}
189
190/* free the ring buf and the buffer itself */
191static inline void free_ringbuf(struct ringbuf *pb)
192{
193 if (pb != NULL) {
194 kfree(pb->buf_buf);
195 kfree(pb);
196 }
197}
198
199/* clear pipo , juest repoint the pointer here */
200static inline void clear_ringbuf(struct ringbuf *pb)
201{
202 if (pb != NULL)
203 pb->buf_get = pb->buf_put;
204}
205
206/* get the number of data in the pipo */
207static inline unsigned int ringbuf_avail_data(struct ringbuf *pb)
208{
209 if (pb == NULL)
210 return 0;
Alan Cox0487b582008-07-22 11:15:08 +0100211 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100212}
213
214/* get the number of space in the pipo */
215static inline unsigned int ringbuf_avail_space(struct ringbuf *pb)
216{
217 if (pb == NULL)
218 return 0;
Alan Cox0487b582008-07-22 11:15:08 +0100219 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100220}
221
222/* put count data into pipo */
223static unsigned int put_ringbuf(struct ringbuf *pb, const char *buf,
224 unsigned int count)
225{
226 unsigned int len;
227
228 if (pb == NULL)
229 return 0;
230
231 len = ringbuf_avail_space(pb);
232 if (count > len)
233 count = len;
234
235 if (count == 0)
236 return 0;
237
238 len = pb->buf_buf + pb->buf_size - pb->buf_put;
239 if (count > len) {
240 memcpy(pb->buf_put, buf, len);
241 memcpy(pb->buf_buf, buf+len, count - len);
242 pb->buf_put = pb->buf_buf + count - len;
243 } else {
244 memcpy(pb->buf_put, buf, count);
245 if (count < len)
246 pb->buf_put += count;
247 else /* count == len */
248 pb->buf_put = pb->buf_buf;
249 }
250 return count;
251}
252
253/* get count data from pipo */
254static unsigned int get_ringbuf(struct ringbuf *pb, char *buf,
255 unsigned int count)
256{
257 unsigned int len;
258
259 if (pb == NULL || buf == NULL)
260 return 0;
261
262 len = ringbuf_avail_data(pb);
263 if (count > len)
264 count = len;
265
266 if (count == 0)
267 return 0;
268
269 len = pb->buf_buf + pb->buf_size - pb->buf_get;
270 if (count > len) {
271 memcpy(buf, pb->buf_get, len);
272 memcpy(buf+len, pb->buf_buf, count - len);
273 pb->buf_get = pb->buf_buf + count - len;
274 } else {
275 memcpy(buf, pb->buf_get, count);
276 if (count < len)
277 pb->buf_get += count;
278 else /* count == len */
279 pb->buf_get = pb->buf_buf;
280 }
281
282 return count;
283}
284
285static struct usb_driver spcp8x5_driver = {
286 .name = "spcp8x5",
287 .probe = usb_serial_probe,
288 .disconnect = usb_serial_disconnect,
289 .id_table = id_table,
290 .no_dynamic_id = 1,
291};
292
293
294struct spcp8x5_private {
295 spinlock_t lock;
296 struct ringbuf *buf;
297 int write_urb_in_use;
298 enum spcp8x5_type type;
299 wait_queue_head_t delta_msr_wait;
300 u8 line_control;
301 u8 line_status;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100302};
303
304/* desc : when device plug in,this function would be called.
305 * thanks to usb_serial subsystem,then do almost every things for us. And what
306 * we should do just alloc the buffer */
307static int spcp8x5_startup(struct usb_serial *serial)
308{
309 struct spcp8x5_private *priv;
310 int i;
311 enum spcp8x5_type type = SPCP825_007_TYPE;
Al Virofd05e722008-04-28 07:00:16 +0100312 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100313
Al Virofd05e722008-04-28 07:00:16 +0100314 if (product == 0x0201)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100315 type = SPCP825_007_TYPE;
Al Virofd05e722008-04-28 07:00:16 +0100316 else if (product == 0x0231)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100317 type = SPCP835_TYPE;
Al Virofd05e722008-04-28 07:00:16 +0100318 else if (product == 0x0235)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100319 type = SPCP825_008_TYPE;
Al Virofd05e722008-04-28 07:00:16 +0100320 else if (product == 0x0204)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100321 type = SPCP825_INTERMATIC_TYPE;
Al Virofd05e722008-04-28 07:00:16 +0100322 else if (product == 0x0471 &&
323 serial->dev->descriptor.idVendor == cpu_to_le16(0x081e))
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100324 type = SPCP825_PHILIP_TYPE;
325 dev_dbg(&serial->dev->dev, "device type = %d\n", (int)type);
326
327 for (i = 0; i < serial->num_ports; ++i) {
328 priv = kzalloc(sizeof(struct spcp8x5_private), GFP_KERNEL);
329 if (!priv)
330 goto cleanup;
331
332 spin_lock_init(&priv->lock);
333 priv->buf = alloc_ringbuf(SPCP8x5_BUF_SIZE);
334 if (priv->buf == NULL)
335 goto cleanup2;
336
337 init_waitqueue_head(&priv->delta_msr_wait);
338 priv->type = type;
339 usb_set_serial_port_data(serial->port[i] , priv);
340
341 }
342
343 return 0;
344
345cleanup2:
346 kfree(priv);
347cleanup:
348 for (--i; i >= 0; --i) {
349 priv = usb_get_serial_port_data(serial->port[i]);
350 free_ringbuf(priv->buf);
351 kfree(priv);
352 usb_set_serial_port_data(serial->port[i] , NULL);
353 }
354 return -ENOMEM;
355}
356
357/* call when the device plug out. free all the memory alloced by probe */
Alan Sternf9c99bb2009-06-02 11:53:55 -0400358static void spcp8x5_release(struct usb_serial *serial)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100359{
360 int i;
361 struct spcp8x5_private *priv;
362
363 for (i = 0; i < serial->num_ports; i++) {
364 priv = usb_get_serial_port_data(serial->port[i]);
365 if (priv) {
366 free_ringbuf(priv->buf);
367 kfree(priv);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100368 }
369 }
370}
371
372/* set the modem control line of the device.
373 * NOTE spcp825-007 not supported this */
374static int spcp8x5_set_ctrlLine(struct usb_device *dev, u8 value,
375 enum spcp8x5_type type)
376{
377 int retval;
378 u8 mcr = 0 ;
379
380 if (type == SPCP825_007_TYPE)
381 return -EPERM;
382
383 mcr = (unsigned short)value;
384 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
385 SET_UART_STATUS_TYPE, SET_UART_STATUS,
386 mcr, 0x04, NULL, 0, 100);
387 if (retval != 0)
388 dev_dbg(&dev->dev, "usb_control_msg return %#x\n", retval);
389 return retval;
390}
391
392/* get the modem status register of the device
393 * NOTE spcp825-007 not supported this */
394static int spcp8x5_get_msr(struct usb_device *dev, u8 *status,
395 enum spcp8x5_type type)
396{
397 u8 *status_buffer;
398 int ret;
399
400 /* I return Permited not support here but seem inval device
401 * is more fix */
402 if (type == SPCP825_007_TYPE)
403 return -EPERM;
404 if (status == NULL)
405 return -EINVAL;
406
407 status_buffer = kmalloc(1, GFP_KERNEL);
408 if (!status_buffer)
409 return -ENOMEM;
410 status_buffer[0] = status[0];
411
412 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
413 GET_UART_STATUS, GET_UART_STATUS_TYPE,
414 0, GET_UART_STATUS_MSR, status_buffer, 1, 100);
415 if (ret < 0)
416 dev_dbg(&dev->dev, "Get MSR = 0x%p failed (error = %d)",
417 status_buffer, ret);
418
419 dev_dbg(&dev->dev, "0xc0:0x22:0:6 %d - 0x%p ", ret, status_buffer);
420 status[0] = status_buffer[0];
421 kfree(status_buffer);
422
423 return ret;
424}
425
426/* select the work mode.
427 * NOTE this function not supported by spcp825-007 */
428static void spcp8x5_set_workMode(struct usb_device *dev, u16 value,
429 u16 index, enum spcp8x5_type type)
430{
431 int ret;
432
433 /* I return Permited not support here but seem inval device
434 * is more fix */
435 if (type == SPCP825_007_TYPE)
436 return;
437
438 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
439 SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
440 value, index, NULL, 0, 100);
441 dev_dbg(&dev->dev, "value = %#x , index = %#x\n", value, index);
442 if (ret < 0)
443 dev_dbg(&dev->dev,
444 "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret);
445}
446
Alan Cox335f8512009-06-11 12:26:29 +0100447static int spcp8x5_carrier_raised(struct usb_serial_port *port)
448{
449 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
450 if (priv->line_status & MSR_STATUS_LINE_DCD)
451 return 1;
452 return 0;
453}
454
455static void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100456{
457 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
458 unsigned long flags;
Alan Cox335f8512009-06-11 12:26:29 +0100459 u8 control;
460
461 spin_lock_irqsave(&priv->lock, flags);
462 if (on)
463 priv->line_control = MCR_CONTROL_LINE_DTR
464 | MCR_CONTROL_LINE_RTS;
465 else
466 priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
467 | MCR_CONTROL_LINE_RTS);
468 control = priv->line_control;
469 spin_unlock_irqrestore(&priv->lock, flags);
470 spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
471}
472
473/* close the serial port. We should wait for data sending to device 1st and
474 * then kill all urb. */
475static void spcp8x5_close(struct usb_serial_port *port)
476{
477 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
478 unsigned long flags;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100479 int result;
480
481 dbg("%s - port %d", __func__, port->number);
482
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100483 spin_lock_irqsave(&priv->lock, flags);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100484 /* clear out any remaining data in the buffer */
485 clear_ringbuf(priv->buf);
486 spin_unlock_irqrestore(&priv->lock, flags);
487
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100488 /* kill urb */
489 if (port->write_urb != NULL) {
490 result = usb_unlink_urb(port->write_urb);
491 if (result)
492 dev_dbg(&port->dev,
493 "usb_unlink_urb(write_urb) = %d\n", result);
494 }
495 result = usb_unlink_urb(port->read_urb);
496 if (result)
497 dev_dbg(&port->dev, "usb_unlink_urb(read_urb) = %d\n", result);
498}
499
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700500static void spcp8x5_init_termios(struct tty_struct *tty)
501{
502 /* for the 1st time call this function */
503 *(tty->termios) = tty_std_termios;
504 tty->termios->c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
505 tty->termios->c_ispeed = 115200;
506 tty->termios->c_ospeed = 115200;
507}
508
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100509/* set the serial param for transfer. we should check if we really need to
Alan Cox95da3102008-07-22 11:09:07 +0100510 * transfer. if we set flow control we should do this too. */
511static void spcp8x5_set_termios(struct tty_struct *tty,
512 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100513{
514 struct usb_serial *serial = port->serial;
515 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
516 unsigned long flags;
Alan Cox95da3102008-07-22 11:09:07 +0100517 unsigned int cflag = tty->termios->c_cflag;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100518 unsigned int old_cflag = old_termios->c_cflag;
519 unsigned short uartdata;
520 unsigned char buf[2] = {0, 0};
521 int baud;
522 int i;
523 u8 control;
524
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100525
526 /* check that they really want us to change something */
Alan Cox95da3102008-07-22 11:09:07 +0100527 if (!tty_termios_hw_change(tty->termios, old_termios))
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100528 return;
529
530 /* set DTR/RTS active */
531 spin_lock_irqsave(&priv->lock, flags);
532 control = priv->line_control;
533 if ((old_cflag & CBAUD) == B0) {
534 priv->line_control |= MCR_DTR;
535 if (!(old_cflag & CRTSCTS))
536 priv->line_control |= MCR_RTS;
537 }
538 if (control != priv->line_control) {
539 control = priv->line_control;
540 spin_unlock_irqrestore(&priv->lock, flags);
541 spcp8x5_set_ctrlLine(serial->dev, control , priv->type);
542 } else {
543 spin_unlock_irqrestore(&priv->lock, flags);
544 }
545
546 /* Set Baud Rate */
Joe Perchesa419aef2009-08-18 11:18:35 -0700547 baud = tty_get_baud_rate(tty);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100548 switch (baud) {
549 case 300: buf[0] = 0x00; break;
550 case 600: buf[0] = 0x01; break;
551 case 1200: buf[0] = 0x02; break;
552 case 2400: buf[0] = 0x03; break;
553 case 4800: buf[0] = 0x04; break;
554 case 9600: buf[0] = 0x05; break;
555 case 19200: buf[0] = 0x07; break;
556 case 38400: buf[0] = 0x09; break;
557 case 57600: buf[0] = 0x0a; break;
558 case 115200: buf[0] = 0x0b; break;
559 case 230400: buf[0] = 0x0c; break;
560 case 460800: buf[0] = 0x0d; break;
561 case 921600: buf[0] = 0x0e; break;
562/* case 1200000: buf[0] = 0x0f; break; */
563/* case 2400000: buf[0] = 0x10; break; */
564 case 3000000: buf[0] = 0x11; break;
565/* case 6000000: buf[0] = 0x12; break; */
566 case 0:
567 case 1000000:
568 buf[0] = 0x0b; break;
569 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700570 dev_err(&port->dev, "spcp825 driver does not support the "
571 "baudrate requested, using default of 9600.\n");
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100572 }
573
574 /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
575 if (cflag & CSIZE) {
576 switch (cflag & CSIZE) {
577 case CS5:
578 buf[1] |= SET_UART_FORMAT_SIZE_5;
579 break;
580 case CS6:
581 buf[1] |= SET_UART_FORMAT_SIZE_6;
582 break;
583 case CS7:
584 buf[1] |= SET_UART_FORMAT_SIZE_7;
585 break;
586 default:
587 case CS8:
588 buf[1] |= SET_UART_FORMAT_SIZE_8;
589 break;
590 }
591 }
592
593 /* Set Stop bit2 : 0:1bit 1:2bit */
594 buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
595 SET_UART_FORMAT_STOP_1;
596
597 /* Set Parity bit3-4 01:Odd 11:Even */
598 if (cflag & PARENB) {
599 buf[1] |= (cflag & PARODD) ?
600 SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
601 } else
602 buf[1] |= SET_UART_FORMAT_PAR_NONE;
603
604 uartdata = buf[0] | buf[1]<<8;
605
606 i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
607 SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
608 uartdata, 0, NULL, 0, 100);
609 if (i < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700610 dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
611 uartdata, i);
Joe Perches759f3632010-02-05 16:50:08 -0800612 dbg("0x21:0x40:0:0 %d", i);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100613
614 if (cflag & CRTSCTS) {
615 /* enable hardware flow control */
616 spcp8x5_set_workMode(serial->dev, 0x000a,
617 SET_WORKING_MODE_U2C, priv->type);
618 }
619 return;
620}
621
622/* open the serial port. do some usb system call. set termios and get the line
623 * status of the device. then submit the read urb */
Alan Coxa509a7e2009-09-19 13:13:26 -0700624static int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100625{
626 struct ktermios tmp_termios;
627 struct usb_serial *serial = port->serial;
628 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
629 int ret;
630 unsigned long flags;
631 u8 status = 0x30;
632 /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */
633
634 dbg("%s - port %d", __func__, port->number);
635
636 usb_clear_halt(serial->dev, port->write_urb->pipe);
637 usb_clear_halt(serial->dev, port->read_urb->pipe);
638
639 ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
640 0x09, 0x00,
641 0x01, 0x00, NULL, 0x00, 100);
642 if (ret)
643 return ret;
644
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100645 spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type);
646
647 /* Setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100648 if (tty)
649 spcp8x5_set_termios(tty, port, &tmp_termios);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100650
651 spcp8x5_get_msr(serial->dev, &status, priv->type);
652
653 /* may be we should update uart status here but now we did not do */
654 spin_lock_irqsave(&priv->lock, flags);
655 priv->line_status = status & 0xf0 ;
656 spin_unlock_irqrestore(&priv->lock, flags);
657
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100658 dbg("%s - submitting read urb", __func__);
659 port->read_urb->dev = serial->dev;
660 ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
661 if (ret) {
Alan Cox335f8512009-06-11 12:26:29 +0100662 spcp8x5_close(port);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100663 return -EPROTO;
664 }
Alan Cox335f8512009-06-11 12:26:29 +0100665 port->port.drain_delay = 256;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100666 return 0;
667}
668
669/* bulk read call back function. check the status of the urb. if transfer
670 * failed return. then update the status and the tty send data to tty subsys.
671 * submit urb again.
672 */
673static void spcp8x5_read_bulk_callback(struct urb *urb)
674{
675 struct usb_serial_port *port = urb->context;
676 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
677 struct tty_struct *tty;
678 unsigned char *data = urb->transfer_buffer;
679 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800680 int result = urb->status;
681 u8 status;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100682 char tty_flag;
683
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800684 dev_dbg(&port->dev, "start, result = %d, urb->actual_length = %d\n,",
685 result, urb->actual_length);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100686
687 /* check the urb status */
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800688 if (result) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800689 if (result == -EPROTO) {
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100690 /* spcp8x5 mysteriously fails with -EPROTO */
691 /* reschedule the read */
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100692 urb->dev = port->serial->dev;
693 result = usb_submit_urb(urb , GFP_ATOMIC);
694 if (result)
695 dev_dbg(&port->dev,
696 "failed submitting read urb %d\n",
697 result);
698 return;
699 }
700 dev_dbg(&port->dev, "unable to handle the error, exiting.\n");
701 return;
702 }
703
704 /* get tty_flag from status */
705 tty_flag = TTY_NORMAL;
706
707 spin_lock_irqsave(&priv->lock, flags);
708 status = priv->line_status;
709 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
710 spin_unlock_irqrestore(&priv->lock, flags);
711 /* wake up the wait for termios */
712 wake_up_interruptible(&priv->delta_msr_wait);
713
714 /* break takes precedence over parity, which takes precedence over
715 * framing errors */
716 if (status & UART_BREAK_ERROR)
717 tty_flag = TTY_BREAK;
718 else if (status & UART_PARITY_ERROR)
719 tty_flag = TTY_PARITY;
720 else if (status & UART_FRAME_ERROR)
721 tty_flag = TTY_FRAME;
722 dev_dbg(&port->dev, "tty_flag = %d\n", tty_flag);
723
Alan Cox4a90f092008-10-13 10:39:46 +0100724 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100725 if (tty && urb->actual_length) {
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100726 /* overrun is special, not associated with a char */
727 if (status & UART_OVERRUN_ERROR)
728 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
Johan Hovold70ced222010-05-07 19:46:56 +0200729 tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
730 urb->actual_length);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100731 tty_flip_buffer_push(tty);
732 }
Alan Cox4a90f092008-10-13 10:39:46 +0100733 tty_kref_put(tty);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100734
Alan Stern1f871582010-02-17 10:05:47 -0500735 /* Schedule the next read */
736 urb->dev = port->serial->dev;
737 result = usb_submit_urb(urb , GFP_ATOMIC);
738 if (result)
739 dev_dbg(&port->dev, "failed submitting read urb %d\n", result);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100740}
741
742/* get data from ring buffer and then write to usb bus */
743static void spcp8x5_send(struct usb_serial_port *port)
744{
745 int count, result;
746 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
747 unsigned long flags;
748
749 spin_lock_irqsave(&priv->lock, flags);
750
751 if (priv->write_urb_in_use) {
752 dev_dbg(&port->dev, "write urb still used\n");
753 spin_unlock_irqrestore(&priv->lock, flags);
754 return;
755 }
756
757 /* send the 1st urb for writting */
758 memset(port->write_urb->transfer_buffer , 0x00 , port->bulk_out_size);
759 count = get_ringbuf(priv->buf, port->write_urb->transfer_buffer,
760 port->bulk_out_size);
761
762 if (count == 0) {
763 spin_unlock_irqrestore(&priv->lock, flags);
764 return;
765 }
766
767 /* update the urb status */
768 priv->write_urb_in_use = 1;
769
770 spin_unlock_irqrestore(&priv->lock, flags);
771
772 port->write_urb->transfer_buffer_length = count;
773 port->write_urb->dev = port->serial->dev;
774
775 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
776 if (result) {
777 dev_dbg(&port->dev, "failed submitting write urb, error %d\n",
778 result);
779 priv->write_urb_in_use = 0;
780 /* TODO: reschedule spcp8x5_send */
781 }
782
783
784 schedule_work(&port->work);
785}
786
787/* this is the call back function for write urb. NOTE we should not sleep in
788 * this routine. check the urb return code and then submit the write urb again
789 * to hold the write loop */
790static void spcp8x5_write_bulk_callback(struct urb *urb)
791{
792 struct usb_serial_port *port = urb->context;
793 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
794 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800795 int status = urb->status;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100796
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800797 switch (status) {
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100798 case 0:
799 /* success */
800 break;
801 case -ECONNRESET:
802 case -ENOENT:
803 case -ESHUTDOWN:
804 /* this urb is terminated, clean up */
805 dev_dbg(&port->dev, "urb shutting down with status: %d\n",
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800806 status);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100807 priv->write_urb_in_use = 0;
808 return;
809 default:
810 /* error in the urb, so we have to resubmit it */
811 dbg("%s - Overflow in write", __func__);
812 dbg("%s - nonzero write bulk status received: %d",
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800813 __func__, status);
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100814 port->write_urb->transfer_buffer_length = 1;
815 port->write_urb->dev = port->serial->dev;
816 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
817 if (result)
818 dev_dbg(&port->dev,
819 "failed resubmitting write urb %d\n", result);
820 else
821 return;
822 }
823
824 priv->write_urb_in_use = 0;
825
826 /* send any buffered data */
827 spcp8x5_send(port);
828}
829
830/* write data to ring buffer. and then start the write transfer */
Alan Cox95da3102008-07-22 11:09:07 +0100831static int spcp8x5_write(struct tty_struct *tty, struct usb_serial_port *port,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100832 const unsigned char *buf, int count)
833{
834 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
835 unsigned long flags;
836
837 dev_dbg(&port->dev, "%d bytes\n", count);
838
839 if (!count)
840 return count;
841
842 spin_lock_irqsave(&priv->lock, flags);
843 count = put_ringbuf(priv->buf, buf, count);
844 spin_unlock_irqrestore(&priv->lock, flags);
845
846 spcp8x5_send(port);
847
848 return count;
849}
850
851static int spcp8x5_wait_modem_info(struct usb_serial_port *port,
852 unsigned int arg)
853{
854 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
855 unsigned long flags;
856 unsigned int prevstatus;
857 unsigned int status;
858 unsigned int changed;
859
860 spin_lock_irqsave(&priv->lock, flags);
861 prevstatus = priv->line_status;
862 spin_unlock_irqrestore(&priv->lock, flags);
863
864 while (1) {
865 /* wake up in bulk read */
866 interruptible_sleep_on(&priv->delta_msr_wait);
867
868 /* see if a signal did it */
869 if (signal_pending(current))
870 return -ERESTARTSYS;
871
872 spin_lock_irqsave(&priv->lock, flags);
873 status = priv->line_status;
874 spin_unlock_irqrestore(&priv->lock, flags);
875
876 changed = prevstatus^status;
877
878 if (((arg & TIOCM_RNG) && (changed & MSR_STATUS_LINE_RI)) ||
879 ((arg & TIOCM_DSR) && (changed & MSR_STATUS_LINE_DSR)) ||
880 ((arg & TIOCM_CD) && (changed & MSR_STATUS_LINE_DCD)) ||
881 ((arg & TIOCM_CTS) && (changed & MSR_STATUS_LINE_CTS)))
882 return 0;
883
884 prevstatus = status;
885 }
886 /* NOTREACHED */
887 return 0;
888}
889
Alan Cox95da3102008-07-22 11:09:07 +0100890static int spcp8x5_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100891 unsigned int cmd, unsigned long arg)
892{
Alan Cox95da3102008-07-22 11:09:07 +0100893 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100894 dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
895
896 switch (cmd) {
897 case TIOCMIWAIT:
898 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
899 return spcp8x5_wait_modem_info(port, arg);
900
901 default:
902 dbg("%s not supported = 0x%04x", __func__, cmd);
903 break;
904 }
905
906 return -ENOIOCTLCMD;
907}
908
Alan Cox95da3102008-07-22 11:09:07 +0100909static int spcp8x5_tiocmset(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100910 unsigned int set, unsigned int clear)
911{
Alan Cox95da3102008-07-22 11:09:07 +0100912 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100913 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
914 unsigned long flags;
915 u8 control;
916
917 spin_lock_irqsave(&priv->lock, flags);
918 if (set & TIOCM_RTS)
919 priv->line_control |= MCR_RTS;
920 if (set & TIOCM_DTR)
921 priv->line_control |= MCR_DTR;
922 if (clear & TIOCM_RTS)
923 priv->line_control &= ~MCR_RTS;
924 if (clear & TIOCM_DTR)
925 priv->line_control &= ~MCR_DTR;
926 control = priv->line_control;
927 spin_unlock_irqrestore(&priv->lock, flags);
928
929 return spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
930}
931
Alan Cox95da3102008-07-22 11:09:07 +0100932static int spcp8x5_tiocmget(struct tty_struct *tty, struct file *file)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100933{
Alan Cox95da3102008-07-22 11:09:07 +0100934 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100935 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
936 unsigned long flags;
937 unsigned int mcr;
938 unsigned int status;
939 unsigned int result;
940
941 spin_lock_irqsave(&priv->lock, flags);
942 mcr = priv->line_control;
943 status = priv->line_status;
944 spin_unlock_irqrestore(&priv->lock, flags);
945
946 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
947 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
948 | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
949 | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
950 | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
951 | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
952
953 return result;
954}
955
956/* get the avail space room in ring buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100957static int spcp8x5_write_room(struct tty_struct *tty)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100958{
Alan Cox95da3102008-07-22 11:09:07 +0100959 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100960 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
961 int room = 0;
962 unsigned long flags;
963
964 spin_lock_irqsave(&priv->lock, flags);
965 room = ringbuf_avail_space(priv->buf);
966 spin_unlock_irqrestore(&priv->lock, flags);
967
968 return room;
969}
970
971/* get the number of avail data in write ring buffer */
Alan Cox95da3102008-07-22 11:09:07 +0100972static int spcp8x5_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100973{
Alan Cox95da3102008-07-22 11:09:07 +0100974 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100975 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
976 int chars = 0;
977 unsigned long flags;
978
979 spin_lock_irqsave(&priv->lock, flags);
980 chars = ringbuf_avail_data(priv->buf);
981 spin_unlock_irqrestore(&priv->lock, flags);
982
983 return chars;
984}
985
986/* All of the device info needed for the spcp8x5 SIO serial converter */
987static struct usb_serial_driver spcp8x5_device = {
988 .driver = {
989 .owner = THIS_MODULE,
990 .name = "SPCP8x5",
991 },
992 .id_table = id_table,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100993 .num_ports = 1,
994 .open = spcp8x5_open,
995 .close = spcp8x5_close,
Alan Cox335f8512009-06-11 12:26:29 +0100996 .dtr_rts = spcp8x5_dtr_rts,
997 .carrier_raised = spcp8x5_carrier_raised,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +0100998 .write = spcp8x5_write,
999 .set_termios = spcp8x5_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001000 .init_termios = spcp8x5_init_termios,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +01001001 .ioctl = spcp8x5_ioctl,
1002 .tiocmget = spcp8x5_tiocmget,
1003 .tiocmset = spcp8x5_tiocmset,
1004 .write_room = spcp8x5_write_room,
1005 .read_bulk_callback = spcp8x5_read_bulk_callback,
1006 .write_bulk_callback = spcp8x5_write_bulk_callback,
1007 .chars_in_buffer = spcp8x5_chars_in_buffer,
1008 .attach = spcp8x5_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001009 .release = spcp8x5_release,
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +01001010};
1011
1012static int __init spcp8x5_init(void)
1013{
1014 int retval;
1015 retval = usb_serial_register(&spcp8x5_device);
1016 if (retval)
1017 goto failed_usb_serial_register;
1018 retval = usb_register(&spcp8x5_driver);
1019 if (retval)
1020 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001021 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1022 DRIVER_DESC "\n");
Greg Kroah-Hartman619a6f12008-02-07 23:59:03 +01001023 return 0;
1024failed_usb_register:
1025 usb_serial_deregister(&spcp8x5_device);
1026failed_usb_serial_register:
1027 return retval;
1028}
1029
1030static void __exit spcp8x5_exit(void)
1031{
1032 usb_deregister(&spcp8x5_driver);
1033 usb_serial_deregister(&spcp8x5_device);
1034}
1035
1036module_init(spcp8x5_init);
1037module_exit(spcp8x5_exit);
1038
1039MODULE_DESCRIPTION(DRIVER_DESC);
1040MODULE_VERSION(DRIVER_VERSION);
1041MODULE_LICENSE("GPL");
1042
1043module_param(debug, bool, S_IRUGO | S_IWUSR);
1044MODULE_PARM_DESC(debug, "Debug enabled or not");