2 * spcp8x5 USB to serial adaptor driver
4 * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn)
5 * Copyright (C) 2006 S1 Corp.
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>
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.
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>
31 /* Version Information */
32 #define DRIVER_VERSION "v0.04"
33 #define DRIVER_DESC "SPCP8x5 USB to serial adaptor driver"
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
48 static struct usb_device_id id_table
[] = {
49 { 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 */
56 MODULE_DEVICE_TABLE(usb
, id_table
);
58 struct spcp8x5_usb_ctrl_arg
{
67 /* wait 30s before close */
68 #define SPCP8x5_CLOSING_WAIT (30*HZ)
70 #define SPCP8x5_BUF_SIZE 1024
73 /* spcp8x5 spec register define */
74 #define MCR_CONTROL_LINE_RTS 0x02
75 #define MCR_CONTROL_LINE_DTR 0x01
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
84 /* verdor command here , we should define myself */
85 #define SET_DEFAULT 0x40
86 #define SET_DEFAULT_TYPE 0x20
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
102 #define GET_UART_STATUS_TYPE 0xc0
103 #define GET_UART_STATUS 0x22
104 #define GET_UART_STATUS_MSR 0x06
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
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
120 #define SET_FLOWCTL_CHAR 0x40
121 #define SET_FLOWCTL_CHAR_TYPE 0x25
123 #define GET_VERSION 0xc0
124 #define GET_VERSION_TYPE 0x26
126 #define SET_REGISTER 0x40
127 #define SET_REGISTER_TYPE 0x27
129 #define GET_REGISTER 0xc0
130 #define GET_REGISTER_TYPE 0x28
133 #define SET_RAM_TYPE 0x31
136 #define GET_RAM_TYPE 0x32
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
154 SPCP825_INTERMATIC_TYPE
,
158 /* 1st in 1st out buffer 4 driver */
160 unsigned int buf_size
;
166 /* alloc the ring buf and alloc the buffer itself */
167 static inline struct ringbuf
*alloc_ringbuf(unsigned int size
)
174 pb
= kmalloc(sizeof(*pb
), GFP_KERNEL
);
178 pb
->buf_buf
= kmalloc(size
, GFP_KERNEL
);
179 if (pb
->buf_buf
== NULL
) {
185 pb
->buf_get
= pb
->buf_put
= pb
->buf_buf
;
190 /* free the ring buf and the buffer itself */
191 static inline void free_ringbuf(struct ringbuf
*pb
)
199 /* clear pipo , juest repoint the pointer here */
200 static inline void clear_ringbuf(struct ringbuf
*pb
)
203 pb
->buf_get
= pb
->buf_put
;
206 /* get the number of data in the pipo */
207 static inline unsigned int ringbuf_avail_data(struct ringbuf
*pb
)
211 return ((pb
->buf_size
+ pb
->buf_put
- pb
->buf_get
) % pb
->buf_size
);
214 /* get the number of space in the pipo */
215 static inline unsigned int ringbuf_avail_space(struct ringbuf
*pb
)
219 return ((pb
->buf_size
+ pb
->buf_get
- pb
->buf_put
- 1) % pb
->buf_size
);
222 /* put count data into pipo */
223 static unsigned int put_ringbuf(struct ringbuf
*pb
, const char *buf
,
231 len
= ringbuf_avail_space(pb
);
238 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_put
;
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
;
244 memcpy(pb
->buf_put
, buf
, count
);
246 pb
->buf_put
+= count
;
247 else /* count == len */
248 pb
->buf_put
= pb
->buf_buf
;
253 /* get count data from pipo */
254 static unsigned int get_ringbuf(struct ringbuf
*pb
, char *buf
,
259 if (pb
== NULL
|| buf
== NULL
)
262 len
= ringbuf_avail_data(pb
);
269 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_get
;
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
;
275 memcpy(buf
, pb
->buf_get
, count
);
277 pb
->buf_get
+= count
;
278 else /* count == len */
279 pb
->buf_get
= pb
->buf_buf
;
285 static struct usb_driver spcp8x5_driver
= {
287 .probe
= usb_serial_probe
,
288 .disconnect
= usb_serial_disconnect
,
289 .id_table
= id_table
,
294 struct spcp8x5_private
{
297 int write_urb_in_use
;
298 enum spcp8x5_type type
;
299 wait_queue_head_t delta_msr_wait
;
302 u8 termios_initialized
;
305 /* desc : when device plug in,this function would be called.
306 * thanks to usb_serial subsystem,then do almost every things for us. And what
307 * we should do just alloc the buffer */
308 static int spcp8x5_startup(struct usb_serial
*serial
)
310 struct spcp8x5_private
*priv
;
312 enum spcp8x5_type type
= SPCP825_007_TYPE
;
313 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
315 if (product
== 0x0201)
316 type
= SPCP825_007_TYPE
;
317 else if (product
== 0x0231)
319 else if (product
== 0x0235)
320 type
= SPCP825_008_TYPE
;
321 else if (product
== 0x0204)
322 type
= SPCP825_INTERMATIC_TYPE
;
323 else if (product
== 0x0471 &&
324 serial
->dev
->descriptor
.idVendor
== cpu_to_le16(0x081e))
325 type
= SPCP825_PHILIP_TYPE
;
326 dev_dbg(&serial
->dev
->dev
, "device type = %d\n", (int)type
);
328 for (i
= 0; i
< serial
->num_ports
; ++i
) {
329 priv
= kzalloc(sizeof(struct spcp8x5_private
), GFP_KERNEL
);
333 spin_lock_init(&priv
->lock
);
334 priv
->buf
= alloc_ringbuf(SPCP8x5_BUF_SIZE
);
335 if (priv
->buf
== NULL
)
338 init_waitqueue_head(&priv
->delta_msr_wait
);
340 usb_set_serial_port_data(serial
->port
[i
] , priv
);
349 for (--i
; i
>= 0; --i
) {
350 priv
= usb_get_serial_port_data(serial
->port
[i
]);
351 free_ringbuf(priv
->buf
);
353 usb_set_serial_port_data(serial
->port
[i
] , NULL
);
358 /* call when the device plug out. free all the memory alloced by probe */
359 static void spcp8x5_shutdown(struct usb_serial
*serial
)
362 struct spcp8x5_private
*priv
;
364 for (i
= 0; i
< serial
->num_ports
; i
++) {
365 priv
= usb_get_serial_port_data(serial
->port
[i
]);
367 free_ringbuf(priv
->buf
);
369 usb_set_serial_port_data(serial
->port
[i
] , NULL
);
374 /* set the modem control line of the device.
375 * NOTE spcp825-007 not supported this */
376 static int spcp8x5_set_ctrlLine(struct usb_device
*dev
, u8 value
,
377 enum spcp8x5_type type
)
382 if (type
== SPCP825_007_TYPE
)
385 mcr
= (unsigned short)value
;
386 retval
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
387 SET_UART_STATUS_TYPE
, SET_UART_STATUS
,
388 mcr
, 0x04, NULL
, 0, 100);
390 dev_dbg(&dev
->dev
, "usb_control_msg return %#x\n", retval
);
394 /* get the modem status register of the device
395 * NOTE spcp825-007 not supported this */
396 static int spcp8x5_get_msr(struct usb_device
*dev
, u8
*status
,
397 enum spcp8x5_type type
)
402 /* I return Permited not support here but seem inval device
404 if (type
== SPCP825_007_TYPE
)
409 status_buffer
= kmalloc(1, GFP_KERNEL
);
412 status_buffer
[0] = status
[0];
414 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
415 GET_UART_STATUS
, GET_UART_STATUS_TYPE
,
416 0, GET_UART_STATUS_MSR
, status_buffer
, 1, 100);
418 dev_dbg(&dev
->dev
, "Get MSR = 0x%p failed (error = %d)",
421 dev_dbg(&dev
->dev
, "0xc0:0x22:0:6 %d - 0x%p ", ret
, status_buffer
);
422 status
[0] = status_buffer
[0];
423 kfree(status_buffer
);
428 /* select the work mode.
429 * NOTE this function not supported by spcp825-007 */
430 static void spcp8x5_set_workMode(struct usb_device
*dev
, u16 value
,
431 u16 index
, enum spcp8x5_type type
)
435 /* I return Permited not support here but seem inval device
437 if (type
== SPCP825_007_TYPE
)
440 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
441 SET_WORKING_MODE_TYPE
, SET_WORKING_MODE
,
442 value
, index
, NULL
, 0, 100);
443 dev_dbg(&dev
->dev
, "value = %#x , index = %#x\n", value
, index
);
446 "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret
);
449 /* close the serial port. We should wait for data sending to device 1st and
450 * then kill all urb. */
451 static void spcp8x5_close(struct usb_serial_port
*port
, struct file
*filp
)
453 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
455 unsigned int c_cflag
;
461 dbg("%s - port %d", __func__
, port
->number
);
463 /* wait for data to drain from the buffer */
464 spin_lock_irqsave(&priv
->lock
, flags
);
465 timeout
= SPCP8x5_CLOSING_WAIT
;
466 init_waitqueue_entry(&wait
, current
);
467 add_wait_queue(&port
->tty
->write_wait
, &wait
);
469 set_current_state(TASK_INTERRUPTIBLE
);
470 if (ringbuf_avail_data(priv
->buf
) == 0 ||
471 timeout
== 0 || signal_pending(current
))
473 spin_unlock_irqrestore(&priv
->lock
, flags
);
474 timeout
= schedule_timeout(timeout
);
475 spin_lock_irqsave(&priv
->lock
, flags
);
477 set_current_state(TASK_RUNNING
);
478 remove_wait_queue(&port
->tty
->write_wait
, &wait
);
480 /* clear out any remaining data in the buffer */
481 clear_ringbuf(priv
->buf
);
482 spin_unlock_irqrestore(&priv
->lock
, flags
);
484 /* wait for characters to drain from the device (this is long enough
485 * for the entire all byte spcp8x5 hardware buffer to drain with no
486 * flow control for data rates of 1200 bps or more, for lower rates we
487 * should really know how much data is in the buffer to compute a delay
488 * that is not unnecessarily long) */
489 bps
= tty_get_baud_rate(port
->tty
);
491 timeout
= max((HZ
*2560) / bps
, HZ
/10);
494 set_current_state(TASK_INTERRUPTIBLE
);
495 schedule_timeout(timeout
);
497 /* clear control lines */
499 c_cflag
= port
->tty
->termios
->c_cflag
;
500 if (c_cflag
& HUPCL
) {
501 spin_lock_irqsave(&priv
->lock
, flags
);
502 priv
->line_control
= 0;
503 spin_unlock_irqrestore(&priv
->lock
, flags
);
504 spcp8x5_set_ctrlLine(port
->serial
->dev
, 0 , priv
->type
);
509 if (port
->write_urb
!= NULL
) {
510 result
= usb_unlink_urb(port
->write_urb
);
513 "usb_unlink_urb(write_urb) = %d\n", result
);
515 result
= usb_unlink_urb(port
->read_urb
);
517 dev_dbg(&port
->dev
, "usb_unlink_urb(read_urb) = %d\n", result
);
520 /* set the serial param for transfer. we should check if we really need to
521 * transfer. then if be set flow contorl we should do this too. */
522 static void spcp8x5_set_termios(struct usb_serial_port
*port
,
523 struct ktermios
*old_termios
)
525 struct usb_serial
*serial
= port
->serial
;
526 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
528 unsigned int cflag
= port
->tty
->termios
->c_cflag
;
529 unsigned int old_cflag
= old_termios
->c_cflag
;
530 unsigned short uartdata
;
531 unsigned char buf
[2] = {0, 0};
536 if ((!port
->tty
) || (!port
->tty
->termios
))
539 /* for the 1st time call this function */
540 spin_lock_irqsave(&priv
->lock
, flags
);
541 if (!priv
->termios_initialized
) {
542 *(port
->tty
->termios
) = tty_std_termios
;
543 port
->tty
->termios
->c_cflag
= B115200
| CS8
| CREAD
|
545 priv
->termios_initialized
= 1;
547 spin_unlock_irqrestore(&priv
->lock
, flags
);
549 /* check that they really want us to change something */
550 if (!tty_termios_hw_change(port
->tty
->termios
, old_termios
))
553 /* set DTR/RTS active */
554 spin_lock_irqsave(&priv
->lock
, flags
);
555 control
= priv
->line_control
;
556 if ((old_cflag
& CBAUD
) == B0
) {
557 priv
->line_control
|= MCR_DTR
;
558 if (!(old_cflag
& CRTSCTS
))
559 priv
->line_control
|= MCR_RTS
;
561 if (control
!= priv
->line_control
) {
562 control
= priv
->line_control
;
563 spin_unlock_irqrestore(&priv
->lock
, flags
);
564 spcp8x5_set_ctrlLine(serial
->dev
, control
, priv
->type
);
566 spin_unlock_irqrestore(&priv
->lock
, flags
);
570 baud
= tty_get_baud_rate(port
->tty
);;
572 case 300: buf
[0] = 0x00; break;
573 case 600: buf
[0] = 0x01; break;
574 case 1200: buf
[0] = 0x02; break;
575 case 2400: buf
[0] = 0x03; break;
576 case 4800: buf
[0] = 0x04; break;
577 case 9600: buf
[0] = 0x05; break;
578 case 19200: buf
[0] = 0x07; break;
579 case 38400: buf
[0] = 0x09; break;
580 case 57600: buf
[0] = 0x0a; break;
581 case 115200: buf
[0] = 0x0b; break;
582 case 230400: buf
[0] = 0x0c; break;
583 case 460800: buf
[0] = 0x0d; break;
584 case 921600: buf
[0] = 0x0e; break;
585 /* case 1200000: buf[0] = 0x0f; break; */
586 /* case 2400000: buf[0] = 0x10; break; */
587 case 3000000: buf
[0] = 0x11; break;
588 /* case 6000000: buf[0] = 0x12; break; */
591 buf
[0] = 0x0b; break;
593 err("spcp825 driver does not support the baudrate "
594 "requested, using default of 9600.");
597 /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
599 switch (cflag
& CSIZE
) {
601 buf
[1] |= SET_UART_FORMAT_SIZE_5
;
604 buf
[1] |= SET_UART_FORMAT_SIZE_6
;
607 buf
[1] |= SET_UART_FORMAT_SIZE_7
;
611 buf
[1] |= SET_UART_FORMAT_SIZE_8
;
616 /* Set Stop bit2 : 0:1bit 1:2bit */
617 buf
[1] |= (cflag
& CSTOPB
) ? SET_UART_FORMAT_STOP_2
:
618 SET_UART_FORMAT_STOP_1
;
620 /* Set Parity bit3-4 01:Odd 11:Even */
621 if (cflag
& PARENB
) {
622 buf
[1] |= (cflag
& PARODD
) ?
623 SET_UART_FORMAT_PAR_ODD
: SET_UART_FORMAT_PAR_EVEN
;
625 buf
[1] |= SET_UART_FORMAT_PAR_NONE
;
627 uartdata
= buf
[0] | buf
[1]<<8;
629 i
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
630 SET_UART_FORMAT_TYPE
, SET_UART_FORMAT
,
631 uartdata
, 0, NULL
, 0, 100);
633 err("Set UART format %#x failed (error = %d)", uartdata
, i
);
634 dbg("0x21:0x40:0:0 %d\n", i
);
636 if (cflag
& CRTSCTS
) {
637 /* enable hardware flow control */
638 spcp8x5_set_workMode(serial
->dev
, 0x000a,
639 SET_WORKING_MODE_U2C
, priv
->type
);
644 /* open the serial port. do some usb system call. set termios and get the line
645 * status of the device. then submit the read urb */
646 static int spcp8x5_open(struct usb_serial_port
*port
, struct file
*filp
)
648 struct ktermios tmp_termios
;
649 struct usb_serial
*serial
= port
->serial
;
650 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
654 /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */
656 dbg("%s - port %d", __func__
, port
->number
);
658 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
659 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
661 ret
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
663 0x01, 0x00, NULL
, 0x00, 100);
667 spin_lock_irqsave(&priv
->lock
, flags
);
668 if (port
->tty
->termios
->c_cflag
& CBAUD
)
669 priv
->line_control
= MCR_DTR
| MCR_RTS
;
671 priv
->line_control
= 0;
672 spin_unlock_irqrestore(&priv
->lock
, flags
);
674 spcp8x5_set_ctrlLine(serial
->dev
, priv
->line_control
, priv
->type
);
678 spcp8x5_set_termios(port
, &tmp_termios
);
680 spcp8x5_get_msr(serial
->dev
, &status
, priv
->type
);
682 /* may be we should update uart status here but now we did not do */
683 spin_lock_irqsave(&priv
->lock
, flags
);
684 priv
->line_status
= status
& 0xf0 ;
685 spin_unlock_irqrestore(&priv
->lock
, flags
);
687 /* FIXME: need to assert RTS and DTR if CRTSCTS off */
689 dbg("%s - submitting read urb", __func__
);
690 port
->read_urb
->dev
= serial
->dev
;
691 ret
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
693 spcp8x5_close(port
, NULL
);
699 /* bulk read call back function. check the status of the urb. if transfer
700 * failed return. then update the status and the tty send data to tty subsys.
703 static void spcp8x5_read_bulk_callback(struct urb
*urb
)
705 struct usb_serial_port
*port
= urb
->context
;
706 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
707 struct tty_struct
*tty
;
708 unsigned char *data
= urb
->transfer_buffer
;
715 dev_dbg(&port
->dev
, "start, urb->status = %d, "
716 "urb->actual_length = %d\n,", urb
->status
, urb
->actual_length
);
718 /* check the urb status */
720 if (!port
->open_count
)
722 if (urb
->status
== -EPROTO
) {
723 /* spcp8x5 mysteriously fails with -EPROTO */
724 /* reschedule the read */
726 urb
->dev
= port
->serial
->dev
;
727 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
730 "failed submitting read urb %d\n",
734 dev_dbg(&port
->dev
, "unable to handle the error, exiting.\n");
738 /* get tty_flag from status */
739 tty_flag
= TTY_NORMAL
;
741 spin_lock_irqsave(&priv
->lock
, flags
);
742 status
= priv
->line_status
;
743 priv
->line_status
&= ~UART_STATE_TRANSIENT_MASK
;
744 spin_unlock_irqrestore(&priv
->lock
, flags
);
745 /* wake up the wait for termios */
746 wake_up_interruptible(&priv
->delta_msr_wait
);
748 /* break takes precedence over parity, which takes precedence over
750 if (status
& UART_BREAK_ERROR
)
751 tty_flag
= TTY_BREAK
;
752 else if (status
& UART_PARITY_ERROR
)
753 tty_flag
= TTY_PARITY
;
754 else if (status
& UART_FRAME_ERROR
)
755 tty_flag
= TTY_FRAME
;
756 dev_dbg(&port
->dev
, "tty_flag = %d\n", tty_flag
);
759 if (tty
&& urb
->actual_length
) {
760 tty_buffer_request_room(tty
, urb
->actual_length
+ 1);
761 /* overrun is special, not associated with a char */
762 if (status
& UART_OVERRUN_ERROR
)
763 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
764 for (i
= 0; i
< urb
->actual_length
; ++i
)
765 tty_insert_flip_char(tty
, data
[i
], tty_flag
);
766 tty_flip_buffer_push(tty
);
769 /* Schedule the next read _if_ we are still open */
770 if (port
->open_count
) {
771 urb
->dev
= port
->serial
->dev
;
772 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
774 dev_dbg(&port
->dev
, "failed submitting read urb %d\n",
781 /* get data from ring buffer and then write to usb bus */
782 static void spcp8x5_send(struct usb_serial_port
*port
)
785 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
788 spin_lock_irqsave(&priv
->lock
, flags
);
790 if (priv
->write_urb_in_use
) {
791 dev_dbg(&port
->dev
, "write urb still used\n");
792 spin_unlock_irqrestore(&priv
->lock
, flags
);
796 /* send the 1st urb for writting */
797 memset(port
->write_urb
->transfer_buffer
, 0x00 , port
->bulk_out_size
);
798 count
= get_ringbuf(priv
->buf
, port
->write_urb
->transfer_buffer
,
799 port
->bulk_out_size
);
802 spin_unlock_irqrestore(&priv
->lock
, flags
);
806 /* update the urb status */
807 priv
->write_urb_in_use
= 1;
809 spin_unlock_irqrestore(&priv
->lock
, flags
);
811 port
->write_urb
->transfer_buffer_length
= count
;
812 port
->write_urb
->dev
= port
->serial
->dev
;
814 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
816 dev_dbg(&port
->dev
, "failed submitting write urb, error %d\n",
818 priv
->write_urb_in_use
= 0;
819 /* TODO: reschedule spcp8x5_send */
823 schedule_work(&port
->work
);
826 /* this is the call back function for write urb. NOTE we should not sleep in
827 * this routine. check the urb return code and then submit the write urb again
828 * to hold the write loop */
829 static void spcp8x5_write_bulk_callback(struct urb
*urb
)
831 struct usb_serial_port
*port
= urb
->context
;
832 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
835 switch (urb
->status
) {
842 /* this urb is terminated, clean up */
843 dev_dbg(&port
->dev
, "urb shutting down with status: %d\n",
845 priv
->write_urb_in_use
= 0;
848 /* error in the urb, so we have to resubmit it */
849 dbg("%s - Overflow in write", __func__
);
850 dbg("%s - nonzero write bulk status received: %d",
851 __func__
, urb
->status
);
852 port
->write_urb
->transfer_buffer_length
= 1;
853 port
->write_urb
->dev
= port
->serial
->dev
;
854 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
857 "failed resubmitting write urb %d\n", result
);
862 priv
->write_urb_in_use
= 0;
864 /* send any buffered data */
868 /* write data to ring buffer. and then start the write transfer */
869 static int spcp8x5_write(struct usb_serial_port
*port
,
870 const unsigned char *buf
, int count
)
872 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
875 dev_dbg(&port
->dev
, "%d bytes\n", count
);
880 spin_lock_irqsave(&priv
->lock
, flags
);
881 count
= put_ringbuf(priv
->buf
, buf
, count
);
882 spin_unlock_irqrestore(&priv
->lock
, flags
);
889 static int spcp8x5_wait_modem_info(struct usb_serial_port
*port
,
892 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
894 unsigned int prevstatus
;
896 unsigned int changed
;
898 spin_lock_irqsave(&priv
->lock
, flags
);
899 prevstatus
= priv
->line_status
;
900 spin_unlock_irqrestore(&priv
->lock
, flags
);
903 /* wake up in bulk read */
904 interruptible_sleep_on(&priv
->delta_msr_wait
);
906 /* see if a signal did it */
907 if (signal_pending(current
))
910 spin_lock_irqsave(&priv
->lock
, flags
);
911 status
= priv
->line_status
;
912 spin_unlock_irqrestore(&priv
->lock
, flags
);
914 changed
= prevstatus
^status
;
916 if (((arg
& TIOCM_RNG
) && (changed
& MSR_STATUS_LINE_RI
)) ||
917 ((arg
& TIOCM_DSR
) && (changed
& MSR_STATUS_LINE_DSR
)) ||
918 ((arg
& TIOCM_CD
) && (changed
& MSR_STATUS_LINE_DCD
)) ||
919 ((arg
& TIOCM_CTS
) && (changed
& MSR_STATUS_LINE_CTS
)))
928 static int spcp8x5_ioctl(struct usb_serial_port
*port
, struct file
*file
,
929 unsigned int cmd
, unsigned long arg
)
931 dbg("%s (%d) cmd = 0x%04x", __func__
, port
->number
, cmd
);
935 dbg("%s (%d) TIOCMIWAIT", __func__
, port
->number
);
936 return spcp8x5_wait_modem_info(port
, arg
);
939 dbg("%s not supported = 0x%04x", __func__
, cmd
);
946 static int spcp8x5_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
947 unsigned int set
, unsigned int clear
)
949 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
953 spin_lock_irqsave(&priv
->lock
, flags
);
955 priv
->line_control
|= MCR_RTS
;
957 priv
->line_control
|= MCR_DTR
;
958 if (clear
& TIOCM_RTS
)
959 priv
->line_control
&= ~MCR_RTS
;
960 if (clear
& TIOCM_DTR
)
961 priv
->line_control
&= ~MCR_DTR
;
962 control
= priv
->line_control
;
963 spin_unlock_irqrestore(&priv
->lock
, flags
);
965 return spcp8x5_set_ctrlLine(port
->serial
->dev
, control
, priv
->type
);
968 static int spcp8x5_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
970 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
976 spin_lock_irqsave(&priv
->lock
, flags
);
977 mcr
= priv
->line_control
;
978 status
= priv
->line_status
;
979 spin_unlock_irqrestore(&priv
->lock
, flags
);
981 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
982 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
983 | ((status
& MSR_STATUS_LINE_CTS
) ? TIOCM_CTS
: 0)
984 | ((status
& MSR_STATUS_LINE_DSR
) ? TIOCM_DSR
: 0)
985 | ((status
& MSR_STATUS_LINE_RI
) ? TIOCM_RI
: 0)
986 | ((status
& MSR_STATUS_LINE_DCD
) ? TIOCM_CD
: 0);
991 /* get the avail space room in ring buffer */
992 static int spcp8x5_write_room(struct usb_serial_port
*port
)
994 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
998 spin_lock_irqsave(&priv
->lock
, flags
);
999 room
= ringbuf_avail_space(priv
->buf
);
1000 spin_unlock_irqrestore(&priv
->lock
, flags
);
1005 /* get the number of avail data in write ring buffer */
1006 static int spcp8x5_chars_in_buffer(struct usb_serial_port
*port
)
1008 struct spcp8x5_private
*priv
= usb_get_serial_port_data(port
);
1010 unsigned long flags
;
1012 spin_lock_irqsave(&priv
->lock
, flags
);
1013 chars
= ringbuf_avail_data(priv
->buf
);
1014 spin_unlock_irqrestore(&priv
->lock
, flags
);
1019 /* All of the device info needed for the spcp8x5 SIO serial converter */
1020 static struct usb_serial_driver spcp8x5_device
= {
1022 .owner
= THIS_MODULE
,
1025 .id_table
= id_table
,
1027 .open
= spcp8x5_open
,
1028 .close
= spcp8x5_close
,
1029 .write
= spcp8x5_write
,
1030 .set_termios
= spcp8x5_set_termios
,
1031 .ioctl
= spcp8x5_ioctl
,
1032 .tiocmget
= spcp8x5_tiocmget
,
1033 .tiocmset
= spcp8x5_tiocmset
,
1034 .write_room
= spcp8x5_write_room
,
1035 .read_bulk_callback
= spcp8x5_read_bulk_callback
,
1036 .write_bulk_callback
= spcp8x5_write_bulk_callback
,
1037 .chars_in_buffer
= spcp8x5_chars_in_buffer
,
1038 .attach
= spcp8x5_startup
,
1039 .shutdown
= spcp8x5_shutdown
,
1042 static int __init
spcp8x5_init(void)
1045 retval
= usb_serial_register(&spcp8x5_device
);
1047 goto failed_usb_serial_register
;
1048 retval
= usb_register(&spcp8x5_driver
);
1050 goto failed_usb_register
;
1051 info(DRIVER_DESC
" " DRIVER_VERSION
);
1053 failed_usb_register
:
1054 usb_serial_deregister(&spcp8x5_device
);
1055 failed_usb_serial_register
:
1059 static void __exit
spcp8x5_exit(void)
1061 usb_deregister(&spcp8x5_driver
);
1062 usb_serial_deregister(&spcp8x5_device
);
1065 module_init(spcp8x5_init
);
1066 module_exit(spcp8x5_exit
);
1068 MODULE_DESCRIPTION(DRIVER_DESC
);
1069 MODULE_VERSION(DRIVER_VERSION
);
1070 MODULE_LICENSE("GPL");
1072 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1073 MODULE_PARM_DESC(debug
, "Debug enabled or not");