2 * This code was developed for the Quatech USB line for linux, it used
3 * much of the code developed by Greg Kroah-Hartman for USB serial devices
7 #include <linux/sched.h>
8 #include <linux/slab.h>
10 #include <linux/tty_flip.h>
11 #include <linux/module.h>
12 #include <linux/usb.h>
13 #include <linux/wait.h>
14 #include <linux/types.h>
15 #include <linux/version.h>
16 #include <linux/uaccess.h>
18 /* Use our own dbg macro */
19 /* #define DEBUG_ON */
22 #define mydbg(const...) printk(const)
24 #define mydbg(const...)
27 /* parity check flag */
28 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
30 #define SERIAL_TTY_MAJOR 0 /* Nice legal number now */
31 #define SERIAL_TTY_MINORS 255 /* loads of devices :) */
32 #define MAX_NUM_PORTS 8 /* The maximum number of ports one device can grab at once */
33 #define PREFUFF_LEVEL_CONSERVATIVE 128
34 #define ATC_DISABLED 0x00
36 #define RR_BITS 0x03 /* for clearing clock bits */
37 #define DUPMODE_BITS 0xc0
39 #define RS232_MODE 0x00
40 #define RTSCTS_TO_CONNECTOR 0x40
43 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
44 #define ALL_LOOPBACK 0x01
45 #define MODEM_CTRL 0x40
47 #define THISCHAR data[i]
48 #define NEXTCHAR data[i + 1]
49 #define THIRDCHAR data[i + 2]
50 #define FOURTHCHAR data[i + 3]
53 * Useful defintions for port A, Port B and Port C
55 #define FULLPWRBIT 0x00000080
56 #define NEXT_BOARD_POWER_BIT 0x00000004
58 #define SERIAL_LSR_OE 0x02
59 #define SERIAL_LSR_PE 0x04
60 #define SERIAL_LSR_FE 0x08
61 #define SERIAL_LSR_BI 0x10
63 #define SERIAL_LSR_TEMT 0x40
65 #define DIV_LATCH_LS 0x00
66 #define XMT_HOLD_REGISTER 0x00
67 #define XVR_BUFFER_REGISTER 0x00
68 #define DIV_LATCH_MS 0x01
69 #define FIFO_CONTROL_REGISTER 0x02
70 #define LINE_CONTROL_REGISTER 0x03
71 #define MODEM_CONTROL_REGISTER 0x04
72 #define LINE_STATUS_REGISTER 0x05
73 #define MODEM_STATUS_REGISTER 0x06
75 #define SERIAL_MCR_DTR 0x01
76 #define SERIAL_MCR_RTS 0x02
77 #define SERIAL_MCR_LOOP 0x10
79 #define SERIAL_MSR_CTS 0x10
80 #define SERIAL_MSR_CD 0x80
81 #define SERIAL_MSR_RI 0x40
82 #define SERIAL_MSR_DSR 0x20
83 #define SERIAL_MSR_MASK 0xf0
85 #define SERIAL_8_DATA 0x03
86 #define SERIAL_7_DATA 0x02
87 #define SERIAL_6_DATA 0x01
88 #define SERIAL_5_DATA 0x00
90 #define SERIAL_ODD_PARITY 0X08
91 #define SERIAL_EVEN_PARITY 0X18
92 #define SERIAL_TWO_STOPB 0x04
93 #define SERIAL_ONE_STOPB 0x00
95 #define MAX_BAUD_RATE 460800
96 #define MAX_BAUD_REMAINDER 4608
98 #define QT_SET_GET_DEVICE 0xc2
99 #define QT_OPEN_CLOSE_CHANNEL 0xca
100 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
101 #define QT_SET_ATF 0xcd
102 #define QT_GET_SET_REGISTER 0xc0
103 #define QT_GET_SET_UART 0xc1
104 #define QT_HW_FLOW_CONTROL_MASK 0xc5
105 #define QT_SW_FLOW_CONTROL_MASK 0xc6
106 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
107 #define QT_BREAK_CONTROL 0xc8
109 #define SERIALQT_PCI_IOC_MAGIC 'k'
110 #define SERIALQT_WRITE_QOPR _IOW(SERIALQT_PCI_IOC_MAGIC, 0, int)
111 #define SERIALQT_WRITE_QMCR _IOW(SERIALQT_PCI_IOC_MAGIC, 1, int)
112 #define SERIALQT_GET_NUMOF_UNITS _IOR(SERIALQT_PCI_IOC_MAGIC, 2, void *)
113 #define SERIALQT_GET_THIS_UNIT _IOR(SERIALQT_PCI_IOC_MAGIC, 3, void *)
114 #define SERIALQT_READ_QOPR _IOR(SERIALQT_PCI_IOC_MAGIC, 4, int)
115 #define SERIALQT_READ_QMCR _IOR(SERIALQT_PCI_IOC_MAGIC, 5, int)
116 #define SERIALQT_IS422_EXTENDED _IOR(SERIALQT_PCI_IOC_MAGIC, 6, int) /* returns successful if 422 extended */
118 #define USBD_TRANSFER_DIRECTION_IN 0xc0
119 #define USBD_TRANSFER_DIRECTION_OUT 0x40
121 #define ATC_DISABLED 0x00
122 #define ATC_RTS_ENABLED 0x02
123 #define ATC_DTR_ENABLED 0x01
125 #define RR_BITS 0x03 /* for clearing clock bits */
126 #define DUPMODE_BITS 0xc0
128 #define FULL_DUPLEX 0x00
129 #define HALF_DUPLEX_RTS 0x40
130 #define HALF_DUPLEX_DTR 0x80
132 #define QMCR_FULL_DUPLEX 0x00
133 #define QMCR_HALF_DUPLEX_RTS 0x02
134 #define QMCR_HALF_DUPLEX_DTR 0x01
135 #define QMCR_HALF_DUPLEX_MASK 0x03
136 #define QMCR_CONNECTOR_MASK 0x1C
138 #define QMCR_RX_EN_MASK 0x20
140 #define QMCR_ALL_LOOPBACK 0x10
141 #define QMCR_MODEM_CONTROL 0X00
143 #define SERIALQT_IOC_MAXNR 6
145 struct usb_serial_port
{
146 struct usb_serial
*serial
; /* pointer back to the owner of this port */
147 struct tty_struct
*tty
; /* the coresponding tty for this port */
148 unsigned char number
;
149 char active
; /* someone has this device open */
151 unsigned char *interrupt_in_buffer
;
152 struct urb
*interrupt_in_urb
;
153 __u8 interrupt_in_endpointAddress
;
155 unsigned char *bulk_in_buffer
;
156 unsigned char *xfer_to_tty_buffer
;
157 struct urb
*read_urb
;
158 __u8 bulk_in_endpointAddress
;
160 unsigned char *bulk_out_buffer
;
162 struct urb
*write_urb
;
163 __u8 bulk_out_endpointAddress
;
165 wait_queue_head_t write_wait
;
166 wait_queue_head_t wait
;
167 struct work_struct work
;
169 int open_count
; /* number of times this port has been opened */
170 struct semaphore sem
; /* locks this structure */
172 __u8 shadowLCR
; /* last LCR value received */
173 __u8 shadowMCR
; /* last MCR value received */
174 __u8 shadowMSR
; /* last MSR value received */
175 __u8 shadowLSR
; /* last LSR value received */
180 void *private; /* data private to the specific port */
189 struct usb_device
*dev
;
190 struct usb_interface
*interface
; /* the interface for this device */
191 struct tty_driver
*tty_driver
; /* the tty_driver for this device */
192 unsigned char minor
; /* the starting minor number for this device */
193 unsigned char num_ports
; /* the number of ports this device has */
194 char num_interrupt_in
; /* number of interrupt in endpoints we have */
195 char num_bulk_in
; /* number of bulk in endpoints we have */
196 char num_bulk_out
; /* number of bulk out endpoints we have */
197 unsigned char num_OpenCount
; /* the number of ports this device has */
199 __u16 vendor
; /* vendor id of this device */
200 __u16 product
; /* product id of this device */
201 struct usb_serial_port port
[MAX_NUM_PORTS
];
203 void *private; /* data private to the specific driver */
206 static inline int port_paranoia_check(struct usb_serial_port
*port
,
207 const char *function
)
210 dbg("%s - port == NULL", function
);
214 dbg("%s - port->serial == NULL\n", function
);
218 dbg("%s - port->tty == NULL\n", function
);
225 /* Inline functions to check the sanity of a pointer that is passed to us */
226 static inline int serial_paranoia_check(struct usb_serial
*serial
,
227 const char *function
)
230 dbg("%s - serial == NULL\n", function
);
237 static inline struct usb_serial
*get_usb_serial(struct usb_serial_port
*port
,
238 const char *function
)
240 /* if no port was specified, or it fails a paranoia check */
242 port_paranoia_check(port
, function
) ||
243 serial_paranoia_check(port
->serial
, function
)) {
244 /* then say that we dont have a valid usb_serial thing, which will
245 * end up genrating -ENODEV return values */
252 struct qt_get_device_data
{
258 struct qt_open_channel_data
{
263 static void ProcessLineStatus(struct usb_serial_port
*port
,
264 unsigned char line_status
);
265 static void ProcessModemStatus(struct usb_serial_port
*port
,
266 unsigned char modem_status
);
267 static void ProcessRxChar(struct usb_serial_port
*port
, unsigned char Data
);
268 static struct usb_serial
*get_free_serial(int num_ports
, int *minor
);
270 static int serqt_probe(struct usb_interface
*interface
,
271 const struct usb_device_id
*id
);
273 static void serqt_usb_disconnect(struct usb_interface
*interface
);
274 static int box_set_device(struct usb_serial
*serial
,
275 struct qt_get_device_data
*pDeviceData
);
276 static int box_get_device(struct usb_serial
*serial
,
277 struct qt_get_device_data
*pDeviceData
);
278 static int serial_open(struct tty_struct
*tty
, struct file
*filp
);
279 static void serial_close(struct tty_struct
*tty
, struct file
*filp
);
280 static int serial_write_room(struct tty_struct
*tty
);
281 static int serial_ioctl(struct tty_struct
*tty
, struct file
*file
,
282 unsigned int cmd
, unsigned long arg
);
283 static void serial_set_termios(struct tty_struct
*tty
, struct ktermios
*old
);
284 static int serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
287 static void serial_throttle(struct tty_struct
*tty
);
288 static void serial_unthrottle(struct tty_struct
*tty
);
289 static int serial_break(struct tty_struct
*tty
, int break_state
);
290 static int serial_chars_in_buffer(struct tty_struct
*tty
);
292 static int qt_open(struct tty_struct
*tty
, struct usb_serial_port
*port
,
294 static int BoxSetPrebufferLevel(struct usb_serial
*serial
);
296 static int BoxSetATC(struct usb_serial
*serial
, __u16 n_Mode
);
297 static int BoxSetUart(struct usb_serial
*serial
, unsigned short Uart_Number
,
298 unsigned short default_divisor
,
299 unsigned char default_LCR
);
301 static int BoxOPenCloseChannel(struct usb_serial
*serial
, __u16 Uart_Number
,
303 struct qt_open_channel_data
*pDeviceData
);
304 static void qt_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
306 static int BoxGetRegister(struct usb_serial
*serial
, unsigned short Uart_Number
,
307 unsigned short Register_Num
, __u8
*pValue
);
308 static int BoxSetRegister(struct usb_serial
*serial
, unsigned short Uart_Number
,
309 unsigned short Register_Num
, unsigned short Value
);
310 static void qt_write_bulk_callback(struct urb
*urb
);
311 static int qt_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
312 const unsigned char *buf
, int count
);
313 static void port_softint(struct work_struct
*work
);
314 static int qt_write_room(struct usb_serial_port
*port
);
315 static int qt_chars_in_buffer(struct usb_serial_port
*port
);
316 static int qt_ioctl(struct tty_struct
*tty
, struct usb_serial_port
*port
,
317 struct file
*file
, unsigned int cmd
, unsigned long arg
);
318 static void qt_set_termios(struct tty_struct
*tty
,
319 struct usb_serial_port
*port
,
320 struct ktermios
*old_termios
);
321 static int BoxSetHW_FlowCtrl(struct usb_serial
*serial
, unsigned int index
,
323 static int BoxDisable_SW_FlowCtrl(struct usb_serial
*serial
, __u16 index
);
324 static int EmulateWriteQMCR_Reg(int index
, unsigned uc_value
);
325 static int EmulateReadQMCR_Reg(int index
, unsigned *uc_value
);
326 static struct usb_serial
*find_the_box(unsigned int index
);
327 static int ioctl_serial_usb(struct inode
*innod
, struct file
*filp
, unsigned int cmd
,
330 static int BoxSetSW_FlowCtrl(struct usb_serial
*serial
, __u16 Uart
,
331 unsigned char stop_char
, unsigned char start_char
);
332 static void qt_read_bulk_callback(struct urb
*urb
);
334 static void port_sofrint(void *private);
336 static void return_serial(struct usb_serial
*serial
);
338 static int serial_tiocmset(struct tty_struct
*tty
, struct file
*file
,
339 unsigned int set
, unsigned int clear
);
340 static int serial_tiocmget(struct tty_struct
*tty
, struct file
*file
);
342 static int qt_tiocmset(struct tty_struct
*tty
, struct usb_serial_port
*port
,
343 struct file
*file
, unsigned int value
);
345 static int qt_tiocmget(struct tty_struct
*tty
, struct usb_serial_port
*port
,
348 /* Version Information */
349 #define DRIVER_VERSION "v2.14"
350 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
351 #define DRIVER_DESC "Quatech USB to Serial Driver"
353 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
354 #define DEVICE_ID_QUATECH_RS232_SINGLE_PORT 0xC020 /* SSU100 */
355 #define DEVICE_ID_QUATECH_RS422_SINGLE_PORT 0xC030 /* SSU200 */
356 #define DEVICE_ID_QUATECH_RS232_DUAL_PORT 0xC040 /* DSU100 */
357 #define DEVICE_ID_QUATECH_RS422_DUAL_PORT 0xC050 /* DSU200 */
358 #define DEVICE_ID_QUATECH_RS232_FOUR_PORT 0xC060 /* QSU100 */
359 #define DEVICE_ID_QUATECH_RS422_FOUR_PORT 0xC070 /* QSU200 */
360 #define DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A 0xC080 /* ESU100A */
361 #define DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B 0xC081 /* ESU100B */
362 #define DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A 0xC0A0 /* ESU200A */
363 #define DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B 0xC0A1 /* ESU200B */
364 #define DEVICE_ID_QUATECH_RS232_16_PORT_A 0xC090 /* HSU100A */
365 #define DEVICE_ID_QUATECH_RS232_16_PORT_B 0xC091 /* HSU100B */
366 #define DEVICE_ID_QUATECH_RS232_16_PORT_C 0xC092 /* HSU100C */
367 #define DEVICE_ID_QUATECH_RS232_16_PORT_D 0xC093 /* HSU100D */
368 #define DEVICE_ID_QUATECH_RS422_16_PORT_A 0xC0B0 /* HSU200A */
369 #define DEVICE_ID_QUATECH_RS422_16_PORT_B 0xC0B1 /* HSU200B */
370 #define DEVICE_ID_QUATECH_RS422_16_PORT_C 0xC0B2 /* HSU200C */
371 #define DEVICE_ID_QUATECH_RS422_16_PORT_D 0xC0B3 /* HSU200D */
373 /* table of Quatech devices */
374 static struct usb_device_id serqt_table
[] = {
376 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_SINGLE_PORT
)},
378 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_SINGLE_PORT
)},
379 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_DUAL_PORT
)},
380 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_DUAL_PORT
)},
381 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_FOUR_PORT
)},
382 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_FOUR_PORT
)},
384 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A
)},
386 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B
)},
388 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A
)},
390 (USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B
)},
391 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_16_PORT_A
)},
392 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_16_PORT_B
)},
393 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_16_PORT_C
)},
394 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS232_16_PORT_D
)},
395 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_16_PORT_A
)},
396 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_16_PORT_B
)},
397 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_16_PORT_C
)},
398 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, DEVICE_ID_QUATECH_RS422_16_PORT_D
)},
399 {} /* Terminating entry */
402 MODULE_DEVICE_TABLE(usb
, serqt_table
);
404 static int major_number
;
405 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
]; /* initially all NULL */
407 /* table of Quatech 422devices */
408 static unsigned int serqt_422_table
[] = {
409 DEVICE_ID_QUATECH_RS422_SINGLE_PORT
,
410 DEVICE_ID_QUATECH_RS422_DUAL_PORT
,
411 DEVICE_ID_QUATECH_RS422_FOUR_PORT
,
412 DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A
,
413 DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B
,
414 DEVICE_ID_QUATECH_RS422_16_PORT_A
,
415 DEVICE_ID_QUATECH_RS422_16_PORT_B
,
416 DEVICE_ID_QUATECH_RS422_16_PORT_C
,
417 DEVICE_ID_QUATECH_RS422_16_PORT_D
,
418 0 /* terminate with zero */
421 /* usb specific object needed to register this driver with the usb subsystem */
422 static struct usb_driver serqt_usb_driver
= {
423 .name
= "quatech-usb-serial",
424 .probe
= serqt_probe
,
425 .disconnect
= serqt_usb_disconnect
,
426 .id_table
= serqt_table
,
429 static struct ktermios
*serial_termios
[SERIAL_TTY_MINORS
];
430 static struct ktermios
*serial_termios_locked
[SERIAL_TTY_MINORS
];
432 static const struct tty_operations serial_ops
= {
434 .close
= serial_close
,
435 .write
= serial_write
,
436 .write_room
= serial_write_room
,
437 .ioctl
= serial_ioctl
,
438 .set_termios
= serial_set_termios
,
439 .throttle
= serial_throttle
,
440 .unthrottle
= serial_unthrottle
,
441 .break_ctl
= serial_break
,
442 .chars_in_buffer
= serial_chars_in_buffer
,
443 .tiocmset
= serial_tiocmset
,
444 .tiocmget
= serial_tiocmget
,
447 static struct tty_driver serial_tty_driver
= {
448 .magic
= TTY_DRIVER_MAGIC
,
449 .driver_name
= "Quatech usb-serial",
451 .major
= SERIAL_TTY_MAJOR
,
453 .num
= SERIAL_TTY_MINORS
,
454 .type
= TTY_DRIVER_TYPE_SERIAL
,
455 .subtype
= SERIAL_TYPE_NORMAL
,
456 .flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
,
458 .termios
= serial_termios
,
459 .termios_locked
= serial_termios_locked
,
460 .init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
,
462 .init_termios
.c_iflag
= ICRNL
| IXON
,
463 .init_termios
.c_oflag
= OPOST
,
465 .init_termios
.c_lflag
=
466 ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOKE
| IEXTEN
,
469 /* fops for parent device */
470 static const struct file_operations serialqt_usb_fops
= {
471 .ioctl
= ioctl_serial_usb
,
477 * Called by the usb core when a new device is connected that it thinks
478 * this driver might be interested in.
481 static int serqt_probe(struct usb_interface
*interface
,
482 const struct usb_device_id
*id
)
484 struct usb_device
*dev
= interface_to_usbdev(interface
);
485 struct usb_serial
*serial
= NULL
;
486 struct usb_serial_port
*port
;
487 struct usb_endpoint_descriptor
*endpoint
;
488 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
489 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
490 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
494 struct usb_host_interface
*iface_desc
;
495 int num_interrupt_in
= 0;
497 int num_bulk_out
= 0;
499 struct qt_get_device_data DeviceData
;
502 mydbg("In %s\n", __func__
);
504 /* let's find the endpoints needed */
505 /* check out the endpoints */
506 iface_desc
= interface
->cur_altsetting
;;
507 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
508 endpoint
= &iface_desc
->endpoint
[i
].desc
;
510 if ((endpoint
->bEndpointAddress
& 0x80) &&
511 ((endpoint
->bmAttributes
& 3) == 0x02)) {
512 /* we found a bulk in endpoint */
513 mydbg("found bulk in");
514 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
518 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
519 ((endpoint
->bmAttributes
& 3) == 0x02)) {
520 /* we found a bulk out endpoint */
521 mydbg("found bulk out\n");
522 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
526 if ((endpoint
->bEndpointAddress
& 0x80) &&
527 ((endpoint
->bmAttributes
& 3) == 0x03)) {
528 /* we found a interrupt in endpoint */
529 mydbg("found interrupt in\n");
530 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
535 /* found all that we need */
536 dev_info(&interface
->dev
, "Quatech converter detected\n");
537 num_ports
= num_bulk_out
;
538 if (num_ports
== 0) {
539 err("Quatech device with no bulk out, not allowed.");
544 serial
= get_free_serial(num_ports
, &minor
);
545 if (serial
== NULL
) {
546 err("No more free serial devices");
551 serial
->interface
= interface
;
552 serial
->minor
= minor
;
553 serial
->num_ports
= num_ports
;
554 serial
->num_bulk_in
= num_bulk_in
;
555 serial
->num_bulk_out
= num_bulk_out
;
556 serial
->num_interrupt_in
= num_interrupt_in
;
557 serial
->vendor
= dev
->descriptor
.idVendor
;
558 serial
->product
= dev
->descriptor
.idProduct
;
560 /* set up the endpoint information */
561 for (i
= 0; i
< num_bulk_in
; ++i
) {
562 endpoint
= bulk_in_endpoint
[i
];
563 port
= &serial
->port
[i
];
564 port
->read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
565 if (!port
->read_urb
) {
566 err("No free urbs available");
569 buffer_size
= endpoint
->wMaxPacketSize
;
570 port
->bulk_in_endpointAddress
= endpoint
->bEndpointAddress
;
571 port
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
572 port
->xfer_to_tty_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
573 if (!port
->bulk_in_buffer
) {
574 err("Couldn't allocate bulk_in_buffer");
577 usb_fill_bulk_urb(port
->read_urb
, dev
,
579 endpoint
->bEndpointAddress
),
580 port
->bulk_in_buffer
, buffer_size
,
581 qt_read_bulk_callback
, port
);
584 for (i
= 0; i
< num_bulk_out
; ++i
) {
585 endpoint
= bulk_out_endpoint
[i
];
586 port
= &serial
->port
[i
];
587 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
588 if (!port
->write_urb
) {
589 err("No free urbs available");
592 buffer_size
= endpoint
->wMaxPacketSize
;
593 port
->bulk_out_size
= buffer_size
;
594 port
->bulk_out_endpointAddress
= endpoint
->bEndpointAddress
;
595 port
->bulk_out_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
596 if (!port
->bulk_out_buffer
) {
597 err("Couldn't allocate bulk_out_buffer");
600 usb_fill_bulk_urb(port
->write_urb
, dev
,
602 endpoint
->bEndpointAddress
),
603 port
->bulk_out_buffer
, buffer_size
,
604 qt_write_bulk_callback
, port
);
608 /* For us numb of bulkin or out = number of ports */
609 mydbg("%s - setting up %d port structures for this device\n",
610 __func__
, num_bulk_in
);
611 for (i
= 0; i
< num_bulk_in
; ++i
) {
612 port
= &serial
->port
[i
];
613 port
->number
= i
+ serial
->minor
;
614 port
->serial
= serial
;
616 INIT_WORK(&port
->work
, port_softint
);
618 init_MUTEX(&port
->sem
);
621 status
= box_get_device(serial
, &DeviceData
);
623 mydbg(__FILE__
"box_get_device failed");
627 mydbg(__FILE__
"DeviceData.portb = 0x%x", DeviceData
.portb
);
629 DeviceData
.portb
&= ~FULLPWRBIT
;
630 mydbg(__FILE__
"Changing DeviceData.portb to 0x%x", DeviceData
.portb
);
632 status
= box_set_device(serial
, &DeviceData
);
634 mydbg(__FILE__
"box_set_device failed\n");
638 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
639 for (i
= 0; i
< serial
->num_ports
; ++i
) {
640 dev_info(&interface
->dev
,
641 "Converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
642 serial
->port
[i
].number
, serial
->port
[i
].number
);
645 /* usb_serial_console_init (debug, minor); */
647 /***********TAG add start next board here ****/
648 status
= box_get_device(serial
, &DeviceData
);
650 mydbg(__FILE__
"box_get_device failed");
654 * and before we power up lets initialiaze parnent device stuff here before
655 * we set thmem via any other method such as the property pages
657 switch (serial
->product
) {
658 case DEVICE_ID_QUATECH_RS232_SINGLE_PORT
:
659 case DEVICE_ID_QUATECH_RS232_DUAL_PORT
:
660 case DEVICE_ID_QUATECH_RS232_FOUR_PORT
:
661 case DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A
:
662 case DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B
:
663 case DEVICE_ID_QUATECH_RS232_16_PORT_A
:
664 case DEVICE_ID_QUATECH_RS232_16_PORT_B
:
665 case DEVICE_ID_QUATECH_RS232_16_PORT_C
:
666 case DEVICE_ID_QUATECH_RS232_16_PORT_D
:
667 DeviceData
.porta
&= ~(RR_BITS
| DUPMODE_BITS
);
668 DeviceData
.porta
|= CLKS_X4
;
669 DeviceData
.portb
&= ~(LOOPMODE_BITS
);
670 DeviceData
.portb
|= RS232_MODE
;
673 case DEVICE_ID_QUATECH_RS422_SINGLE_PORT
:
674 case DEVICE_ID_QUATECH_RS422_DUAL_PORT
:
675 case DEVICE_ID_QUATECH_RS422_FOUR_PORT
:
676 case DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A
:
677 case DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B
:
678 case DEVICE_ID_QUATECH_RS422_16_PORT_A
:
679 case DEVICE_ID_QUATECH_RS422_16_PORT_B
:
680 case DEVICE_ID_QUATECH_RS422_16_PORT_C
:
681 case DEVICE_ID_QUATECH_RS422_16_PORT_D
:
682 DeviceData
.porta
&= ~(RR_BITS
| DUPMODE_BITS
);
683 DeviceData
.porta
|= CLKS_X4
;
684 DeviceData
.portb
&= ~(LOOPMODE_BITS
);
685 DeviceData
.portb
|= ALL_LOOPBACK
;
688 DeviceData
.porta
&= ~(RR_BITS
| DUPMODE_BITS
);
689 DeviceData
.porta
|= CLKS_X4
;
690 DeviceData
.portb
&= ~(LOOPMODE_BITS
);
691 DeviceData
.portb
|= RS232_MODE
;
695 status
= BoxSetPrebufferLevel(serial
); /* sets to default vaue */
697 mydbg(__FILE__
"BoxSetPrebufferLevel failed\n");
701 status
= BoxSetATC(serial
, ATC_DISABLED
);
703 mydbg(__FILE__
"BoxSetATC failed\n");
706 /**********************************************************/
707 mydbg(__FILE__
"DeviceData.portb = 0x%x", DeviceData
.portb
);
709 DeviceData
.portb
|= NEXT_BOARD_POWER_BIT
;
710 mydbg(__FILE__
"Changing DeviceData.portb to 0x%x", DeviceData
.portb
);
712 status
= box_set_device(serial
, &DeviceData
);
714 mydbg(__FILE__
"box_set_device failed\n");
718 mydbg("Exit Success %s\n", __func__
);
720 usb_set_intfdata(interface
, serial
);
725 for (i
= 0; i
< num_bulk_in
; ++i
) {
726 port
= &serial
->port
[i
];
727 usb_free_urb(port
->read_urb
);
728 kfree(port
->bulk_in_buffer
);
730 for (i
= 0; i
< num_bulk_out
; ++i
) {
731 port
= &serial
->port
[i
];
732 usb_free_urb(port
->write_urb
);
733 kfree(port
->bulk_out_buffer
);
734 kfree(port
->xfer_to_tty_buffer
);
736 for (i
= 0; i
< num_interrupt_in
; ++i
) {
737 port
= &serial
->port
[i
];
738 usb_free_urb(port
->interrupt_in_urb
);
739 kfree(port
->interrupt_in_buffer
);
742 /* return the minor range that this device had */
743 return_serial(serial
);
744 mydbg("Exit fail %s\n", __func__
);
746 /* free up any memory that we allocated */
752 * returns the serial_table array pointers that are taken
753 * up in consecutive positions for each port to a common usb_serial structure
756 static void return_serial(struct usb_serial
*serial
)
760 mydbg("%s\n", __func__
);
765 for (i
= 0; i
< serial
->num_ports
; ++i
)
766 serial_table
[serial
->minor
+ i
] = NULL
;
772 * Finds the first locatio int the serial_table array where it can fit
773 * num_ports number of consecutive points to a common usb_serial
774 * structure,allocates a stucture points to it in all the structures, and
775 * returns the index to the first location in the array in the "minor"
778 static struct usb_serial
*get_free_serial(int num_ports
, int *minor
)
780 struct usb_serial
*serial
= NULL
;
784 mydbg("%s %d\n", __func__
, num_ports
);
787 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
793 * find a spot in the array where you can fit consecutive
794 * positions to put the pointers to the usb_serail allocated
795 * structure for all the minor numbers (ie. ports)
797 for (j
= 1; j
<= num_ports
- 1; ++j
)
798 if (serial_table
[i
+ j
])
803 serial
= kmalloc(sizeof(struct usb_serial
), GFP_KERNEL
);
805 err("%s - Out of memory", __func__
);
808 memset(serial
, 0, sizeof(struct usb_serial
));
809 serial_table
[i
] = serial
;
811 mydbg("%s - minor base = %d\n", __func__
, *minor
);
814 * copy in the pointer into the array starting a the *minor
815 * position minor is the index into the array.
818 (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
819 serial_table
[i
] = serial
;
825 static int flip_that(struct tty_struct
*tty
, __u16 index
,
826 struct usb_serial
*serial
)
828 tty_flip_buffer_push(tty
);
829 tty_schedule_flip(tty
);
833 /* Handles processing and moving data to the tty layer */
834 static void port_sofrint(void *private)
836 struct usb_serial_port
*port
= private;
837 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
838 struct tty_struct
*tty
= port
->tty
;
839 unsigned char *data
= port
->read_urb
->transfer_buffer
;
841 struct urb
*urb
= port
->read_urb
;
842 unsigned int RxCount
= urb
->actual_length
;
846 /* index = MINOR(port->tty->device) - serial->minor; */
847 index
= tty
->index
- serial
->minor
;
849 mydbg("%s - port %d\n", __func__
, port
->number
);
850 mydbg("%s - port->RxHolding = %d\n", __func__
, port
->RxHolding
);
852 if (port_paranoia_check(port
, __func__
) != 0) {
853 mydbg("%s - port_paranoia_check, exiting\n", __func__
);
854 port
->ReadBulkStopped
= 1;
859 mydbg("%s - bad serial pointer, exiting\n", __func__
);
862 if (port
->closePending
== 1) {
863 /* Were closing , stop reading */
864 mydbg("%s - (port->closepending == 1\n", __func__
);
865 port
->ReadBulkStopped
= 1;
870 * RxHolding is asserted by throttle, if we assert it, we're not
871 * receiving any more characters and let the box handle the flow
874 if (port
->RxHolding
== 1) {
875 port
->ReadBulkStopped
= 1;
880 port
->ReadBulkStopped
= 1;
882 mydbg("%s - nonzero read bulk status received: %d\n",
883 __func__
, urb
->status
);
888 mydbg("%s - port %d, tty =0x%p\n", __func__
, port
->number
, tty
);
890 if (tty
&& RxCount
) {
892 for (i
= 0; i
< RxCount
; ++i
) {
893 /* Look ahead code here */
894 if ((i
<= (RxCount
- 3)) && (THISCHAR
== 0x1b)
895 && (NEXTCHAR
== 0x1b)) {
899 /* Line status change 4th byte must follow */
900 if (i
> (RxCount
- 4)) {
901 mydbg("Illegal escape sequences in received data\n");
904 ProcessLineStatus(port
, FOURTHCHAR
);
910 /* Modem status status change 4th byte must follow */
911 mydbg("Modem status status. \n");
912 if (i
> (RxCount
- 4)) {
914 ("Illegal escape sequences in received data\n");
917 ProcessModemStatus(port
, FOURTHCHAR
);
922 mydbg("No status sequence. \n");
924 ProcessRxChar(port
, THISCHAR
);
925 ProcessRxChar(port
, NEXTCHAR
);
933 if (tty
&& urb
->actual_length
) {
934 tty_buffer_request_room(tty
, 1);
935 tty_insert_flip_string(tty
, (data
+ i
), 1);
939 tty_flip_buffer_push(tty
);
942 /* Continue trying to always read */
943 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
944 usb_rcvbulkpipe(serial
->dev
,
945 port
->bulk_in_endpointAddress
),
946 port
->read_urb
->transfer_buffer
,
947 port
->read_urb
->transfer_buffer_length
,
948 qt_read_bulk_callback
, port
);
949 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
951 mydbg("%s - failed resubmitting read urb, error %d",
955 flip_that(tty
, index
, serial
);
962 static void qt_read_bulk_callback(struct urb
*urb
)
965 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
968 port
->ReadBulkStopped
= 1;
969 mydbg("%s - nonzero write bulk status received: %d\n",
970 __func__
, urb
->status
);
974 port_sofrint((void *)port
);
975 schedule_work(&port
->work
);
978 static void ProcessRxChar(struct usb_serial_port
*port
, unsigned char Data
)
980 struct tty_struct
*tty
;
981 struct urb
*urb
= port
->read_urb
;
983 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
985 if (tty
&& urb
->actual_length
) {
986 tty_buffer_request_room(tty
, 1);
987 tty_insert_flip_string(tty
, &Data
, 1);
988 /* tty_flip_buffer_push(tty); */
994 static void ProcessLineStatus(struct usb_serial_port
*port
,
995 unsigned char line_status
)
999 line_status
& (SERIAL_LSR_OE
| SERIAL_LSR_PE
| SERIAL_LSR_FE
|
1004 static void ProcessModemStatus(struct usb_serial_port
*port
,
1005 unsigned char modem_status
)
1008 port
->shadowMSR
= modem_status
;
1009 wake_up_interruptible(&port
->wait
);
1013 static void serqt_usb_disconnect(struct usb_interface
*interface
)
1015 struct usb_serial
*serial
= usb_get_intfdata(interface
);
1016 /* struct device *dev = &interface->dev; */
1017 struct usb_serial_port
*port
;
1020 mydbg("%s\n", __func__
);
1025 for (i
= 0; i
< serial
->num_ports
; ++i
)
1026 serial
->port
[i
].open_count
= 0;
1028 for (i
= 0; i
< serial
->num_bulk_in
; ++i
) {
1029 port
= &serial
->port
[i
];
1030 usb_unlink_urb(port
->read_urb
);
1031 usb_free_urb(port
->read_urb
);
1032 kfree(port
->bulk_in_buffer
);
1034 for (i
= 0; i
< serial
->num_bulk_out
; ++i
) {
1035 port
= &serial
->port
[i
];
1036 usb_unlink_urb(port
->write_urb
);
1037 usb_free_urb(port
->write_urb
);
1038 kfree(port
->bulk_out_buffer
);
1040 for (i
= 0; i
< serial
->num_interrupt_in
; ++i
) {
1041 port
= &serial
->port
[i
];
1042 usb_unlink_urb(port
->interrupt_in_urb
);
1043 usb_free_urb(port
->interrupt_in_urb
);
1044 kfree(port
->interrupt_in_buffer
);
1047 /* return the minor range that this device had */
1048 return_serial(serial
);
1050 /* free up any memory that we allocated */
1054 dev_info(&interface
->dev
, "device disconnected");
1059 static struct usb_serial
*get_serial_by_minor(unsigned int minor
)
1061 return serial_table
[minor
];
1064 /*****************************************************************************
1065 * Driver tty interface functions
1066 *****************************************************************************/
1067 static int serial_open(struct tty_struct
*tty
, struct file
*filp
)
1069 struct usb_serial
*serial
;
1070 struct usb_serial_port
*port
;
1071 unsigned int portNumber
;
1074 mydbg("%s\n", __func__
);
1076 /* initialize the pointer incase something fails */
1077 tty
->driver_data
= NULL
;
1079 /* get the serial object associated with this tty pointer */
1080 /* serial = get_serial_by_minor (MINOR(tty->device)); */
1082 /* get the serial object associated with this tty pointer */
1083 serial
= get_serial_by_minor(tty
->index
);
1085 if (serial_paranoia_check(serial
, __func__
))
1088 /* set up our port structure making the tty driver remember our port object, and us it */
1089 portNumber
= tty
->index
- serial
->minor
;
1090 port
= &serial
->port
[portNumber
];
1091 tty
->driver_data
= port
;
1097 if (port
->open_count
== 1) {
1098 port
->closePending
= 0;
1099 mydbg("%s port->closepending = 0\n", __func__
);
1101 port
->RxHolding
= 0;
1102 mydbg("%s port->RxHolding = 0\n", __func__
);
1104 retval
= qt_open(tty
, port
, filp
);
1108 port
->open_count
= 0;
1109 mydbg("%s returning port->closePending = %d\n", __func__
,
1110 port
->closePending
);
1116 /*****************************************************************************
1117 *device's specific driver functions
1118 *****************************************************************************/
1119 static int qt_open(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1122 struct usb_serial
*serial
= port
->serial
;
1125 struct qt_get_device_data DeviceData
;
1126 struct qt_open_channel_data ChannelData
;
1127 unsigned short default_divisor
= 0x30; /* gives 9600 baud rate */
1128 unsigned char default_LCR
= SERIAL_8_DATA
; /* 8, none , 1 */
1131 if (port_paranoia_check(port
, __func__
))
1134 mydbg("%s - port %d\n", __func__
, port
->number
);
1136 index
= tty
->index
- serial
->minor
;
1138 status
= box_get_device(serial
, &DeviceData
);
1140 mydbg(__FILE__
"box_get_device failed\n");
1143 serial
->num_OpenCount
++;
1144 mydbg("%s serial->num_OpenCount = %d\n", __func__
,
1145 serial
->num_OpenCount
);
1146 /* Open uart channel */
1148 /* Port specific setups */
1149 status
= BoxOPenCloseChannel(serial
, index
, 1, &ChannelData
);
1151 mydbg(__FILE__
"BoxOPenCloseChannel failed\n");
1154 mydbg(__FILE__
"BoxOPenCloseChannel completed.\n");
1156 port
->shadowLSR
= ChannelData
.line_status
&
1157 (SERIAL_LSR_OE
| SERIAL_LSR_PE
| SERIAL_LSR_FE
| SERIAL_LSR_BI
);
1159 port
->shadowMSR
= ChannelData
.modem_status
&
1160 (SERIAL_MSR_CTS
| SERIAL_MSR_DSR
| SERIAL_MSR_RI
| SERIAL_MSR_CD
);
1162 /* Set Baud rate to default and turn off (default)flow control here */
1163 status
= BoxSetUart(serial
, index
, default_divisor
, default_LCR
);
1165 mydbg(__FILE__
"BoxSetUart failed\n");
1168 mydbg(__FILE__
"BoxSetUart completed.\n");
1170 /* Put this here to make it responsive to stty and defauls set by the tty layer */
1171 qt_set_termios(tty
, port
, NULL
);
1173 /* Initialize the wait que head */
1174 init_waitqueue_head(&(port
->wait
));
1176 /* if we have a bulk endpoint, start reading from it */
1177 if (serial
->num_bulk_in
) {
1178 /* Start reading from the device */
1179 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
1180 usb_rcvbulkpipe(serial
->dev
,
1182 bulk_in_endpointAddress
),
1183 port
->read_urb
->transfer_buffer
,
1184 port
->read_urb
->transfer_buffer_length
,
1185 qt_read_bulk_callback
, port
);
1187 port
->ReadBulkStopped
= 0;
1189 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1192 err("%s - failed resubmitting read urb, error %d\n",
1194 port
->ReadBulkStopped
= 1;
1202 static void serial_close(struct tty_struct
*tty
, struct file
*filp
)
1204 struct usb_serial_port
*port
=
1206 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1213 mydbg("%s - port %d\n", __func__
, port
->number
);
1215 /* if disconnect beat us to the punch here, there's nothing to do */
1216 if (tty
->driver_data
) {
1217 if (!port
->open_count
) {
1218 mydbg("%s - port not opened\n", __func__
);
1223 if (port
->open_count
<= 0) {
1224 port
->closePending
= 1;
1225 mydbg("%s - port->closePending = 1\n", __func__
);
1228 qt_close(tty
, port
, filp
);
1229 port
->open_count
= 0;
1238 mydbg("%s - %d return\n", __func__
, port
->number
);
1242 static void qt_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1245 unsigned long jift
= jiffies
+ 10 * HZ
;
1247 struct usb_serial
*serial
= port
->serial
;
1251 struct qt_open_channel_data ChannelData
;
1255 mydbg("%s - port %d\n", __func__
, port
->number
);
1256 index
= tty
->index
- serial
->minor
;
1258 /* shutdown any bulk reads that might be going on */
1259 if (serial
->num_bulk_out
)
1260 usb_unlink_urb(port
->write_urb
);
1261 if (serial
->num_bulk_in
)
1262 usb_unlink_urb(port
->read_urb
);
1264 /* wait up to 30 seconds for transmitter to empty */
1266 status
= BoxGetRegister(serial
, index
, LINE_STATUS_REGISTER
, &lsr
);
1268 mydbg(__FILE__
"box_get_device failed\n");
1272 if ((lsr
& SERIAL_LSR_TEMT
)
1273 && (port
->ReadBulkStopped
== 1))
1278 while (jiffies
<= jift
);
1281 mydbg("%s - port %d timout of checking transmitter empty\n",
1282 __func__
, port
->number
);
1284 mydbg("%s - port %d checking transmitter empty succeded\n",
1285 __func__
, port
->number
);
1288 BoxGetRegister(serial
, index
, MODEM_CONTROL_REGISTER
,
1290 mydbg(__FILE__
"BoxGetRegister MCR = 0x%x.\n", mcr
);
1293 mcr
&= ~(SERIAL_MCR_DTR
| SERIAL_MCR_RTS
);
1294 /* status = BoxSetRegister(serial, index, MODEM_CONTROL_REGISTER, mcr); */
1297 /* Close uart channel */
1298 status
= BoxOPenCloseChannel(serial
, index
, 0, &ChannelData
);
1300 mydbg("%s - port %d BoxOPenCloseChannel failed.\n",
1301 __func__
, port
->number
);
1303 serial
->num_OpenCount
--;
1307 static int serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
1310 struct usb_serial_port
*port
= tty
->driver_data
;
1311 struct usb_serial
*serial
;
1312 int retval
= -EINVAL
;
1315 serial
= get_usb_serial(port
, __func__
);
1318 /* This can happen if we get disconnected a */
1319 if (port
->open_count
== 0)
1321 index
= tty
->index
- serial
->minor
;
1323 mydbg("%s - port %d, %d byte(s)\n", __func__
, port
->number
, count
);
1324 mydbg("%s - port->RxHolding = %d\n", __func__
, port
->RxHolding
);
1326 if (!port
->open_count
) {
1327 mydbg("%s - port not opened\n", __func__
);
1331 retval
= qt_write(tty
, port
, buf
, count
);
1337 static int qt_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1338 const unsigned char *buf
, int count
)
1342 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1347 mydbg("%s - port %d\n", __func__
, port
->number
);
1350 mydbg("%s - write request of 0 bytes\n", __func__
);
1354 index
= tty
->index
- serial
->minor
;
1355 /* only do something if we have a bulk out endpoint */
1356 if (serial
->num_bulk_out
) {
1357 if (port
->write_urb
->status
== -EINPROGRESS
) {
1358 mydbg("%s - already writing\n", __func__
);
1363 (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
1364 memcpy(port
->write_urb
->transfer_buffer
, buf
, count
);
1366 /* usb_serial_debug_data(__FILE__, __func__, count, port->write_urb->transfer_buffer); */
1368 /* set up our urb */
1370 usb_fill_bulk_urb(port
->write_urb
, serial
->dev
,
1371 usb_sndbulkpipe(serial
->dev
,
1373 bulk_out_endpointAddress
),
1374 port
->write_urb
->transfer_buffer
, count
,
1375 qt_write_bulk_callback
, port
);
1377 /* send the data out the bulk port */
1378 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
1380 mydbg("%s - failed submitting write urb, error %d\n",
1388 /* no bulk out, so return 0 bytes written */
1392 static void qt_write_bulk_callback(struct urb
*urb
)
1394 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1395 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1397 mydbg("%s - port %d\n", __func__
, port
->number
);
1400 mydbg("%s - bad serial pointer, exiting\n", __func__
);
1405 mydbg("%s - nonzero write bulk status received: %d\n",
1406 __func__
, urb
->status
);
1409 port_softint(&port
->work
);
1410 schedule_work(&port
->work
);
1415 static void port_softint(struct work_struct
*work
)
1417 struct usb_serial_port
*port
=
1418 container_of(work
, struct usb_serial_port
, work
);
1419 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1420 struct tty_struct
*tty
;
1422 mydbg("%s - port %d\n", __func__
, port
->number
);
1431 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
))
1432 && tty
->ldisc
.write_wakeup
) {
1433 mydbg("%s - write wakeup call.\n", __func__
);
1434 (tty
->ldisc
.write_wakeup
) (tty
);
1438 wake_up_interruptible(&tty
->write_wait
);
1440 static int serial_write_room(struct tty_struct
*tty
)
1442 struct usb_serial_port
*port
= tty
->driver_data
;
1443 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1444 int retval
= -EINVAL
;
1451 mydbg("%s - port %d\n", __func__
, port
->number
);
1453 if (!port
->open_count
) {
1454 mydbg("%s - port not open\n", __func__
);
1458 retval
= qt_write_room(port
);
1464 static int qt_write_room(struct usb_serial_port
*port
)
1466 struct usb_serial
*serial
= port
->serial
;
1468 if (port
->closePending
== 1) {
1469 mydbg("%s - port->closePending == 1\n", __func__
);
1473 mydbg("%s - port %d\n", __func__
, port
->number
);
1475 if (serial
->num_bulk_out
) {
1476 if (port
->write_urb
->status
!= -EINPROGRESS
)
1477 room
= port
->bulk_out_size
;
1480 mydbg("%s - returns %d\n", __func__
, room
);
1483 static int serial_chars_in_buffer(struct tty_struct
*tty
)
1485 struct usb_serial_port
*port
= tty
->driver_data
;
1486 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1487 int retval
= -EINVAL
;
1494 mydbg("%s = port %d\n", __func__
, port
->number
);
1496 if (!port
->open_count
) {
1497 mydbg("%s - port not open\n", __func__
);
1501 retval
= qt_chars_in_buffer(port
);
1508 static int qt_chars_in_buffer(struct usb_serial_port
*port
)
1510 struct usb_serial
*serial
= port
->serial
;
1513 mydbg("%s - port %d\n", __func__
, port
->number
);
1515 if (serial
->num_bulk_out
) {
1516 if (port
->write_urb
->status
== -EINPROGRESS
)
1517 chars
= port
->write_urb
->transfer_buffer_length
;
1520 mydbg("%s - returns %d\n", __func__
, chars
);
1524 static int serial_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1525 unsigned int set
, unsigned int clear
)
1528 struct usb_serial_port
*port
= tty
->driver_data
;
1529 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1530 int retval
= -ENODEV
;
1532 mydbg("In %s \n", __func__
);
1537 index
= tty
->index
- serial
->minor
;
1541 mydbg("%s - port %d \n", __func__
, port
->number
);
1542 mydbg("%s - port->RxHolding = %d\n", __func__
, port
->RxHolding
);
1544 if (!port
->open_count
) {
1545 mydbg("%s - port not open\n", __func__
);
1549 retval
= qt_tiocmset(tty
, port
, file
, set
);
1556 static int qt_tiocmset(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1557 struct file
*file
, unsigned int value
)
1563 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1568 mydbg("%s - port %d\n", __func__
, port
->number
);
1570 /**************************************************************************************/
1573 index
= tty
->index
- serial
->minor
;
1575 BoxGetRegister(port
->serial
, index
, MODEM_CONTROL_REGISTER
,
1581 * Turn off the RTS and DTR and loopbcck and then only turn on what was
1584 mcr
&= ~(SERIAL_MCR_RTS
| SERIAL_MCR_DTR
| SERIAL_MCR_LOOP
);
1585 if (value
& TIOCM_RTS
)
1586 mcr
|= SERIAL_MCR_RTS
;
1587 if (value
& TIOCM_DTR
)
1588 mcr
|= SERIAL_MCR_DTR
;
1589 if (value
& TIOCM_LOOP
)
1590 mcr
|= SERIAL_MCR_LOOP
;
1593 BoxSetRegister(port
->serial
, index
, MODEM_CONTROL_REGISTER
,
1601 static int serial_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1604 struct usb_serial_port
*port
= tty
->driver_data
;
1605 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1606 int retval
= -ENODEV
;
1608 mydbg("In %s \n", __func__
);
1613 index
= tty
->index
- serial
->minor
;
1617 mydbg("%s - port %d\n", __func__
, port
->number
);
1618 mydbg("%s - port->RxHolding = %d\n", __func__
, port
->RxHolding
);
1620 if (!port
->open_count
) {
1621 mydbg("%s - port not open\n", __func__
);
1625 retval
= qt_tiocmget(tty
, port
, file
);
1632 static int qt_tiocmget(struct tty_struct
*tty
,
1633 struct usb_serial_port
*port
, struct file
*file
)
1638 unsigned int result
= 0;
1642 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1646 mydbg("%s - port %d, tty =0x%p\n", __func__
, port
->number
, tty
);
1648 /**************************************************************************************/
1651 index
= tty
->index
- serial
->minor
;
1653 BoxGetRegister(port
->serial
, index
, MODEM_CONTROL_REGISTER
,
1657 BoxGetRegister(port
->serial
, index
,
1658 MODEM_STATUS_REGISTER
, &msr
);
1663 result
= ((mcr
& SERIAL_MCR_DTR
) ? TIOCM_DTR
: 0)
1665 | ((mcr
& SERIAL_MCR_RTS
) ? TIOCM_RTS
: 0)
1667 | ((msr
& SERIAL_MSR_CTS
) ? TIOCM_CTS
: 0)
1669 | ((msr
& SERIAL_MSR_CD
) ? TIOCM_CAR
: 0)
1670 /* Carrier detect is set */
1671 | ((msr
& SERIAL_MSR_RI
) ? TIOCM_RI
: 0)
1672 /* Ring indicator set */
1673 | ((msr
& SERIAL_MSR_DSR
) ? TIOCM_DSR
: 0);
1681 static int serial_ioctl(struct tty_struct
*tty
, struct file
*file
,
1682 unsigned int cmd
, unsigned long arg
)
1685 struct usb_serial_port
*port
= tty
->driver_data
;
1686 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1687 int retval
= -ENODEV
;
1689 mydbg("In %s \n", __func__
);
1694 index
= tty
->index
- serial
->minor
;
1698 mydbg("%s - port %d, cmd 0x%.4x\n", __func__
, port
->number
, cmd
);
1699 mydbg("%s - port->RxHolding = %d\n", __func__
, port
->RxHolding
);
1701 if (!port
->open_count
) {
1702 mydbg("%s - port not open\n", __func__
);
1706 retval
= qt_ioctl(tty
, port
, file
, cmd
, arg
);
1712 static int qt_ioctl(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1713 struct file
*file
, unsigned int cmd
, unsigned long arg
)
1717 unsigned short prev_msr
;
1718 unsigned int value
, result
= 0;
1722 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1726 mydbg("%s - port %d, tty =0x%p\n", __func__
, port
->number
, tty
);
1729 index
= tty
->index
- serial
->minor
;
1731 if (cmd
== TIOCMIWAIT
) {
1732 DECLARE_WAITQUEUE(wait
, current
);
1733 prev_msr
= port
->shadowMSR
& SERIAL_MSR_MASK
;
1735 add_wait_queue(&port
->wait
, &wait
);
1736 set_current_state(TASK_INTERRUPTIBLE
);
1738 remove_wait_queue(&port
->wait
, &wait
);
1739 /* see if a signal woke us up */
1740 if (signal_pending(current
))
1741 return -ERESTARTSYS
;
1742 msr
= port
->shadowMSR
& SERIAL_MSR_MASK
;
1743 if (msr
== prev_msr
)
1744 return -EIO
; /* no change error */
1746 if ((arg
& TIOCM_RNG
1747 && ((prev_msr
& SERIAL_MSR_RI
) ==
1748 (msr
& SERIAL_MSR_RI
)))
1750 && ((prev_msr
& SERIAL_MSR_DSR
) ==
1751 (msr
& SERIAL_MSR_DSR
)))
1753 && ((prev_msr
& SERIAL_MSR_CD
) ==
1754 (msr
& SERIAL_MSR_CD
)))
1756 && ((prev_msr
& SERIAL_MSR_CTS
) ==
1757 (msr
& SERIAL_MSR_CTS
)))) {
1764 mydbg("%s -No ioctl for that one. port = %d\n", __func__
,
1767 return -ENOIOCTLCMD
;
1770 static void serial_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1772 struct usb_serial_port
*port
=
1774 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
1781 mydbg("%s - port %d\n", __func__
, port
->number
);
1783 if (!port
->open_count
) {
1784 mydbg("%s - port not open\n", __func__
);
1788 /* pass on to the driver specific version of this function if it is available */
1789 qt_set_termios(tty
, port
, old
);
1795 static void qt_set_termios(struct tty_struct
*tty
,
1796 struct usb_serial_port
*port
,
1797 struct ktermios
*old_termios
)
1800 int baud
, divisor
, remainder
;
1801 unsigned char new_LCR
= 0;
1803 struct usb_serial
*serial
;
1807 mydbg("%s - port %d\n", __func__
, port
->number
);
1809 tmp
= port
->tty
->index
;
1810 mydbg("%s - MINOR(port->tty->index) = %d\n", __func__
, tmp
);
1812 serial
= port
->serial
;
1813 tmp2
= serial
->minor
;
1814 mydbg("%s - serial->minor = %d\n", __func__
, tmp2
);
1816 index
= port
->tty
->index
- serial
->minor
;
1818 cflag
= tty
->termios
->c_cflag
;
1820 mydbg("%s - 3\n", __func__
);
1824 new_LCR
|= SERIAL_5_DATA
;
1827 new_LCR
|= SERIAL_6_DATA
;
1830 new_LCR
|= SERIAL_7_DATA
;
1834 new_LCR
|= SERIAL_8_DATA
;
1839 if (cflag
& PARENB
) {
1841 new_LCR
|= SERIAL_ODD_PARITY
;
1843 new_LCR
|= SERIAL_EVEN_PARITY
;
1846 new_LCR
|= SERIAL_TWO_STOPB
;
1848 new_LCR
|= SERIAL_TWO_STOPB
;
1850 mydbg("%s - 4\n", __func__
);
1851 /* Thats the LCR stuff, go ahead and set it */
1852 baud
= tty_get_baud_rate(tty
);
1854 /* pick a default, any default... */
1857 mydbg("%s - got baud = %d\n", __func__
, baud
);
1859 divisor
= MAX_BAUD_RATE
/ baud
;
1860 remainder
= MAX_BAUD_RATE
% baud
;
1861 /* Round to nearest divisor */
1862 if (((remainder
* 2) >= baud
) && (baud
!= 110))
1866 * Set Baud rate to default and turn off (default)flow control here
1868 status
= BoxSetUart(serial
, index
, (unsigned short)divisor
, new_LCR
);
1870 mydbg(__FILE__
"BoxSetUart failed\n");
1874 /* Now determine flow control */
1875 if (cflag
& CRTSCTS
) {
1876 mydbg("%s - Enabling HW flow control port %d\n", __func__
,
1879 /* Enable RTS/CTS flow control */
1880 status
= BoxSetHW_FlowCtrl(serial
, index
, 1);
1883 mydbg(__FILE__
"BoxSetHW_FlowCtrl failed\n");
1887 /* Disable RTS/CTS flow control */
1888 mydbg("%s - disabling HW flow control port %d\n", __func__
,
1891 status
= BoxSetHW_FlowCtrl(serial
, index
, 0);
1893 mydbg(__FILE__
"BoxSetHW_FlowCtrl failed\n");
1899 /* if we are implementing XON/XOFF, set the start and stop character in
1901 if (I_IXOFF(tty
) || I_IXON(tty
)) {
1902 unsigned char stop_char
= STOP_CHAR(tty
);
1903 unsigned char start_char
= START_CHAR(tty
);
1905 BoxSetSW_FlowCtrl(serial
, index
, stop_char
,
1908 mydbg(__FILE__
"BoxSetSW_FlowCtrl (enabled) failed\n");
1911 /* disable SW flow control */
1912 status
= BoxDisable_SW_FlowCtrl(serial
, index
);
1914 mydbg(__FILE__
"BoxSetSW_FlowCtrl (diabling) failed\n");
1917 tty
->termios
->c_cflag
&= ~CMSPAR
;
1918 /* FIXME: Error cases should be returning the actual bits changed only */
1921 /****************************************************************************
1923 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
1924 * If successful, fills in the pValue with the register value asked for
1925 ****************************************************************************/
1926 static int BoxGetRegister(struct usb_serial
*serial
, unsigned short Uart_Number
,
1927 unsigned short Register_Num
, __u8
*pValue
)
1930 __u16 current_length
;
1932 current_length
= sizeof(struct qt_get_device_data
);
1935 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
1936 QT_GET_SET_REGISTER
, 0xC0, Register_Num
,
1937 Uart_Number
, (void *)pValue
, sizeof(*pValue
), 300);
1942 /****************************************************************************
1944 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
1945 * If successful, fills in the pValue with the register value asked for
1946 ****************************************************************************/
1947 static int BoxSetRegister(struct usb_serial
*serial
, unsigned short Uart_Number
,
1948 unsigned short Register_Num
, unsigned short Value
)
1951 unsigned short RegAndByte
;
1954 RegAndByte
= RegAndByte
<< 8;
1955 RegAndByte
= RegAndByte
+ Register_Num
;
1958 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1959 QT_GET_SET_REGISTER, 0xC0, Register_Num,
1960 Uart_Number, NULL, 0, 300);
1964 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1965 QT_GET_SET_REGISTER
, 0x40, RegAndByte
, Uart_Number
,
1973 * Issue a GET_DEVICE vendor-specific request on the default control pipe If
1974 * successful, fills in the qt_get_device_data structure pointed to by
1975 * device_data, otherwise return a negative error number of the problem.
1977 static int box_get_device(struct usb_serial
*serial
,
1978 struct qt_get_device_data
*device_data
)
1981 __u16 current_length
;
1982 unsigned char *transfer_buffer
;
1984 current_length
= sizeof(struct qt_get_device_data
);
1985 transfer_buffer
= kmalloc(current_length
, GFP_KERNEL
);
1986 if (!transfer_buffer
)
1989 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
1990 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
1991 transfer_buffer
, current_length
, 300);
1993 memcpy(device_data
, transfer_buffer
, current_length
);
1994 kfree(transfer_buffer
);
2001 * Issue a SET_DEVICE vendor-specific request on the default control pipe If
2002 * successful returns the number of bytes written, otherwise it returns a
2003 * negative error number of the problem.
2005 static int box_set_device(struct usb_serial
*serial
,
2006 struct qt_get_device_data
*device_data
)
2012 PortSettings
= ((__u16
) (device_data
->portb
));
2013 PortSettings
= (PortSettings
<< 8);
2014 PortSettings
+= ((__u16
) (device_data
->porta
));
2016 length
= sizeof(struct qt_get_device_data
);
2017 mydbg("%s - PortSettings = 0x%x\n", __func__
, PortSettings
);
2019 result
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2020 QT_SET_GET_DEVICE
, 0x40, PortSettings
,
2025 /****************************************************************************
2026 * BoxOPenCloseChannel
2027 * This funciotn notifies the device that the device driver wishes to open a particular UART channel. its
2028 * purpose is to allow the device driver and the device to synchronize state information.
2029 * OpenClose = 1 for open , 0 for close
2030 ****************************************************************************/
2031 static int BoxOPenCloseChannel(struct usb_serial
*serial
, __u16 Uart_Number
,
2033 struct qt_open_channel_data
*pDeviceData
)
2039 length
= sizeof(struct qt_open_channel_data
);
2042 if (OpenClose
== 1) {
2043 Direcion
= USBD_TRANSFER_DIRECTION_IN
;
2044 pipe
= usb_rcvctrlpipe(serial
->dev
, 0);
2046 usb_control_msg(serial
->dev
, pipe
, QT_OPEN_CLOSE_CHANNEL
,
2047 Direcion
, OpenClose
, Uart_Number
,
2048 pDeviceData
, length
, 300);
2051 Direcion
= USBD_TRANSFER_DIRECTION_OUT
;
2052 pipe
= usb_sndctrlpipe(serial
->dev
, 0);
2054 usb_control_msg(serial
->dev
, pipe
, QT_OPEN_CLOSE_CHANNEL
,
2055 Direcion
, OpenClose
, Uart_Number
, NULL
, 0,
2063 /****************************************************************************
2064 * BoxSetPrebufferLevel
2065 TELLS BOX WHEN TO ASSERT FLOW CONTROL
2066 ****************************************************************************/
2067 static int BoxSetPrebufferLevel(struct usb_serial
*serial
)
2070 __u16 buffer_length
;
2072 buffer_length
= PREFUFF_LEVEL_CONSERVATIVE
;
2073 result
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2074 QT_GET_SET_PREBUF_TRIG_LVL
, 0x40,
2075 buffer_length
, 0, NULL
, 0, 300);
2079 /****************************************************************************
2081 TELLS BOX WHEN TO ASSERT automatic transmitter control
2082 ****************************************************************************/
2083 static int BoxSetATC(struct usb_serial
*serial
, __u16 n_Mode
)
2086 __u16 buffer_length
;
2088 buffer_length
= PREFUFF_LEVEL_CONSERVATIVE
;
2091 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2092 QT_SET_ATF
, 0x40, n_Mode
, 0, NULL
, 0, 300);
2097 /****************************************************************************
2099 * issuse a SET_UART vendor-spcific request on the default control pipe
2100 * If successful sets baud rate divisor and LCR value
2101 ****************************************************************************/
2102 static int BoxSetUart(struct usb_serial
*serial
, unsigned short Uart_Number
,
2103 unsigned short default_divisor
, unsigned char default_LCR
)
2106 unsigned short UartNumandLCR
;
2108 UartNumandLCR
= (default_LCR
<< 8) + Uart_Number
;
2111 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2112 QT_GET_SET_UART
, 0x40, default_divisor
,
2113 UartNumandLCR
, NULL
, 0, 300);
2118 static int BoxSetHW_FlowCtrl(struct usb_serial
*serial
, unsigned int index
,
2122 __u8 msr
= 0, MOUT_Value
= 0;
2123 struct usb_serial_port
*port
;
2124 unsigned int status
;
2126 port
= serial
->port
;
2129 /* flow control, box will clear RTS line to prevent remote */
2130 mcr
= SERIAL_MCR_RTS
;
2131 } /* device from xmitting more chars */
2133 /* no flow control to remote device */
2137 MOUT_Value
= mcr
<< 8;
2140 /* flow control, box will inhibit xmit data if CTS line is
2142 msr
= SERIAL_MSR_CTS
;
2144 /* Box will not inhimbe xmit data due to CTS line */
2150 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2151 QT_HW_FLOW_CONTROL_MASK
, 0x40, MOUT_Value
,
2152 index
, NULL
, 0, 300);
2157 static int BoxSetSW_FlowCtrl(struct usb_serial
*serial
, __u16 index
,
2158 unsigned char stop_char
, unsigned char start_char
)
2163 nSWflowout
= start_char
<< 8;
2164 nSWflowout
= (unsigned short)stop_char
;
2167 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2168 QT_SW_FLOW_CONTROL_MASK
, 0x40, nSWflowout
,
2169 index
, NULL
, 0, 300);
2173 static int BoxDisable_SW_FlowCtrl(struct usb_serial
*serial
, __u16 index
)
2178 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2179 QT_SW_FLOW_CONTROL_DISABLE
, 0x40, 0, index
,
2185 static void serial_throttle(struct tty_struct
*tty
)
2187 struct usb_serial_port
*port
=
2189 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
2190 mydbg("%s - port %d\n", __func__
, port
->number
);
2197 if (!port
->open_count
) {
2198 mydbg("%s - port not open\n", __func__
);
2201 /* shut down any bulk reads that may be going on */
2202 /* usb_unlink_urb (port->read_urb); */
2203 /* pass on to the driver specific version of this function */
2204 port
->RxHolding
= 1;
2205 mydbg("%s - port->RxHolding = 1\n", __func__
);
2212 static void serial_unthrottle(struct tty_struct
*tty
)
2214 struct usb_serial_port
*port
=
2216 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
2217 unsigned int result
;
2223 mydbg("%s - port %d\n", __func__
, port
->number
);
2225 if (!port
->open_count
) {
2226 mydbg("%s - port not open\n", __func__
);
2230 if (port
->RxHolding
== 1) {
2231 mydbg("%s -port->RxHolding == 1\n", __func__
);
2233 port
->RxHolding
= 0;
2234 mydbg("%s - port->RxHolding = 0\n", __func__
);
2236 /* if we have a bulk endpoint, start it up */
2237 if ((serial
->num_bulk_in
) && (port
->ReadBulkStopped
== 1)) {
2238 /* Start reading from the device */
2239 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
2240 usb_rcvbulkpipe(serial
->dev
,
2242 bulk_in_endpointAddress
),
2243 port
->read_urb
->transfer_buffer
,
2245 transfer_buffer_length
,
2246 qt_read_bulk_callback
, port
);
2247 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
2249 err("%s - failed restarting read urb, error %d",
2259 static int serial_break(struct tty_struct
*tty
, int break_state
)
2261 struct usb_serial_port
*port
= tty
->driver_data
;
2262 struct usb_serial
*serial
= get_usb_serial(port
, __func__
);
2264 unsigned int result
;
2266 index
= tty
->index
- serial
->minor
;
2270 if (break_state
== -1)
2277 mydbg("%s - port %d\n", __func__
, port
->number
);
2279 if (!port
->open_count
) {
2280 mydbg("%s - port not open\n", __func__
);
2285 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2286 QT_BREAK_CONTROL
, 0x40, onoff
, index
,
2294 static int ioctl_serial_usb(struct inode
*innod
, struct file
*filp
, unsigned int cmd
,
2299 unsigned ucOPR_NewValue
, uc_Value
;
2300 int *p_Num_of_adapters
, counts
, index
, *p_QMCR_Value
;
2301 struct identity
*p_Identity_of
;
2302 struct identity Identity_of
;
2303 struct usb_serial
*lastserial
, *serial
;
2305 mydbg(KERN_DEBUG
"ioctl_serial_usb cmd =\n");
2306 if (_IOC_TYPE(cmd
) != SERIALQT_PCI_IOC_MAGIC
)
2308 if (_IOC_NR(cmd
) > SERIALQT_IOC_MAXNR
)
2310 mydbg(KERN_DEBUG
"ioctl_serial_usb cmd = 0x%x\n", cmd
);
2314 case SERIALQT_WRITE_QMCR
:
2319 ucOPR_NewValue
= arg
;
2321 err
= EmulateWriteQMCR_Reg(index
, ucOPR_NewValue
);
2324 case SERIALQT_READ_QMCR
:
2326 p_QMCR_Value
= (int *)arg
;
2330 err
= EmulateReadQMCR_Reg(index
, &uc_Value
);
2332 err
= put_user(uc_Value
, p_QMCR_Value
);
2335 case SERIALQT_GET_NUMOF_UNITS
:
2336 p_Num_of_adapters
= (int *)arg
;
2337 counts
= 0; /* Initialize counts to zero */
2338 /* struct usb_serial *lastserial = serial_table[0], *serial; */
2339 lastserial
= serial_table
[0];
2341 mydbg(KERN_DEBUG
"SERIALQT_GET_NUMOF_UNITS \n");
2342 /* if first pointer is nonull, we at least have one box */
2344 counts
= 1; /* we at least have one box */
2346 for (index
= 1; index
< SERIAL_TTY_MINORS
; index
++) {
2347 serial
= serial_table
[index
];
2349 if (serial
!= lastserial
) {
2350 /* we had a change in the array, hence
2351 * another box is there */
2352 lastserial
= serial
;
2359 mydbg(KERN_DEBUG
"ioctl_serial_usb writting counts = %d",
2362 err
= put_user(counts
, p_Num_of_adapters
);
2365 case SERIALQT_GET_THIS_UNIT
:
2367 p_Identity_of
= (struct identity
*)arg
;
2368 /* copy user structure to local variable */
2369 get_user(Identity_of
.index
, &p_Identity_of
->index
);
2370 mydbg(KERN_DEBUG
"SERIALQT_GET_THIS_UNIT Identity_of.index\n");
2372 "SERIALQT_GET_THIS_UNIT Identity_of.index= 0x%x\n",
2376 serial
= find_the_box(Identity_of
.index
);
2379 put_user(serial
->product
,
2380 &p_Identity_of
->n_identity
);
2385 case SERIALQT_IS422_EXTENDED
:
2387 mydbg(KERN_DEBUG
"SERIALQT_IS422_EXTENDED \n");
2393 "SERIALQT_IS422_EXTENDED, looking Identity_of.indext = 0x%x\n",
2395 serial
= find_the_box(index
);
2397 mydbg("%s index = 0x%x, serial = 0x%p\n", __func__
,
2399 for (counts
= 0; serqt_422_table
[counts
] != 0; counts
++) {
2402 ("%s serial->product = = 0x%x, serqt_422_table[counts] = 0x%x\n",
2403 __func__
, serial
->product
,
2404 serqt_422_table
[counts
]);
2405 if (serial
->product
== serqt_422_table
[counts
]) {
2409 ("%s found match for 422extended\n",
2421 mydbg("%s returning err = 0x%x\n", __func__
, err
);
2425 static struct usb_serial
*find_the_box(unsigned int index
)
2427 struct usb_serial
*lastserial
, *foundserial
, *serial
;
2428 int counts
= 0, index2
;
2429 lastserial
= serial_table
[0];
2431 for (index2
= 0; index2
< SERIAL_TTY_MINORS
; index2
++) {
2432 serial
= serial_table
[index2
];
2434 mydbg("%s index = 0x%x, index2 = 0x%x, serial = 0x%p\n",
2435 __func__
, index
, index2
, serial
);
2438 /* first see if this is the unit we'er looking for */
2440 ("%s inside if(serial) counts = 0x%x , index = 0x%x\n",
2441 __func__
, counts
, index
);
2442 if (counts
== index
) {
2443 /* we found the one we're looking for, copythe
2444 * product Id to user */
2445 mydbg("%s we found the one we're looking for serial = 0x%p\n",
2447 foundserial
= serial
;
2451 if (serial
!= lastserial
) {
2452 /* when we have a change in the pointer */
2453 lastserial
= serial
;
2457 break; /* no matches */
2460 mydbg("%s returning foundserial = 0x%p\n", __func__
, foundserial
);
2464 static int EmulateWriteQMCR_Reg(int index
, unsigned uc_value
)
2468 struct usb_serial
*serial
;
2470 struct qt_get_device_data DeviceData
;
2471 unsigned uc_temp
= 0;
2472 mydbg("Inside %s, uc_value = 0x%x\n", __func__
, uc_value
);
2474 DeviceData
.porta
= 0;
2475 DeviceData
.portb
= 0;
2476 serial
= find_the_box(index
);
2477 /* Determine Duplex mode */
2480 status
= box_get_device(serial
, &DeviceData
);
2482 mydbg(__FILE__
"box_set_device failed\n");
2486 uc_temp
= uc_value
& QMCR_HALF_DUPLEX_MASK
;
2488 case QMCR_FULL_DUPLEX
:
2489 DeviceData
.porta
&= ~DUPMODE_BITS
;
2490 DeviceData
.porta
|= FULL_DUPLEX
;
2491 ATC_Mode
= ATC_DISABLED
;
2493 case QMCR_HALF_DUPLEX_RTS
:
2494 DeviceData
.porta
&= ~DUPMODE_BITS
;
2495 DeviceData
.porta
|= HALF_DUPLEX_RTS
;
2496 ATC_Mode
= ATC_RTS_ENABLED
;
2498 case QMCR_HALF_DUPLEX_DTR
:
2499 DeviceData
.porta
&= ~DUPMODE_BITS
;
2500 DeviceData
.porta
|= HALF_DUPLEX_DTR
;
2501 ATC_Mode
= ATC_DTR_ENABLED
;
2507 uc_temp
= uc_value
& QMCR_CONNECTOR_MASK
;
2509 case QMCR_MODEM_CONTROL
:
2510 DeviceData
.portb
&= ~LOOPMODE_BITS
; /* reset connection bits */
2511 DeviceData
.portb
|= MODEM_CTRL
;
2513 case QMCR_ALL_LOOPBACK
:
2514 DeviceData
.portb
&= ~LOOPMODE_BITS
; /* reset connection bits */
2515 DeviceData
.portb
|= ALL_LOOPBACK
;
2519 mydbg(__FILE__
"Calling box_set_device with failed\n");
2520 status
= box_set_device(serial
, &DeviceData
);
2522 mydbg(__FILE__
"box_set_device failed\n");
2526 /* This bit (otherwise unused) i'll used to detect whether ATC is
2528 if (uc_value
& QMCR_RX_EN_MASK
) {
2531 "calling BoxsetATC with DeviceData.porta = 0x%x and DeviceData.portb = 0x%x\n",
2532 DeviceData
.porta
, DeviceData
.portb
);
2533 status
= BoxSetATC(serial
, ATC_Mode
);
2535 mydbg(__FILE__
"BoxSetATC failed\n");
2541 "calling BoxsetATC with DeviceData.porta = 0x%x and DeviceData.portb = 0x%x\n",
2542 DeviceData
.porta
, DeviceData
.portb
);
2543 status
= BoxSetATC(serial
, ATC_DISABLED
);
2545 mydbg(__FILE__
"BoxSetATC failed\n");
2554 static int EmulateReadQMCR_Reg(int index
, unsigned *uc_value
)
2556 struct usb_serial
*serial
;
2558 struct qt_get_device_data DeviceData
;
2563 serial
= find_the_box(index
);
2567 status
= box_get_device(serial
, &DeviceData
);
2569 mydbg(__FILE__
"box_get_device failed\n");
2572 uc_temp
= DeviceData
.porta
& DUPMODE_BITS
;
2575 *uc_value
|= QMCR_FULL_DUPLEX
;
2577 case HALF_DUPLEX_RTS
:
2578 *uc_value
|= QMCR_HALF_DUPLEX_RTS
;
2580 case HALF_DUPLEX_DTR
:
2581 *uc_value
|= QMCR_HALF_DUPLEX_DTR
;
2587 /* I use this for ATC control se */
2588 uc_temp
= DeviceData
.portb
& LOOPMODE_BITS
;
2592 *uc_value
|= QMCR_ALL_LOOPBACK
;
2595 *uc_value
|= QMCR_MODEM_CONTROL
;
2605 static int __init
serqt_usb_init(void)
2610 mydbg("%s\n", __func__
);
2611 tty_set_operations(&serial_tty_driver
, &serial_ops
);
2612 result
= tty_register_driver(&serial_tty_driver
);
2614 mydbg("tty_register_driver failed error = 0x%x", result
);
2618 /* Initalize our global data */
2619 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
)
2620 serial_table
[i
] = NULL
;
2622 /* register this driver with the USB subsystem */
2623 result
= usb_register(&serqt_usb_driver
);
2625 err("usb_register failed for the " __FILE__
2626 " driver. Error number %d", result
);
2629 status
= 0; /* Dynamic assignment of major number */
2631 register_chrdev(status
, "SerialQT_USB", &serialqt_usb_fops
);
2632 if (major_number
< 0) {
2633 mydbg(KERN_DEBUG
"No devices found \n\n");
2636 mydbg(KERN_DEBUG
"SerQT_USB major number assignment = %d \n\n",
2639 printk(KERN_INFO DRIVER_DESC
" " DRIVER_VERSION
);
2643 static void __exit
serqt_usb_exit(void)
2645 /* deregister this driver with the USB subsystem */
2646 usb_deregister(&serqt_usb_driver
);
2647 tty_unregister_driver(&serial_tty_driver
);
2648 unregister_chrdev(major_number
, "SerialQT_USB");
2651 module_init(serqt_usb_init
);
2652 module_exit(serqt_usb_exit
);
2654 MODULE_AUTHOR(DRIVER_AUTHOR
);
2655 MODULE_DESCRIPTION(DRIVER_DESC
);
2656 MODULE_LICENSE("GPL");