]>
Commit | Line | Data |
---|---|---|
5ca0121f GKH |
1 | /* |
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 | |
4 | * | |
5 | */ | |
6 | ||
7 | #include <linux/sched.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/tty.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> | |
c23ee6c3 | 15 | #include <linux/uaccess.h> |
5ca0121f GKH |
16 | |
17 | /* Use our own dbg macro */ | |
c23ee6c3 GKH |
18 | /* #define DEBUG_ON */ |
19 | /* #undef dbg */ | |
5ca0121f | 20 | #ifdef DEBUG_ON |
a9ea2266 | 21 | #define mydbg(const...) printk(const) |
5ca0121f | 22 | #else |
a9ea2266 | 23 | #define mydbg(const...) |
5ca0121f GKH |
24 | #endif |
25 | ||
5ca0121f GKH |
26 | /* parity check flag */ |
27 | #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) | |
28 | ||
5ca0121f GKH |
29 | #define SERIAL_TTY_MAJOR 0 /* Nice legal number now */ |
30 | #define SERIAL_TTY_MINORS 255 /* loads of devices :) */ | |
31 | #define MAX_NUM_PORTS 8 /* The maximum number of ports one device can grab at once */ | |
32 | #define PREFUFF_LEVEL_CONSERVATIVE 128 | |
33 | #define ATC_DISABLED 0x00 | |
34 | ||
c23ee6c3 | 35 | #define RR_BITS 0x03 /* for clearing clock bits */ |
5ca0121f GKH |
36 | #define DUPMODE_BITS 0xc0 |
37 | ||
38 | #define RS232_MODE 0x00 | |
39 | #define RTSCTS_TO_CONNECTOR 0x40 | |
c23ee6c3 | 40 | #define CLKS_X4 0x02 |
5ca0121f | 41 | |
c23ee6c3 | 42 | #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */ |
5ca0121f GKH |
43 | #define ALL_LOOPBACK 0x01 |
44 | #define MODEM_CTRL 0x40 | |
45 | ||
5ca0121f GKH |
46 | #define THISCHAR data[i] |
47 | #define NEXTCHAR data[i + 1] | |
48 | #define THIRDCHAR data[i + 2] | |
49 | #define FOURTHCHAR data[i + 3] | |
50 | ||
c23ee6c3 GKH |
51 | /* |
52 | * Useful defintions for port A, Port B and Port C | |
53 | */ | |
5ca0121f GKH |
54 | #define FULLPWRBIT 0x00000080 |
55 | #define NEXT_BOARD_POWER_BIT 0x00000004 | |
56 | ||
5ca0121f GKH |
57 | #define SERIAL_LSR_OE 0x02 |
58 | #define SERIAL_LSR_PE 0x04 | |
59 | #define SERIAL_LSR_FE 0x08 | |
60 | #define SERIAL_LSR_BI 0x10 | |
61 | ||
62 | #define SERIAL_LSR_TEMT 0x40 | |
63 | ||
64 | #define DIV_LATCH_LS 0x00 | |
65 | #define XMT_HOLD_REGISTER 0x00 | |
66 | #define XVR_BUFFER_REGISTER 0x00 | |
67 | #define DIV_LATCH_MS 0x01 | |
68 | #define FIFO_CONTROL_REGISTER 0x02 | |
69 | #define LINE_CONTROL_REGISTER 0x03 | |
70 | #define MODEM_CONTROL_REGISTER 0x04 | |
71 | #define LINE_STATUS_REGISTER 0x05 | |
72 | #define MODEM_STATUS_REGISTER 0x06 | |
73 | ||
5ca0121f GKH |
74 | #define SERIAL_MCR_DTR 0x01 |
75 | #define SERIAL_MCR_RTS 0x02 | |
76 | #define SERIAL_MCR_LOOP 0x10 | |
77 | ||
78 | #define SERIAL_MSR_CTS 0x10 | |
79 | #define SERIAL_MSR_CD 0x80 | |
80 | #define SERIAL_MSR_RI 0x40 | |
81 | #define SERIAL_MSR_DSR 0x20 | |
82 | #define SERIAL_MSR_MASK 0xf0 | |
83 | ||
84 | #define SERIAL_8_DATA 0x03 | |
85 | #define SERIAL_7_DATA 0x02 | |
86 | #define SERIAL_6_DATA 0x01 | |
87 | #define SERIAL_5_DATA 0x00 | |
88 | ||
89 | #define SERIAL_ODD_PARITY 0X08 | |
90 | #define SERIAL_EVEN_PARITY 0X18 | |
91 | #define SERIAL_TWO_STOPB 0x04 | |
92 | #define SERIAL_ONE_STOPB 0x00 | |
93 | ||
94 | #define MAX_BAUD_RATE 460800 | |
95 | #define MAX_BAUD_REMAINDER 4608 | |
96 | ||
5ca0121f GKH |
97 | #define QT_SET_GET_DEVICE 0xc2 |
98 | #define QT_OPEN_CLOSE_CHANNEL 0xca | |
99 | #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc | |
100 | #define QT_SET_ATF 0xcd | |
101 | #define QT_GET_SET_REGISTER 0xc0 | |
102 | #define QT_GET_SET_UART 0xc1 | |
103 | #define QT_HW_FLOW_CONTROL_MASK 0xc5 | |
104 | #define QT_SW_FLOW_CONTROL_MASK 0xc6 | |
105 | #define QT_SW_FLOW_CONTROL_DISABLE 0xc7 | |
106 | #define QT_BREAK_CONTROL 0xc8 | |
107 | ||
108 | #define SERIALQT_PCI_IOC_MAGIC 'k' | |
109 | #define SERIALQT_WRITE_QOPR _IOW(SERIALQT_PCI_IOC_MAGIC, 0, int) | |
110 | #define SERIALQT_WRITE_QMCR _IOW(SERIALQT_PCI_IOC_MAGIC, 1, int) | |
111 | #define SERIALQT_GET_NUMOF_UNITS _IOR(SERIALQT_PCI_IOC_MAGIC, 2, void *) | |
112 | #define SERIALQT_GET_THIS_UNIT _IOR(SERIALQT_PCI_IOC_MAGIC, 3, void *) | |
113 | #define SERIALQT_READ_QOPR _IOR(SERIALQT_PCI_IOC_MAGIC, 4, int) | |
114 | #define SERIALQT_READ_QMCR _IOR(SERIALQT_PCI_IOC_MAGIC, 5, int) | |
c23ee6c3 | 115 | #define SERIALQT_IS422_EXTENDED _IOR(SERIALQT_PCI_IOC_MAGIC, 6, int) /* returns successful if 422 extended */ |
5ca0121f GKH |
116 | |
117 | #define USBD_TRANSFER_DIRECTION_IN 0xc0 | |
118 | #define USBD_TRANSFER_DIRECTION_OUT 0x40 | |
119 | ||
120 | #define ATC_DISABLED 0x00 | |
121 | #define ATC_RTS_ENABLED 0x02 | |
122 | #define ATC_DTR_ENABLED 0x01 | |
123 | ||
c23ee6c3 | 124 | #define RR_BITS 0x03 /* for clearing clock bits */ |
5ca0121f GKH |
125 | #define DUPMODE_BITS 0xc0 |
126 | ||
127 | #define FULL_DUPLEX 0x00 | |
128 | #define HALF_DUPLEX_RTS 0x40 | |
129 | #define HALF_DUPLEX_DTR 0x80 | |
130 | ||
131 | #define QMCR_FULL_DUPLEX 0x00 | |
132 | #define QMCR_HALF_DUPLEX_RTS 0x02 | |
133 | #define QMCR_HALF_DUPLEX_DTR 0x01 | |
134 | #define QMCR_HALF_DUPLEX_MASK 0x03 | |
135 | #define QMCR_CONNECTOR_MASK 0x1C | |
136 | ||
137 | #define QMCR_RX_EN_MASK 0x20 | |
138 | ||
139 | #define QMCR_ALL_LOOPBACK 0x10 | |
140 | #define QMCR_MODEM_CONTROL 0X00 | |
141 | ||
5ca0121f GKH |
142 | #define SERIALQT_IOC_MAXNR 6 |
143 | ||
144 | struct usb_serial_port { | |
a9ea2266 GKH |
145 | struct usb_serial *serial; /* pointer back to the owner of this port */ |
146 | struct tty_struct *tty; /* the coresponding tty for this port */ | |
147 | unsigned char number; | |
148 | char active; /* someone has this device open */ | |
149 | ||
150 | unsigned char *interrupt_in_buffer; | |
151 | struct urb *interrupt_in_urb; | |
152 | __u8 interrupt_in_endpointAddress; | |
153 | ||
154 | unsigned char *bulk_in_buffer; | |
155 | unsigned char *xfer_to_tty_buffer; | |
156 | struct urb *read_urb; | |
157 | __u8 bulk_in_endpointAddress; | |
158 | ||
159 | unsigned char *bulk_out_buffer; | |
160 | int bulk_out_size; | |
161 | struct urb *write_urb; | |
162 | __u8 bulk_out_endpointAddress; | |
163 | ||
164 | wait_queue_head_t write_wait; | |
165 | wait_queue_head_t wait; | |
166 | struct work_struct work; | |
167 | ||
168 | int open_count; /* number of times this port has been opened */ | |
169 | struct semaphore sem; /* locks this structure */ | |
170 | ||
171 | __u8 shadowLCR; /* last LCR value received */ | |
172 | __u8 shadowMCR; /* last MCR value received */ | |
173 | __u8 shadowMSR; /* last MSR value received */ | |
174 | __u8 shadowLSR; /* last LSR value received */ | |
175 | int RxHolding; | |
176 | char closePending; | |
177 | int ReadBulkStopped; | |
178 | ||
179 | void *private; /* data private to the specific port */ | |
5ca0121f GKH |
180 | }; |
181 | ||
182 | struct identity { | |
a9ea2266 GKH |
183 | int index; |
184 | int n_identity; | |
5ca0121f GKH |
185 | }; |
186 | ||
187 | struct usb_serial { | |
a9ea2266 GKH |
188 | struct usb_device *dev; |
189 | struct usb_interface *interface; /* the interface for this device */ | |
190 | struct tty_driver *tty_driver; /* the tty_driver for this device */ | |
191 | unsigned char minor; /* the starting minor number for this device */ | |
192 | unsigned char num_ports; /* the number of ports this device has */ | |
193 | char num_interrupt_in; /* number of interrupt in endpoints we have */ | |
194 | char num_bulk_in; /* number of bulk in endpoints we have */ | |
195 | char num_bulk_out; /* number of bulk out endpoints we have */ | |
196 | unsigned char num_OpenCount; /* the number of ports this device has */ | |
197 | ||
198 | __u16 vendor; /* vendor id of this device */ | |
199 | __u16 product; /* product id of this device */ | |
200 | struct usb_serial_port port[MAX_NUM_PORTS]; | |
201 | ||
202 | void *private; /* data private to the specific driver */ | |
5ca0121f GKH |
203 | }; |
204 | ||
a9ea2266 GKH |
205 | static inline int port_paranoia_check(struct usb_serial_port *port, |
206 | const char *function) | |
5ca0121f GKH |
207 | { |
208 | if (!port) { | |
209 | dbg("%s - port == NULL", function); | |
210 | return -1; | |
211 | } | |
212 | if (!port->serial) { | |
213 | dbg("%s - port->serial == NULL\n", function); | |
214 | return -1; | |
215 | } | |
216 | if (!port->tty) { | |
217 | dbg("%s - port->tty == NULL\n", function); | |
218 | return -1; | |
219 | } | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
5ca0121f | 224 | /* Inline functions to check the sanity of a pointer that is passed to us */ |
a9ea2266 GKH |
225 | static inline int serial_paranoia_check(struct usb_serial *serial, |
226 | const char *function) | |
5ca0121f GKH |
227 | { |
228 | if (!serial) { | |
229 | dbg("%s - serial == NULL\n", function); | |
230 | return -1; | |
231 | } | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
a9ea2266 GKH |
236 | static inline struct usb_serial *get_usb_serial(struct usb_serial_port *port, |
237 | const char *function) | |
5ca0121f GKH |
238 | { |
239 | /* if no port was specified, or it fails a paranoia check */ | |
240 | if (!port || | |
a9ea2266 GKH |
241 | port_paranoia_check(port, function) || |
242 | serial_paranoia_check(port->serial, function)) { | |
5ca0121f GKH |
243 | /* then say that we dont have a valid usb_serial thing, which will |
244 | * end up genrating -ENODEV return values */ | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | return port->serial; | |
249 | } | |
250 | ||
a9ea2266 | 251 | struct qt_get_device_data { |
5ca0121f GKH |
252 | __u8 porta; |
253 | __u8 portb; | |
254 | __u8 portc; | |
255 | }; | |
256 | ||
a9ea2266 | 257 | struct qt_open_channel_data { |
5ca0121f GKH |
258 | __u8 line_status; |
259 | __u8 modem_status; | |
260 | }; | |
261 | ||
a9ea2266 GKH |
262 | static void ProcessLineStatus(struct usb_serial_port *port, |
263 | unsigned char line_status); | |
264 | static void ProcessModemStatus(struct usb_serial_port *port, | |
265 | unsigned char modem_status); | |
5ca0121f | 266 | static void ProcessRxChar(struct usb_serial_port *port, unsigned char Data); |
a9ea2266 | 267 | static struct usb_serial *get_free_serial(int num_ports, int *minor); |
5ca0121f GKH |
268 | |
269 | static int serqt_probe(struct usb_interface *interface, | |
a9ea2266 | 270 | const struct usb_device_id *id); |
5ca0121f GKH |
271 | |
272 | static void serqt_usb_disconnect(struct usb_interface *interface); | |
a9ea2266 GKH |
273 | static int box_set_device(struct usb_serial *serial, |
274 | struct qt_get_device_data *pDeviceData); | |
275 | static int box_get_device(struct usb_serial *serial, | |
276 | struct qt_get_device_data *pDeviceData); | |
277 | static int serial_open(struct tty_struct *tty, struct file *filp); | |
278 | static void serial_close(struct tty_struct *tty, struct file *filp); | |
279 | static int serial_write_room(struct tty_struct *tty); | |
280 | static int serial_ioctl(struct tty_struct *tty, struct file *file, | |
281 | unsigned int cmd, unsigned long arg); | |
282 | static void serial_set_termios(struct tty_struct *tty, struct ktermios *old); | |
283 | static int serial_write(struct tty_struct *tty, const unsigned char *buf, | |
284 | int count); | |
285 | ||
286 | static void serial_throttle(struct tty_struct *tty); | |
287 | static void serial_unthrottle(struct tty_struct *tty); | |
288 | static int serial_break(struct tty_struct *tty, int break_state); | |
289 | static int serial_chars_in_buffer(struct tty_struct *tty); | |
a9ea2266 | 290 | |
0c212603 AC |
291 | static int qt_open(struct tty_struct *tty, struct usb_serial_port *port, |
292 | struct file *filp); | |
5ca0121f GKH |
293 | static int BoxSetPrebufferLevel(struct usb_serial *serial); |
294 | ||
295 | static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode); | |
a9ea2266 GKH |
296 | static int BoxSetUart(struct usb_serial *serial, unsigned short Uart_Number, |
297 | unsigned short default_divisor, | |
298 | unsigned char default_LCR); | |
299 | ||
300 | static int BoxOPenCloseChannel(struct usb_serial *serial, __u16 Uart_Number, | |
301 | __u16 OpenClose, | |
302 | struct qt_open_channel_data *pDeviceData); | |
0c212603 AC |
303 | static void qt_close(struct tty_struct *tty, struct usb_serial_port *port, |
304 | struct file *filp); | |
a9ea2266 | 305 | static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number, |
c23ee6c3 | 306 | unsigned short Register_Num, __u8 *pValue); |
a9ea2266 GKH |
307 | static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number, |
308 | unsigned short Register_Num, unsigned short Value); | |
5ca0121f | 309 | static void qt_write_bulk_callback(struct urb *urb); |
0c212603 | 310 | static int qt_write(struct tty_struct *tty, struct usb_serial_port *port, |
a9ea2266 | 311 | const unsigned char *buf, int count); |
5ca0121f | 312 | static void port_softint(struct work_struct *work); |
a9ea2266 GKH |
313 | static int qt_write_room(struct usb_serial_port *port); |
314 | static int qt_chars_in_buffer(struct usb_serial_port *port); | |
0c212603 AC |
315 | static int qt_ioctl(struct tty_struct *tty, struct usb_serial_port *port, |
316 | struct file *file, unsigned int cmd, unsigned long arg); | |
317 | static void qt_set_termios(struct tty_struct *tty, | |
318 | struct usb_serial_port *port, | |
a9ea2266 | 319 | struct ktermios *old_termios); |
0c212603 | 320 | static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index, |
a9ea2266 | 321 | int bSet); |
0c212603 | 322 | static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index); |
5ca0121f GKH |
323 | static int EmulateWriteQMCR_Reg(int index, unsigned uc_value); |
324 | static int EmulateReadQMCR_Reg(int index, unsigned *uc_value); | |
325 | static struct usb_serial *find_the_box(unsigned int index); | |
c23ee6c3 | 326 | static int ioctl_serial_usb(struct inode *innod, struct file *filp, unsigned int cmd, |
a9ea2266 | 327 | unsigned long arg); |
5ca0121f | 328 | |
a9ea2266 GKH |
329 | static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 Uart, |
330 | unsigned char stop_char, unsigned char start_char); | |
5ca0121f GKH |
331 | static void qt_read_bulk_callback(struct urb *urb); |
332 | ||
333 | static void port_sofrint(void *private); | |
334 | ||
335 | static void return_serial(struct usb_serial *serial); | |
336 | ||
a9ea2266 GKH |
337 | static int serial_tiocmset(struct tty_struct *tty, struct file *file, |
338 | unsigned int set, unsigned int clear); | |
5ca0121f GKH |
339 | static int serial_tiocmget(struct tty_struct *tty, struct file *file); |
340 | ||
0c212603 AC |
341 | static int qt_tiocmset(struct tty_struct *tty, struct usb_serial_port *port, |
342 | struct file *file, unsigned int value); | |
5ca0121f | 343 | |
0c212603 AC |
344 | static int qt_tiocmget(struct tty_struct *tty, struct usb_serial_port *port, |
345 | struct file *file); | |
5ca0121f GKH |
346 | |
347 | /* Version Information */ | |
348 | #define DRIVER_VERSION "v2.14" | |
349 | #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc" | |
350 | #define DRIVER_DESC "Quatech USB to Serial Driver" | |
351 | ||
c23ee6c3 GKH |
352 | #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ |
353 | #define DEVICE_ID_QUATECH_RS232_SINGLE_PORT 0xC020 /* SSU100 */ | |
354 | #define DEVICE_ID_QUATECH_RS422_SINGLE_PORT 0xC030 /* SSU200 */ | |
355 | #define DEVICE_ID_QUATECH_RS232_DUAL_PORT 0xC040 /* DSU100 */ | |
356 | #define DEVICE_ID_QUATECH_RS422_DUAL_PORT 0xC050 /* DSU200 */ | |
357 | #define DEVICE_ID_QUATECH_RS232_FOUR_PORT 0xC060 /* QSU100 */ | |
358 | #define DEVICE_ID_QUATECH_RS422_FOUR_PORT 0xC070 /* QSU200 */ | |
359 | #define DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A 0xC080 /* ESU100A */ | |
360 | #define DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B 0xC081 /* ESU100B */ | |
361 | #define DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A 0xC0A0 /* ESU200A */ | |
362 | #define DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B 0xC0A1 /* ESU200B */ | |
363 | #define DEVICE_ID_QUATECH_RS232_16_PORT_A 0xC090 /* HSU100A */ | |
364 | #define DEVICE_ID_QUATECH_RS232_16_PORT_B 0xC091 /* HSU100B */ | |
365 | #define DEVICE_ID_QUATECH_RS232_16_PORT_C 0xC092 /* HSU100C */ | |
366 | #define DEVICE_ID_QUATECH_RS232_16_PORT_D 0xC093 /* HSU100D */ | |
367 | #define DEVICE_ID_QUATECH_RS422_16_PORT_A 0xC0B0 /* HSU200A */ | |
368 | #define DEVICE_ID_QUATECH_RS422_16_PORT_B 0xC0B1 /* HSU200B */ | |
369 | #define DEVICE_ID_QUATECH_RS422_16_PORT_C 0xC0B2 /* HSU200C */ | |
370 | #define DEVICE_ID_QUATECH_RS422_16_PORT_D 0xC0B3 /* HSU200D */ | |
5ca0121f | 371 | |
5ca0121f | 372 | /* table of Quatech devices */ |
a9ea2266 GKH |
373 | static struct usb_device_id serqt_table[] = { |
374 | {USB_DEVICE | |
375 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_SINGLE_PORT)}, | |
376 | {USB_DEVICE | |
377 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_SINGLE_PORT)}, | |
378 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_DUAL_PORT)}, | |
379 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_DUAL_PORT)}, | |
380 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_FOUR_PORT)}, | |
381 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_FOUR_PORT)}, | |
382 | {USB_DEVICE | |
383 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A)}, | |
384 | {USB_DEVICE | |
385 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B)}, | |
386 | {USB_DEVICE | |
387 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A)}, | |
388 | {USB_DEVICE | |
389 | (USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B)}, | |
390 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_16_PORT_A)}, | |
391 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_16_PORT_B)}, | |
392 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_16_PORT_C)}, | |
393 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS232_16_PORT_D)}, | |
394 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_16_PORT_A)}, | |
395 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_16_PORT_B)}, | |
396 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_16_PORT_C)}, | |
397 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, DEVICE_ID_QUATECH_RS422_16_PORT_D)}, | |
398 | {} /* Terminating entry */ | |
5ca0121f | 399 | }; |
a9ea2266 | 400 | |
5ca0121f GKH |
401 | MODULE_DEVICE_TABLE(usb, serqt_table); |
402 | ||
403 | static int major_number; | |
a9ea2266 | 404 | static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */ |
5ca0121f GKH |
405 | |
406 | /* table of Quatech 422devices */ | |
407 | static unsigned int serqt_422_table[] = { | |
408 | DEVICE_ID_QUATECH_RS422_SINGLE_PORT, | |
409 | DEVICE_ID_QUATECH_RS422_DUAL_PORT, | |
410 | DEVICE_ID_QUATECH_RS422_FOUR_PORT, | |
411 | DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A, | |
412 | DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B, | |
413 | DEVICE_ID_QUATECH_RS422_16_PORT_A, | |
414 | DEVICE_ID_QUATECH_RS422_16_PORT_B, | |
415 | DEVICE_ID_QUATECH_RS422_16_PORT_C, | |
416 | DEVICE_ID_QUATECH_RS422_16_PORT_D, | |
a9ea2266 | 417 | 0 /* terminate with zero */ |
5ca0121f GKH |
418 | }; |
419 | ||
5ca0121f GKH |
420 | /* usb specific object needed to register this driver with the usb subsystem */ |
421 | static struct usb_driver serqt_usb_driver = { | |
a9ea2266 GKH |
422 | .name = "quatech-usb-serial", |
423 | .probe = serqt_probe, | |
424 | .disconnect = serqt_usb_disconnect, | |
425 | .id_table = serqt_table, | |
5ca0121f GKH |
426 | }; |
427 | ||
428 | static struct ktermios *serial_termios[SERIAL_TTY_MINORS]; | |
429 | static struct ktermios *serial_termios_locked[SERIAL_TTY_MINORS]; | |
430 | ||
431 | static const struct tty_operations serial_ops = { | |
a9ea2266 GKH |
432 | .open = serial_open, |
433 | .close = serial_close, | |
434 | .write = serial_write, | |
435 | .write_room = serial_write_room, | |
436 | .ioctl = serial_ioctl, | |
437 | .set_termios = serial_set_termios, | |
438 | .throttle = serial_throttle, | |
439 | .unthrottle = serial_unthrottle, | |
440 | .break_ctl = serial_break, | |
441 | .chars_in_buffer = serial_chars_in_buffer, | |
a9ea2266 GKH |
442 | .tiocmset = serial_tiocmset, |
443 | .tiocmget = serial_tiocmget, | |
5ca0121f GKH |
444 | }; |
445 | ||
446 | static struct tty_driver serial_tty_driver = { | |
a9ea2266 GKH |
447 | .magic = TTY_DRIVER_MAGIC, |
448 | .driver_name = "Quatech usb-serial", | |
449 | .name = "ttyQT_USB", | |
450 | .major = SERIAL_TTY_MAJOR, | |
451 | .minor_start = 0, | |
452 | .num = SERIAL_TTY_MINORS, | |
453 | .type = TTY_DRIVER_TYPE_SERIAL, | |
454 | .subtype = SERIAL_TYPE_NORMAL, | |
455 | .flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV, | |
456 | ||
457 | .termios = serial_termios, | |
458 | .termios_locked = serial_termios_locked, | |
459 | .init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL, | |
460 | ||
461 | .init_termios.c_iflag = ICRNL | IXON, | |
462 | .init_termios.c_oflag = OPOST, | |
463 | ||
464 | .init_termios.c_lflag = | |
465 | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN, | |
5ca0121f GKH |
466 | }; |
467 | ||
c23ee6c3 GKH |
468 | /* fops for parent device */ |
469 | static const struct file_operations serialqt_usb_fops = { | |
a9ea2266 | 470 | .ioctl = ioctl_serial_usb, |
5ca0121f GKH |
471 | }; |
472 | ||
5ca0121f GKH |
473 | /** |
474 | * serqt_probe | |
475 | * | |
476 | * Called by the usb core when a new device is connected that it thinks | |
477 | * this driver might be interested in. | |
478 | * | |
479 | */ | |
480 | static int serqt_probe(struct usb_interface *interface, | |
a9ea2266 | 481 | const struct usb_device_id *id) |
5ca0121f | 482 | { |
a9ea2266 GKH |
483 | struct usb_device *dev = interface_to_usbdev(interface); |
484 | struct usb_serial *serial = NULL; | |
485 | struct usb_serial_port *port; | |
486 | struct usb_endpoint_descriptor *endpoint; | |
487 | struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS]; | |
488 | struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS]; | |
489 | struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS]; | |
490 | int minor; | |
491 | int buffer_size; | |
492 | int i; | |
493 | struct usb_host_interface *iface_desc; | |
494 | int num_interrupt_in = 0; | |
495 | int num_bulk_in = 0; | |
496 | int num_bulk_out = 0; | |
497 | int num_ports; | |
498 | struct qt_get_device_data DeviceData; | |
499 | int status; | |
5ca0121f | 500 | |
c23ee6c3 | 501 | mydbg("In %s\n", __func__); |
5ca0121f | 502 | |
a9ea2266 GKH |
503 | /* let's find the endpoints needed */ |
504 | /* check out the endpoints */ | |
505 | iface_desc = interface->cur_altsetting;; | |
506 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
507 | endpoint = &iface_desc->endpoint[i].desc; | |
508 | ||
509 | if ((endpoint->bEndpointAddress & 0x80) && | |
510 | ((endpoint->bmAttributes & 3) == 0x02)) { | |
511 | /* we found a bulk in endpoint */ | |
512 | mydbg("found bulk in"); | |
513 | bulk_in_endpoint[num_bulk_in] = endpoint; | |
514 | ++num_bulk_in; | |
515 | } | |
516 | ||
517 | if (((endpoint->bEndpointAddress & 0x80) == 0x00) && | |
518 | ((endpoint->bmAttributes & 3) == 0x02)) { | |
519 | /* we found a bulk out endpoint */ | |
520 | mydbg("found bulk out\n"); | |
521 | bulk_out_endpoint[num_bulk_out] = endpoint; | |
522 | ++num_bulk_out; | |
523 | } | |
524 | ||
525 | if ((endpoint->bEndpointAddress & 0x80) && | |
526 | ((endpoint->bmAttributes & 3) == 0x03)) { | |
527 | /* we found a interrupt in endpoint */ | |
528 | mydbg("found interrupt in\n"); | |
529 | interrupt_in_endpoint[num_interrupt_in] = endpoint; | |
530 | ++num_interrupt_in; | |
531 | } | |
532 | } | |
533 | ||
534 | /* found all that we need */ | |
535 | dev_info(&interface->dev, "Quatech converter detected\n"); | |
536 | num_ports = num_bulk_out; | |
537 | if (num_ports == 0) { | |
538 | err("Quatech device with no bulk out, not allowed."); | |
539 | return -ENODEV; | |
540 | ||
541 | } | |
542 | ||
543 | serial = get_free_serial(num_ports, &minor); | |
544 | if (serial == NULL) { | |
545 | err("No more free serial devices"); | |
546 | return -ENODEV; | |
547 | } | |
548 | ||
549 | serial->dev = dev; | |
550 | serial->interface = interface; | |
551 | serial->minor = minor; | |
552 | serial->num_ports = num_ports; | |
553 | serial->num_bulk_in = num_bulk_in; | |
554 | serial->num_bulk_out = num_bulk_out; | |
555 | serial->num_interrupt_in = num_interrupt_in; | |
556 | serial->vendor = dev->descriptor.idVendor; | |
557 | serial->product = dev->descriptor.idProduct; | |
558 | ||
559 | /* set up the endpoint information */ | |
560 | for (i = 0; i < num_bulk_in; ++i) { | |
561 | endpoint = bulk_in_endpoint[i]; | |
562 | port = &serial->port[i]; | |
563 | port->read_urb = usb_alloc_urb(0, GFP_KERNEL); | |
564 | if (!port->read_urb) { | |
565 | err("No free urbs available"); | |
566 | goto probe_error; | |
567 | } | |
568 | buffer_size = endpoint->wMaxPacketSize; | |
569 | port->bulk_in_endpointAddress = endpoint->bEndpointAddress; | |
570 | port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); | |
571 | port->xfer_to_tty_buffer = kmalloc(buffer_size, GFP_KERNEL); | |
572 | if (!port->bulk_in_buffer) { | |
573 | err("Couldn't allocate bulk_in_buffer"); | |
574 | goto probe_error; | |
575 | } | |
576 | usb_fill_bulk_urb(port->read_urb, dev, | |
577 | usb_rcvbulkpipe(dev, | |
578 | endpoint->bEndpointAddress), | |
579 | port->bulk_in_buffer, buffer_size, | |
580 | qt_read_bulk_callback, port); | |
581 | } | |
582 | ||
583 | for (i = 0; i < num_bulk_out; ++i) { | |
584 | endpoint = bulk_out_endpoint[i]; | |
585 | port = &serial->port[i]; | |
586 | port->write_urb = usb_alloc_urb(0, GFP_KERNEL); | |
587 | if (!port->write_urb) { | |
588 | err("No free urbs available"); | |
589 | goto probe_error; | |
590 | } | |
591 | buffer_size = endpoint->wMaxPacketSize; | |
592 | port->bulk_out_size = buffer_size; | |
593 | port->bulk_out_endpointAddress = endpoint->bEndpointAddress; | |
594 | port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL); | |
595 | if (!port->bulk_out_buffer) { | |
596 | err("Couldn't allocate bulk_out_buffer"); | |
597 | goto probe_error; | |
598 | } | |
599 | usb_fill_bulk_urb(port->write_urb, dev, | |
600 | usb_sndbulkpipe(dev, | |
601 | endpoint->bEndpointAddress), | |
602 | port->bulk_out_buffer, buffer_size, | |
603 | qt_write_bulk_callback, port); | |
604 | ||
605 | } | |
606 | ||
c23ee6c3 | 607 | /* For us numb of bulkin or out = number of ports */ |
a9ea2266 | 608 | mydbg("%s - setting up %d port structures for this device\n", |
c23ee6c3 | 609 | __func__, num_bulk_in); |
a9ea2266 GKH |
610 | for (i = 0; i < num_bulk_in; ++i) { |
611 | port = &serial->port[i]; | |
612 | port->number = i + serial->minor; | |
613 | port->serial = serial; | |
614 | ||
615 | INIT_WORK(&port->work, port_softint); | |
616 | ||
617 | init_MUTEX(&port->sem); | |
618 | ||
619 | } | |
620 | status = box_get_device(serial, &DeviceData); | |
621 | if (status < 0) { | |
622 | mydbg(__FILE__ "box_get_device failed"); | |
623 | goto probe_error; | |
624 | } | |
625 | ||
626 | mydbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb); | |
627 | ||
628 | DeviceData.portb &= ~FULLPWRBIT; | |
629 | mydbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb); | |
630 | ||
631 | status = box_set_device(serial, &DeviceData); | |
632 | if (status < 0) { | |
633 | mydbg(__FILE__ "box_set_device failed\n"); | |
634 | goto probe_error; | |
635 | } | |
636 | ||
637 | /* initialize the devfs nodes for this device and let the user know what ports we are bound to */ | |
638 | for (i = 0; i < serial->num_ports; ++i) { | |
639 | dev_info(&interface->dev, | |
640 | "Converter now attached to ttyUSB%d (or usb/tts/%d for devfs)", | |
641 | serial->port[i].number, serial->port[i].number); | |
642 | } | |
5ca0121f | 643 | |
c23ee6c3 | 644 | /* usb_serial_console_init (debug, minor); */ |
5ca0121f | 645 | |
c23ee6c3 | 646 | /***********TAG add start next board here ****/ |
a9ea2266 GKH |
647 | status = box_get_device(serial, &DeviceData); |
648 | if (status < 0) { | |
649 | mydbg(__FILE__ "box_get_device failed"); | |
650 | goto probe_error; | |
651 | } | |
c23ee6c3 GKH |
652 | /* |
653 | * and before we power up lets initialiaze parnent device stuff here before | |
654 | * we set thmem via any other method such as the property pages | |
655 | */ | |
a9ea2266 GKH |
656 | switch (serial->product) { |
657 | case DEVICE_ID_QUATECH_RS232_SINGLE_PORT: | |
658 | case DEVICE_ID_QUATECH_RS232_DUAL_PORT: | |
659 | case DEVICE_ID_QUATECH_RS232_FOUR_PORT: | |
660 | case DEVICE_ID_QUATECH_RS232_EIGHT_PORT_A: | |
661 | case DEVICE_ID_QUATECH_RS232_EIGHT_PORT_B: | |
662 | case DEVICE_ID_QUATECH_RS232_16_PORT_A: | |
663 | case DEVICE_ID_QUATECH_RS232_16_PORT_B: | |
664 | case DEVICE_ID_QUATECH_RS232_16_PORT_C: | |
665 | case DEVICE_ID_QUATECH_RS232_16_PORT_D: | |
666 | DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); | |
667 | DeviceData.porta |= CLKS_X4; | |
668 | DeviceData.portb &= ~(LOOPMODE_BITS); | |
669 | DeviceData.portb |= RS232_MODE; | |
670 | break; | |
671 | ||
672 | case DEVICE_ID_QUATECH_RS422_SINGLE_PORT: | |
673 | case DEVICE_ID_QUATECH_RS422_DUAL_PORT: | |
674 | case DEVICE_ID_QUATECH_RS422_FOUR_PORT: | |
675 | case DEVICE_ID_QUATECH_RS422_EIGHT_PORT_A: | |
676 | case DEVICE_ID_QUATECH_RS422_EIGHT_PORT_B: | |
677 | case DEVICE_ID_QUATECH_RS422_16_PORT_A: | |
678 | case DEVICE_ID_QUATECH_RS422_16_PORT_B: | |
679 | case DEVICE_ID_QUATECH_RS422_16_PORT_C: | |
680 | case DEVICE_ID_QUATECH_RS422_16_PORT_D: | |
681 | DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); | |
682 | DeviceData.porta |= CLKS_X4; | |
683 | DeviceData.portb &= ~(LOOPMODE_BITS); | |
684 | DeviceData.portb |= ALL_LOOPBACK; | |
685 | break; | |
686 | default: | |
687 | DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); | |
688 | DeviceData.porta |= CLKS_X4; | |
689 | DeviceData.portb &= ~(LOOPMODE_BITS); | |
690 | DeviceData.portb |= RS232_MODE; | |
691 | break; | |
5ca0121f | 692 | |
a9ea2266 | 693 | } |
c23ee6c3 | 694 | status = BoxSetPrebufferLevel(serial); /* sets to default vaue */ |
a9ea2266 GKH |
695 | if (status < 0) { |
696 | mydbg(__FILE__ "BoxSetPrebufferLevel failed\n"); | |
697 | goto probe_error; | |
698 | } | |
5ca0121f | 699 | |
a9ea2266 GKH |
700 | status = BoxSetATC(serial, ATC_DISABLED); |
701 | if (status < 0) { | |
702 | mydbg(__FILE__ "BoxSetATC failed\n"); | |
703 | goto probe_error; | |
704 | } | |
c23ee6c3 | 705 | /**********************************************************/ |
a9ea2266 | 706 | mydbg(__FILE__ "DeviceData.portb = 0x%x", DeviceData.portb); |
5ca0121f | 707 | |
a9ea2266 GKH |
708 | DeviceData.portb |= NEXT_BOARD_POWER_BIT; |
709 | mydbg(__FILE__ "Changing DeviceData.portb to 0x%x", DeviceData.portb); | |
710 | ||
711 | status = box_set_device(serial, &DeviceData); | |
712 | if (status < 0) { | |
713 | mydbg(__FILE__ "box_set_device failed\n"); | |
714 | goto probe_error; | |
715 | } | |
5ca0121f | 716 | |
c23ee6c3 | 717 | mydbg("Exit Success %s\n", __func__); |
5ca0121f | 718 | |
a9ea2266 GKH |
719 | usb_set_intfdata(interface, serial); |
720 | return 0; | |
5ca0121f | 721 | |
a9ea2266 GKH |
722 | probe_error: |
723 | ||
724 | for (i = 0; i < num_bulk_in; ++i) { | |
725 | port = &serial->port[i]; | |
c23ee6c3 GKH |
726 | usb_free_urb(port->read_urb); |
727 | kfree(port->bulk_in_buffer); | |
a9ea2266 GKH |
728 | } |
729 | for (i = 0; i < num_bulk_out; ++i) { | |
730 | port = &serial->port[i]; | |
c23ee6c3 GKH |
731 | usb_free_urb(port->write_urb); |
732 | kfree(port->bulk_out_buffer); | |
733 | kfree(port->xfer_to_tty_buffer); | |
a9ea2266 GKH |
734 | } |
735 | for (i = 0; i < num_interrupt_in; ++i) { | |
736 | port = &serial->port[i]; | |
c23ee6c3 GKH |
737 | usb_free_urb(port->interrupt_in_urb); |
738 | kfree(port->interrupt_in_buffer); | |
a9ea2266 | 739 | } |
5ca0121f | 740 | |
a9ea2266 GKH |
741 | /* return the minor range that this device had */ |
742 | return_serial(serial); | |
c23ee6c3 | 743 | mydbg("Exit fail %s\n", __func__); |
5ca0121f | 744 | |
a9ea2266 GKH |
745 | /* free up any memory that we allocated */ |
746 | kfree(serial); | |
747 | return -EIO; | |
748 | } | |
5ca0121f | 749 | |
c23ee6c3 GKH |
750 | /* |
751 | * returns the serial_table array pointers that are taken | |
752 | * up in consecutive positions for each port to a common usb_serial structure | |
753 | * back to NULL | |
754 | */ | |
5ca0121f GKH |
755 | static void return_serial(struct usb_serial *serial) |
756 | { | |
a9ea2266 | 757 | int i; |
5ca0121f | 758 | |
c23ee6c3 | 759 | mydbg("%s\n", __func__); |
5ca0121f | 760 | |
a9ea2266 GKH |
761 | if (serial == NULL) |
762 | return; | |
5ca0121f | 763 | |
c23ee6c3 | 764 | for (i = 0; i < serial->num_ports; ++i) |
a9ea2266 | 765 | serial_table[serial->minor + i] = NULL; |
5ca0121f | 766 | |
a9ea2266 | 767 | return; |
5ca0121f GKH |
768 | } |
769 | ||
c23ee6c3 GKH |
770 | /* |
771 | * Finds the first locatio int the serial_table array where it can fit | |
772 | * num_ports number of consecutive points to a common usb_serial | |
773 | * structure,allocates a stucture points to it in all the structures, and | |
774 | * returns the index to the first location in the array in the "minor" | |
775 | * variable. | |
776 | */ | |
a9ea2266 | 777 | static struct usb_serial *get_free_serial(int num_ports, int *minor) |
5ca0121f | 778 | { |
a9ea2266 GKH |
779 | struct usb_serial *serial = NULL; |
780 | int i, j; | |
781 | int good_spot; | |
782 | ||
c23ee6c3 | 783 | mydbg("%s %d\n", __func__, num_ports); |
a9ea2266 GKH |
784 | |
785 | *minor = 0; | |
786 | for (i = 0; i < SERIAL_TTY_MINORS; ++i) { | |
787 | if (serial_table[i]) | |
788 | continue; | |
789 | ||
790 | good_spot = 1; | |
c23ee6c3 GKH |
791 | /* |
792 | * find a spot in the array where you can fit consecutive | |
793 | * positions to put the pointers to the usb_serail allocated | |
794 | * structure for all the minor numbers (ie. ports) | |
795 | */ | |
a9ea2266 GKH |
796 | for (j = 1; j <= num_ports - 1; ++j) |
797 | if (serial_table[i + j]) | |
798 | good_spot = 0; | |
799 | if (good_spot == 0) | |
800 | continue; | |
801 | ||
c23ee6c3 GKH |
802 | serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL); |
803 | if (!serial) { | |
804 | err("%s - Out of memory", __func__); | |
a9ea2266 GKH |
805 | return NULL; |
806 | } | |
807 | memset(serial, 0, sizeof(struct usb_serial)); | |
808 | serial_table[i] = serial; | |
809 | *minor = i; | |
c23ee6c3 GKH |
810 | mydbg("%s - minor base = %d\n", __func__, *minor); |
811 | ||
812 | /* | |
813 | * copy in the pointer into the array starting a the *minor | |
814 | * position minor is the index into the array. | |
815 | */ | |
a9ea2266 GKH |
816 | for (i = *minor + 1; |
817 | (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) | |
818 | serial_table[i] = serial; | |
819 | return serial; | |
820 | } | |
821 | return NULL; | |
5ca0121f GKH |
822 | } |
823 | ||
0c212603 | 824 | static int flip_that(struct tty_struct *tty, __u16 index, |
a9ea2266 | 825 | struct usb_serial *serial) |
5ca0121f | 826 | { |
a9ea2266 GKH |
827 | tty_flip_buffer_push(tty); |
828 | tty_schedule_flip(tty); | |
829 | return 0; | |
5ca0121f GKH |
830 | } |
831 | ||
c23ee6c3 | 832 | /* Handles processing and moving data to the tty layer */ |
5ca0121f GKH |
833 | static void port_sofrint(void *private) |
834 | { | |
0c212603 | 835 | struct usb_serial_port *port = private; |
c23ee6c3 | 836 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 GKH |
837 | struct tty_struct *tty = port->tty; |
838 | unsigned char *data = port->read_urb->transfer_buffer; | |
0c212603 | 839 | unsigned int index; |
a9ea2266 GKH |
840 | struct urb *urb = port->read_urb; |
841 | unsigned int RxCount = urb->actual_length; | |
842 | int i, result; | |
843 | int flag, flag_data; | |
5ca0121f | 844 | |
0c212603 AC |
845 | /* index = MINOR(port->tty->device) - serial->minor; */ |
846 | index = tty->index - serial->minor; | |
5ca0121f | 847 | |
c23ee6c3 GKH |
848 | mydbg("%s - port %d\n", __func__, port->number); |
849 | mydbg("%s - port->RxHolding = %d\n", __func__, port->RxHolding); | |
5ca0121f | 850 | |
c23ee6c3 GKH |
851 | if (port_paranoia_check(port, __func__) != 0) { |
852 | mydbg("%s - port_paranoia_check, exiting\n", __func__); | |
a9ea2266 GKH |
853 | port->ReadBulkStopped = 1; |
854 | return; | |
855 | } | |
5ca0121f | 856 | |
a9ea2266 | 857 | if (!serial) { |
c23ee6c3 | 858 | mydbg("%s - bad serial pointer, exiting\n", __func__); |
a9ea2266 GKH |
859 | return; |
860 | } | |
c23ee6c3 GKH |
861 | if (port->closePending == 1) { |
862 | /* Were closing , stop reading */ | |
863 | mydbg("%s - (port->closepending == 1\n", __func__); | |
a9ea2266 GKH |
864 | port->ReadBulkStopped = 1; |
865 | return; | |
866 | } | |
c23ee6c3 GKH |
867 | |
868 | /* | |
869 | * RxHolding is asserted by throttle, if we assert it, we're not | |
870 | * receiving any more characters and let the box handle the flow | |
871 | * control | |
872 | */ | |
a9ea2266 GKH |
873 | if (port->RxHolding == 1) { |
874 | port->ReadBulkStopped = 1; | |
875 | return; | |
876 | } | |
5ca0121f | 877 | |
a9ea2266 GKH |
878 | if (urb->status) { |
879 | port->ReadBulkStopped = 1; | |
5ca0121f | 880 | |
a9ea2266 | 881 | mydbg("%s - nonzero read bulk status received: %d\n", |
c23ee6c3 | 882 | __func__, urb->status); |
a9ea2266 GKH |
883 | return; |
884 | } | |
5ca0121f | 885 | |
a9ea2266 | 886 | tty = port->tty; |
c23ee6c3 | 887 | mydbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty); |
a9ea2266 GKH |
888 | |
889 | if (tty && RxCount) { | |
890 | flag_data = 0; | |
891 | for (i = 0; i < RxCount; ++i) { | |
c23ee6c3 | 892 | /* Look ahead code here */ |
a9ea2266 GKH |
893 | if ((i <= (RxCount - 3)) && (THISCHAR == 0x1b) |
894 | && (NEXTCHAR == 0x1b)) { | |
895 | flag = 0; | |
896 | switch (THIRDCHAR) { | |
897 | case 0x00: | |
c23ee6c3 | 898 | /* Line status change 4th byte must follow */ |
a9ea2266 | 899 | if (i > (RxCount - 4)) { |
c23ee6c3 | 900 | mydbg("Illegal escape sequences in received data\n"); |
a9ea2266 GKH |
901 | break; |
902 | } | |
903 | ProcessLineStatus(port, FOURTHCHAR); | |
904 | i += 3; | |
905 | flag = 1; | |
906 | break; | |
907 | ||
908 | case 0x01: | |
c23ee6c3 | 909 | /* Modem status status change 4th byte must follow */ |
a9ea2266 | 910 | mydbg("Modem status status. \n"); |
a9ea2266 GKH |
911 | if (i > (RxCount - 4)) { |
912 | mydbg | |
913 | ("Illegal escape sequences in received data\n"); | |
914 | break; | |
915 | } | |
916 | ProcessModemStatus(port, FOURTHCHAR); | |
917 | i += 3; | |
918 | flag = 1; | |
919 | break; | |
920 | case 0xff: | |
921 | mydbg("No status sequence. \n"); | |
922 | ||
923 | ProcessRxChar(port, THISCHAR); | |
924 | ProcessRxChar(port, NEXTCHAR); | |
925 | i += 2; | |
926 | break; | |
c23ee6c3 | 927 | } |
a9ea2266 GKH |
928 | if (flag == 1) |
929 | continue; | |
c23ee6c3 | 930 | } |
a9ea2266 GKH |
931 | |
932 | if (tty && urb->actual_length) { | |
933 | tty_buffer_request_room(tty, 1); | |
934 | tty_insert_flip_string(tty, (data + i), 1); | |
935 | } | |
936 | ||
c23ee6c3 | 937 | } |
a9ea2266 | 938 | tty_flip_buffer_push(tty); |
c23ee6c3 | 939 | } |
a9ea2266 GKH |
940 | |
941 | /* Continue trying to always read */ | |
942 | usb_fill_bulk_urb(port->read_urb, serial->dev, | |
943 | usb_rcvbulkpipe(serial->dev, | |
944 | port->bulk_in_endpointAddress), | |
945 | port->read_urb->transfer_buffer, | |
946 | port->read_urb->transfer_buffer_length, | |
947 | qt_read_bulk_callback, port); | |
948 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); | |
949 | if (result) | |
950 | mydbg("%s - failed resubmitting read urb, error %d", | |
c23ee6c3 | 951 | __func__, result); |
a9ea2266 GKH |
952 | else { |
953 | if (tty && RxCount) | |
0c212603 | 954 | flip_that(tty, index, serial); |
a9ea2266 | 955 | } |
5ca0121f | 956 | |
a9ea2266 | 957 | return; |
5ca0121f | 958 | |
a9ea2266 | 959 | } |
5ca0121f GKH |
960 | |
961 | static void qt_read_bulk_callback(struct urb *urb) | |
962 | { | |
963 | ||
a9ea2266 | 964 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
5ca0121f | 965 | |
a9ea2266 | 966 | if (urb->status) { |
a9ea2266 GKH |
967 | port->ReadBulkStopped = 1; |
968 | mydbg("%s - nonzero write bulk status received: %d\n", | |
c23ee6c3 | 969 | __func__, urb->status); |
a9ea2266 GKH |
970 | return; |
971 | } | |
5ca0121f | 972 | |
a9ea2266 GKH |
973 | port_sofrint((void *)port); |
974 | schedule_work(&port->work); | |
5ca0121f GKH |
975 | } |
976 | ||
a9ea2266 GKH |
977 | static void ProcessRxChar(struct usb_serial_port *port, unsigned char Data) |
978 | { | |
979 | struct tty_struct *tty; | |
980 | struct urb *urb = port->read_urb; | |
981 | tty = port->tty; | |
982 | /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */ | |
5ca0121f | 983 | |
a9ea2266 | 984 | if (tty && urb->actual_length) { |
5ca0121f GKH |
985 | tty_buffer_request_room(tty, 1); |
986 | tty_insert_flip_string(tty, &Data, 1); | |
c23ee6c3 | 987 | /* tty_flip_buffer_push(tty); */ |
5ca0121f GKH |
988 | } |
989 | ||
a9ea2266 | 990 | return; |
5ca0121f GKH |
991 | } |
992 | ||
a9ea2266 GKH |
993 | static void ProcessLineStatus(struct usb_serial_port *port, |
994 | unsigned char line_status) | |
995 | { | |
5ca0121f | 996 | |
a9ea2266 GKH |
997 | port->shadowLSR = |
998 | line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | | |
999 | SERIAL_LSR_BI); | |
1000 | return; | |
5ca0121f GKH |
1001 | } |
1002 | ||
a9ea2266 GKH |
1003 | static void ProcessModemStatus(struct usb_serial_port *port, |
1004 | unsigned char modem_status) | |
1005 | { | |
5ca0121f | 1006 | |
a9ea2266 GKH |
1007 | port->shadowMSR = modem_status; |
1008 | wake_up_interruptible(&port->wait); | |
1009 | return; | |
5ca0121f GKH |
1010 | } |
1011 | ||
a9ea2266 GKH |
1012 | static void serqt_usb_disconnect(struct usb_interface *interface) |
1013 | { | |
1014 | struct usb_serial *serial = usb_get_intfdata(interface); | |
c23ee6c3 | 1015 | /* struct device *dev = &interface->dev; */ |
a9ea2266 GKH |
1016 | struct usb_serial_port *port; |
1017 | int i; | |
5ca0121f | 1018 | |
c23ee6c3 | 1019 | mydbg("%s\n", __func__); |
a9ea2266 | 1020 | if (serial) { |
5ca0121f | 1021 | |
a9ea2266 | 1022 | serial->dev = NULL; |
5ca0121f | 1023 | |
a9ea2266 GKH |
1024 | for (i = 0; i < serial->num_ports; ++i) |
1025 | serial->port[i].open_count = 0; | |
5ca0121f | 1026 | |
a9ea2266 GKH |
1027 | for (i = 0; i < serial->num_bulk_in; ++i) { |
1028 | port = &serial->port[i]; | |
c23ee6c3 GKH |
1029 | usb_unlink_urb(port->read_urb); |
1030 | usb_free_urb(port->read_urb); | |
1031 | kfree(port->bulk_in_buffer); | |
a9ea2266 GKH |
1032 | } |
1033 | for (i = 0; i < serial->num_bulk_out; ++i) { | |
1034 | port = &serial->port[i]; | |
c23ee6c3 GKH |
1035 | usb_unlink_urb(port->write_urb); |
1036 | usb_free_urb(port->write_urb); | |
1037 | kfree(port->bulk_out_buffer); | |
a9ea2266 GKH |
1038 | } |
1039 | for (i = 0; i < serial->num_interrupt_in; ++i) { | |
1040 | port = &serial->port[i]; | |
c23ee6c3 GKH |
1041 | usb_unlink_urb(port->interrupt_in_urb); |
1042 | usb_free_urb(port->interrupt_in_urb); | |
1043 | kfree(port->interrupt_in_buffer); | |
1044 | } | |
5ca0121f | 1045 | |
a9ea2266 GKH |
1046 | /* return the minor range that this device had */ |
1047 | return_serial(serial); | |
5ca0121f | 1048 | |
a9ea2266 GKH |
1049 | /* free up any memory that we allocated */ |
1050 | kfree(serial); | |
5ca0121f | 1051 | |
a9ea2266 GKH |
1052 | } else { |
1053 | dev_info(&interface->dev, "device disconnected"); | |
1054 | } | |
5ca0121f GKH |
1055 | |
1056 | } | |
1057 | ||
a9ea2266 | 1058 | static struct usb_serial *get_serial_by_minor(unsigned int minor) |
5ca0121f | 1059 | { |
a9ea2266 | 1060 | return serial_table[minor]; |
5ca0121f GKH |
1061 | } |
1062 | ||
1063 | /***************************************************************************** | |
1064 | * Driver tty interface functions | |
1065 | *****************************************************************************/ | |
a9ea2266 GKH |
1066 | static int serial_open(struct tty_struct *tty, struct file *filp) |
1067 | { | |
1068 | struct usb_serial *serial; | |
1069 | struct usb_serial_port *port; | |
1070 | unsigned int portNumber; | |
1071 | int retval = 0; | |
5ca0121f | 1072 | |
c23ee6c3 | 1073 | mydbg("%s\n", __func__); |
5ca0121f | 1074 | |
a9ea2266 GKH |
1075 | /* initialize the pointer incase something fails */ |
1076 | tty->driver_data = NULL; | |
5ca0121f | 1077 | |
a9ea2266 | 1078 | /* get the serial object associated with this tty pointer */ |
c23ee6c3 | 1079 | /* serial = get_serial_by_minor (MINOR(tty->device)); */ |
5ca0121f | 1080 | |
a9ea2266 | 1081 | /* get the serial object associated with this tty pointer */ |
5ca0121f GKH |
1082 | serial = get_serial_by_minor(tty->index); |
1083 | ||
c23ee6c3 | 1084 | if (serial_paranoia_check(serial, __func__)) |
a9ea2266 | 1085 | return -ENODEV; |
5ca0121f | 1086 | |
a9ea2266 GKH |
1087 | /* set up our port structure making the tty driver remember our port object, and us it */ |
1088 | portNumber = tty->index - serial->minor; | |
1089 | port = &serial->port[portNumber]; | |
1090 | tty->driver_data = port; | |
5ca0121f | 1091 | |
a9ea2266 GKH |
1092 | down(&port->sem); |
1093 | port->tty = tty; | |
5ca0121f | 1094 | |
a9ea2266 GKH |
1095 | ++port->open_count; |
1096 | if (port->open_count == 1) { | |
1097 | port->closePending = 0; | |
c23ee6c3 | 1098 | mydbg("%s port->closepending = 0\n", __func__); |
5ca0121f | 1099 | |
a9ea2266 | 1100 | port->RxHolding = 0; |
c23ee6c3 | 1101 | mydbg("%s port->RxHolding = 0\n", __func__); |
5ca0121f | 1102 | |
0c212603 | 1103 | retval = qt_open(tty, port, filp); |
a9ea2266 | 1104 | } |
5ca0121f | 1105 | |
c23ee6c3 | 1106 | if (retval) |
a9ea2266 | 1107 | port->open_count = 0; |
c23ee6c3 | 1108 | mydbg("%s returning port->closePending = %d\n", __func__, |
a9ea2266 | 1109 | port->closePending); |
5ca0121f | 1110 | |
a9ea2266 GKH |
1111 | up(&port->sem); |
1112 | return retval; | |
5ca0121f | 1113 | } |
a9ea2266 | 1114 | |
5ca0121f GKH |
1115 | /***************************************************************************** |
1116 | *device's specific driver functions | |
1117 | *****************************************************************************/ | |
0c212603 AC |
1118 | static int qt_open(struct tty_struct *tty, struct usb_serial_port *port, |
1119 | struct file *filp) | |
a9ea2266 GKH |
1120 | { |
1121 | struct usb_serial *serial = port->serial; | |
1122 | int result = 0; | |
0c212603 | 1123 | unsigned int index; |
a9ea2266 GKH |
1124 | struct qt_get_device_data DeviceData; |
1125 | struct qt_open_channel_data ChannelData; | |
c23ee6c3 GKH |
1126 | unsigned short default_divisor = 0x30; /* gives 9600 baud rate */ |
1127 | unsigned char default_LCR = SERIAL_8_DATA; /* 8, none , 1 */ | |
a9ea2266 | 1128 | int status = 0; |
5ca0121f | 1129 | |
c23ee6c3 | 1130 | if (port_paranoia_check(port, __func__)) |
a9ea2266 | 1131 | return -ENODEV; |
5ca0121f | 1132 | |
c23ee6c3 | 1133 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1134 | |
0c212603 | 1135 | index = tty->index - serial->minor; |
5ca0121f | 1136 | |
a9ea2266 GKH |
1137 | status = box_get_device(serial, &DeviceData); |
1138 | if (status < 0) { | |
1139 | mydbg(__FILE__ "box_get_device failed\n"); | |
1140 | return status; | |
1141 | } | |
1142 | serial->num_OpenCount++; | |
c23ee6c3 | 1143 | mydbg("%s serial->num_OpenCount = %d\n", __func__, |
a9ea2266 | 1144 | serial->num_OpenCount); |
c23ee6c3 | 1145 | /* Open uart channel */ |
a9ea2266 | 1146 | |
c23ee6c3 | 1147 | /* Port specific setups */ |
0c212603 | 1148 | status = BoxOPenCloseChannel(serial, index, 1, &ChannelData); |
a9ea2266 GKH |
1149 | if (status < 0) { |
1150 | mydbg(__FILE__ "BoxOPenCloseChannel failed\n"); | |
1151 | return status; | |
1152 | } | |
1153 | mydbg(__FILE__ "BoxOPenCloseChannel completed.\n"); | |
5ca0121f | 1154 | |
a9ea2266 GKH |
1155 | port->shadowLSR = ChannelData.line_status & |
1156 | (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI); | |
5ca0121f | 1157 | |
a9ea2266 GKH |
1158 | port->shadowMSR = ChannelData.modem_status & |
1159 | (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD); | |
5ca0121f | 1160 | |
c23ee6c3 | 1161 | /* Set Baud rate to default and turn off (default)flow control here */ |
0c212603 | 1162 | status = BoxSetUart(serial, index, default_divisor, default_LCR); |
a9ea2266 GKH |
1163 | if (status < 0) { |
1164 | mydbg(__FILE__ "BoxSetUart failed\n"); | |
1165 | return status; | |
1166 | } | |
1167 | mydbg(__FILE__ "BoxSetUart completed.\n"); | |
5ca0121f | 1168 | |
c23ee6c3 | 1169 | /* Put this here to make it responsive to stty and defauls set by the tty layer */ |
0c212603 | 1170 | qt_set_termios(tty, port, NULL); |
5ca0121f | 1171 | |
c23ee6c3 | 1172 | /* Initialize the wait que head */ |
a9ea2266 | 1173 | init_waitqueue_head(&(port->wait)); |
5ca0121f | 1174 | |
a9ea2266 GKH |
1175 | /* if we have a bulk endpoint, start reading from it */ |
1176 | if (serial->num_bulk_in) { | |
1177 | /* Start reading from the device */ | |
1178 | usb_fill_bulk_urb(port->read_urb, serial->dev, | |
1179 | usb_rcvbulkpipe(serial->dev, | |
1180 | port-> | |
1181 | bulk_in_endpointAddress), | |
1182 | port->read_urb->transfer_buffer, | |
1183 | port->read_urb->transfer_buffer_length, | |
1184 | qt_read_bulk_callback, port); | |
5ca0121f | 1185 | |
a9ea2266 | 1186 | port->ReadBulkStopped = 0; |
5ca0121f | 1187 | |
a9ea2266 | 1188 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
5ca0121f | 1189 | |
a9ea2266 GKH |
1190 | if (result) { |
1191 | err("%s - failed resubmitting read urb, error %d\n", | |
c23ee6c3 | 1192 | __func__, result); |
a9ea2266 GKH |
1193 | port->ReadBulkStopped = 1; |
1194 | } | |
5ca0121f | 1195 | |
a9ea2266 | 1196 | } |
5ca0121f | 1197 | |
a9ea2266 | 1198 | return result; |
5ca0121f GKH |
1199 | } |
1200 | ||
a9ea2266 GKH |
1201 | static void serial_close(struct tty_struct *tty, struct file *filp) |
1202 | { | |
1203 | struct usb_serial_port *port = | |
0c212603 | 1204 | tty->driver_data; |
c23ee6c3 | 1205 | struct usb_serial *serial = get_usb_serial(port, __func__); |
5ca0121f | 1206 | |
a9ea2266 GKH |
1207 | if (!serial) |
1208 | return; | |
5ca0121f | 1209 | |
a9ea2266 | 1210 | down(&port->sem); |
5ca0121f | 1211 | |
c23ee6c3 | 1212 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1213 | |
a9ea2266 GKH |
1214 | /* if disconnect beat us to the punch here, there's nothing to do */ |
1215 | if (tty->driver_data) { | |
1216 | if (!port->open_count) { | |
c23ee6c3 | 1217 | mydbg("%s - port not opened\n", __func__); |
a9ea2266 GKH |
1218 | goto exit; |
1219 | } | |
5ca0121f | 1220 | |
a9ea2266 GKH |
1221 | --port->open_count; |
1222 | if (port->open_count <= 0) { | |
1223 | port->closePending = 1; | |
c23ee6c3 | 1224 | mydbg("%s - port->closePending = 1\n", __func__); |
5ca0121f | 1225 | |
a9ea2266 | 1226 | if (serial->dev) { |
0c212603 | 1227 | qt_close(tty, port, filp); |
a9ea2266 GKH |
1228 | port->open_count = 0; |
1229 | } | |
1230 | } | |
5ca0121f | 1231 | |
a9ea2266 | 1232 | } |
5ca0121f | 1233 | |
a9ea2266 GKH |
1234 | exit: |
1235 | up(&port->sem); | |
5ca0121f | 1236 | |
c23ee6c3 | 1237 | mydbg("%s - %d return\n", __func__, port->number); |
5ca0121f GKH |
1238 | |
1239 | } | |
1240 | ||
0c212603 AC |
1241 | static void qt_close(struct tty_struct *tty, struct usb_serial_port *port, |
1242 | struct file *filp) | |
a9ea2266 GKH |
1243 | { |
1244 | unsigned long jift = jiffies + 10 * HZ; | |
0c212603 | 1245 | u8 lsr, mcr; |
a9ea2266 GKH |
1246 | struct usb_serial *serial = port->serial; |
1247 | int status; | |
0c212603 | 1248 | unsigned int index; |
5ca0121f | 1249 | |
a9ea2266 GKH |
1250 | struct qt_open_channel_data ChannelData; |
1251 | status = 0; | |
0c212603 | 1252 | lsr = 0; |
5ca0121f | 1253 | |
c23ee6c3 | 1254 | mydbg("%s - port %d\n", __func__, port->number); |
0c212603 | 1255 | index = tty->index - serial->minor; |
a9ea2266 GKH |
1256 | |
1257 | /* shutdown any bulk reads that might be going on */ | |
1258 | if (serial->num_bulk_out) | |
1259 | usb_unlink_urb(port->write_urb); | |
1260 | if (serial->num_bulk_in) | |
1261 | usb_unlink_urb(port->read_urb); | |
1262 | ||
c23ee6c3 | 1263 | /* wait up to 30 seconds for transmitter to empty */ |
a9ea2266 | 1264 | do { |
0c212603 | 1265 | status = BoxGetRegister(serial, index, LINE_STATUS_REGISTER, &lsr); |
a9ea2266 GKH |
1266 | if (status < 0) { |
1267 | mydbg(__FILE__ "box_get_device failed\n"); | |
1268 | break; | |
1269 | } | |
5ca0121f | 1270 | |
0c212603 | 1271 | if ((lsr & SERIAL_LSR_TEMT) |
a9ea2266 GKH |
1272 | && (port->ReadBulkStopped == 1)) |
1273 | break; | |
1274 | schedule(); | |
5ca0121f | 1275 | |
a9ea2266 | 1276 | } |
0c212603 | 1277 | while (jiffies <= jift); |
a9ea2266 GKH |
1278 | |
1279 | if (jiffies > jift) | |
1280 | mydbg("%s - port %d timout of checking transmitter empty\n", | |
c23ee6c3 | 1281 | __func__, port->number); |
a9ea2266 GKH |
1282 | else |
1283 | mydbg("%s - port %d checking transmitter empty succeded\n", | |
c23ee6c3 | 1284 | __func__, port->number); |
a9ea2266 GKH |
1285 | |
1286 | status = | |
0c212603 AC |
1287 | BoxGetRegister(serial, index, MODEM_CONTROL_REGISTER, |
1288 | &mcr); | |
1289 | mydbg(__FILE__ "BoxGetRegister MCR = 0x%x.\n", mcr); | |
a9ea2266 GKH |
1290 | |
1291 | if (status >= 0) { | |
0c212603 AC |
1292 | mcr &= ~(SERIAL_MCR_DTR | SERIAL_MCR_RTS); |
1293 | /* status = BoxSetRegister(serial, index, MODEM_CONTROL_REGISTER, mcr); */ | |
a9ea2266 | 1294 | } |
5ca0121f | 1295 | |
c23ee6c3 | 1296 | /* Close uart channel */ |
0c212603 | 1297 | status = BoxOPenCloseChannel(serial, index, 0, &ChannelData); |
a9ea2266 GKH |
1298 | if (status < 0) |
1299 | mydbg("%s - port %d BoxOPenCloseChannel failed.\n", | |
c23ee6c3 | 1300 | __func__, port->number); |
5ca0121f | 1301 | |
a9ea2266 | 1302 | serial->num_OpenCount--; |
5ca0121f GKH |
1303 | |
1304 | } | |
1305 | ||
a9ea2266 GKH |
1306 | static int serial_write(struct tty_struct *tty, const unsigned char *buf, |
1307 | int count) | |
5ca0121f | 1308 | { |
0c212603 | 1309 | struct usb_serial_port *port = tty->driver_data; |
a9ea2266 GKH |
1310 | struct usb_serial *serial; |
1311 | int retval = -EINVAL; | |
0c212603 | 1312 | unsigned int index; |
5ca0121f | 1313 | |
c23ee6c3 | 1314 | serial = get_usb_serial(port, __func__); |
a9ea2266 GKH |
1315 | if (serial == NULL) |
1316 | return -ENODEV; | |
c23ee6c3 GKH |
1317 | /* This can happen if we get disconnected a */ |
1318 | if (port->open_count == 0) | |
a9ea2266 | 1319 | return -ENODEV; |
0c212603 | 1320 | index = tty->index - serial->minor; |
5ca0121f | 1321 | |
c23ee6c3 GKH |
1322 | mydbg("%s - port %d, %d byte(s)\n", __func__, port->number, count); |
1323 | mydbg("%s - port->RxHolding = %d\n", __func__, port->RxHolding); | |
5ca0121f | 1324 | |
a9ea2266 | 1325 | if (!port->open_count) { |
c23ee6c3 | 1326 | mydbg("%s - port not opened\n", __func__); |
a9ea2266 GKH |
1327 | goto exit; |
1328 | } | |
5ca0121f | 1329 | |
0c212603 | 1330 | retval = qt_write(tty, port, buf, count); |
5ca0121f | 1331 | |
a9ea2266 GKH |
1332 | exit: |
1333 | return retval; | |
1334 | } | |
5ca0121f | 1335 | |
0c212603 AC |
1336 | static int qt_write(struct tty_struct *tty, struct usb_serial_port *port, |
1337 | const unsigned char *buf, int count) | |
a9ea2266 GKH |
1338 | { |
1339 | int result; | |
0c212603 | 1340 | unsigned int index; |
c23ee6c3 | 1341 | struct usb_serial *serial = get_usb_serial(port, __func__); |
0c212603 | 1342 | |
a9ea2266 GKH |
1343 | if (serial == NULL) |
1344 | return -ENODEV; | |
5ca0121f | 1345 | |
c23ee6c3 | 1346 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1347 | |
a9ea2266 | 1348 | if (count == 0) { |
c23ee6c3 GKH |
1349 | mydbg("%s - write request of 0 bytes\n", __func__); |
1350 | return 0; | |
a9ea2266 | 1351 | } |
5ca0121f | 1352 | |
0c212603 | 1353 | index = tty->index - serial->minor; |
a9ea2266 GKH |
1354 | /* only do something if we have a bulk out endpoint */ |
1355 | if (serial->num_bulk_out) { | |
1356 | if (port->write_urb->status == -EINPROGRESS) { | |
c23ee6c3 GKH |
1357 | mydbg("%s - already writing\n", __func__); |
1358 | return 0; | |
a9ea2266 | 1359 | } |
5ca0121f | 1360 | |
a9ea2266 GKH |
1361 | count = |
1362 | (count > port->bulk_out_size) ? port->bulk_out_size : count; | |
0c212603 | 1363 | memcpy(port->write_urb->transfer_buffer, buf, count); |
5ca0121f | 1364 | |
c23ee6c3 | 1365 | /* usb_serial_debug_data(__FILE__, __func__, count, port->write_urb->transfer_buffer); */ |
5ca0121f | 1366 | |
a9ea2266 | 1367 | /* set up our urb */ |
5ca0121f | 1368 | |
a9ea2266 GKH |
1369 | usb_fill_bulk_urb(port->write_urb, serial->dev, |
1370 | usb_sndbulkpipe(serial->dev, | |
1371 | port-> | |
1372 | bulk_out_endpointAddress), | |
1373 | port->write_urb->transfer_buffer, count, | |
1374 | qt_write_bulk_callback, port); | |
5ca0121f | 1375 | |
a9ea2266 GKH |
1376 | /* send the data out the bulk port */ |
1377 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); | |
1378 | if (result) | |
1379 | mydbg("%s - failed submitting write urb, error %d\n", | |
c23ee6c3 | 1380 | __func__, result); |
a9ea2266 GKH |
1381 | else |
1382 | result = count; | |
5ca0121f | 1383 | |
a9ea2266 GKH |
1384 | return result; |
1385 | } | |
5ca0121f | 1386 | |
a9ea2266 | 1387 | /* no bulk out, so return 0 bytes written */ |
c23ee6c3 | 1388 | return 0; |
5ca0121f GKH |
1389 | } |
1390 | ||
5ca0121f GKH |
1391 | static void qt_write_bulk_callback(struct urb *urb) |
1392 | { | |
a9ea2266 | 1393 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
c23ee6c3 | 1394 | struct usb_serial *serial = get_usb_serial(port, __func__); |
5ca0121f | 1395 | |
c23ee6c3 | 1396 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1397 | |
a9ea2266 | 1398 | if (!serial) { |
c23ee6c3 | 1399 | mydbg("%s - bad serial pointer, exiting\n", __func__); |
a9ea2266 GKH |
1400 | return; |
1401 | } | |
5ca0121f | 1402 | |
a9ea2266 GKH |
1403 | if (urb->status) { |
1404 | mydbg("%s - nonzero write bulk status received: %d\n", | |
c23ee6c3 | 1405 | __func__, urb->status); |
a9ea2266 GKH |
1406 | return; |
1407 | } | |
a9ea2266 GKH |
1408 | port_softint(&port->work); |
1409 | schedule_work(&port->work); | |
5ca0121f | 1410 | |
a9ea2266 GKH |
1411 | return; |
1412 | } | |
5ca0121f | 1413 | |
a9ea2266 GKH |
1414 | static void port_softint(struct work_struct *work) |
1415 | { | |
1416 | struct usb_serial_port *port = | |
1417 | container_of(work, struct usb_serial_port, work); | |
c23ee6c3 | 1418 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 1419 | struct tty_struct *tty; |
5ca0121f | 1420 | |
c23ee6c3 | 1421 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1422 | |
a9ea2266 GKH |
1423 | if (!serial) |
1424 | return; | |
5ca0121f | 1425 | |
a9ea2266 GKH |
1426 | tty = port->tty; |
1427 | if (!tty) | |
1428 | return; | |
5ca0121f | 1429 | #if 0 |
a9ea2266 GKH |
1430 | if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) |
1431 | && tty->ldisc.write_wakeup) { | |
c23ee6c3 | 1432 | mydbg("%s - write wakeup call.\n", __func__); |
a9ea2266 GKH |
1433 | (tty->ldisc.write_wakeup) (tty); |
1434 | } | |
5ca0121f GKH |
1435 | #endif |
1436 | ||
a9ea2266 | 1437 | wake_up_interruptible(&tty->write_wait); |
5ca0121f | 1438 | } |
a9ea2266 | 1439 | static int serial_write_room(struct tty_struct *tty) |
5ca0121f | 1440 | { |
0c212603 | 1441 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 1442 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 1443 | int retval = -EINVAL; |
5ca0121f | 1444 | |
a9ea2266 GKH |
1445 | if (!serial) |
1446 | return -ENODEV; | |
5ca0121f | 1447 | |
a9ea2266 | 1448 | down(&port->sem); |
5ca0121f | 1449 | |
c23ee6c3 | 1450 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1451 | |
a9ea2266 | 1452 | if (!port->open_count) { |
c23ee6c3 | 1453 | mydbg("%s - port not open\n", __func__); |
a9ea2266 GKH |
1454 | goto exit; |
1455 | } | |
5ca0121f | 1456 | |
a9ea2266 | 1457 | retval = qt_write_room(port); |
5ca0121f | 1458 | |
a9ea2266 GKH |
1459 | exit: |
1460 | up(&port->sem); | |
1461 | return retval; | |
5ca0121f | 1462 | } |
a9ea2266 | 1463 | static int qt_write_room(struct usb_serial_port *port) |
5ca0121f | 1464 | { |
a9ea2266 GKH |
1465 | struct usb_serial *serial = port->serial; |
1466 | int room = 0; | |
1467 | if (port->closePending == 1) { | |
c23ee6c3 | 1468 | mydbg("%s - port->closePending == 1\n", __func__); |
a9ea2266 GKH |
1469 | return -ENODEV; |
1470 | } | |
5ca0121f | 1471 | |
c23ee6c3 | 1472 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1473 | |
a9ea2266 GKH |
1474 | if (serial->num_bulk_out) { |
1475 | if (port->write_urb->status != -EINPROGRESS) | |
1476 | room = port->bulk_out_size; | |
1477 | } | |
5ca0121f | 1478 | |
c23ee6c3 GKH |
1479 | mydbg("%s - returns %d\n", __func__, room); |
1480 | return room; | |
5ca0121f | 1481 | } |
a9ea2266 | 1482 | static int serial_chars_in_buffer(struct tty_struct *tty) |
5ca0121f | 1483 | { |
0c212603 | 1484 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 1485 | struct usb_serial *serial = get_usb_serial(port, __func__); |
5ca0121f GKH |
1486 | int retval = -EINVAL; |
1487 | ||
1488 | if (!serial) | |
1489 | return -ENODEV; | |
1490 | ||
a9ea2266 | 1491 | down(&port->sem); |
5ca0121f | 1492 | |
c23ee6c3 | 1493 | mydbg("%s = port %d\n", __func__, port->number); |
5ca0121f GKH |
1494 | |
1495 | if (!port->open_count) { | |
c23ee6c3 | 1496 | mydbg("%s - port not open\n", __func__); |
5ca0121f GKH |
1497 | goto exit; |
1498 | } | |
1499 | ||
1500 | retval = qt_chars_in_buffer(port); | |
1501 | ||
1502 | exit: | |
a9ea2266 | 1503 | up(&port->sem); |
5ca0121f GKH |
1504 | return retval; |
1505 | } | |
1506 | ||
a9ea2266 | 1507 | static int qt_chars_in_buffer(struct usb_serial_port *port) |
5ca0121f | 1508 | { |
a9ea2266 GKH |
1509 | struct usb_serial *serial = port->serial; |
1510 | int chars = 0; | |
5ca0121f | 1511 | |
c23ee6c3 | 1512 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f | 1513 | |
a9ea2266 GKH |
1514 | if (serial->num_bulk_out) { |
1515 | if (port->write_urb->status == -EINPROGRESS) | |
1516 | chars = port->write_urb->transfer_buffer_length; | |
1517 | } | |
5ca0121f | 1518 | |
c23ee6c3 GKH |
1519 | mydbg("%s - returns %d\n", __func__, chars); |
1520 | return chars; | |
5ca0121f GKH |
1521 | } |
1522 | ||
a9ea2266 GKH |
1523 | static int serial_tiocmset(struct tty_struct *tty, struct file *file, |
1524 | unsigned int set, unsigned int clear) | |
5ca0121f GKH |
1525 | { |
1526 | ||
0c212603 | 1527 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 1528 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 1529 | int retval = -ENODEV; |
0c212603 | 1530 | unsigned int index; |
c23ee6c3 | 1531 | mydbg("In %s \n", __func__); |
5ca0121f | 1532 | |
a9ea2266 GKH |
1533 | if (!serial) |
1534 | return -ENODEV; | |
5ca0121f | 1535 | |
0c212603 | 1536 | index = tty->index - serial->minor; |
5ca0121f | 1537 | |
a9ea2266 | 1538 | down(&port->sem); |
5ca0121f | 1539 | |
c23ee6c3 GKH |
1540 | mydbg("%s - port %d \n", __func__, port->number); |
1541 | mydbg("%s - port->RxHolding = %d\n", __func__, port->RxHolding); | |
5ca0121f | 1542 | |
a9ea2266 | 1543 | if (!port->open_count) { |
c23ee6c3 | 1544 | mydbg("%s - port not open\n", __func__); |
a9ea2266 GKH |
1545 | goto exit; |
1546 | } | |
5ca0121f | 1547 | |
0c212603 | 1548 | retval = qt_tiocmset(tty, port, file, set); |
5ca0121f | 1549 | |
a9ea2266 GKH |
1550 | exit: |
1551 | up(&port->sem); | |
1552 | return retval; | |
5ca0121f GKH |
1553 | } |
1554 | ||
0c212603 AC |
1555 | static int qt_tiocmset(struct tty_struct *tty, struct usb_serial_port *port, |
1556 | struct file *file, unsigned int value) | |
5ca0121f GKH |
1557 | { |
1558 | ||
0c212603 | 1559 | u8 mcr; |
a9ea2266 | 1560 | int status; |
0c212603 | 1561 | unsigned int index; |
c23ee6c3 | 1562 | struct usb_serial *serial = get_usb_serial(port, __func__); |
0c212603 | 1563 | |
a9ea2266 GKH |
1564 | if (serial == NULL) |
1565 | return -ENODEV; | |
5ca0121f | 1566 | |
c23ee6c3 | 1567 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f GKH |
1568 | |
1569 | /**************************************************************************************/ | |
1570 | /** TIOCMGET | |
1571 | */ | |
0c212603 | 1572 | index = tty->index - serial->minor; |
a9ea2266 | 1573 | status = |
0c212603 AC |
1574 | BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, |
1575 | &mcr); | |
a9ea2266 GKH |
1576 | if (status < 0) |
1577 | return -ESPIPE; | |
1578 | ||
c23ee6c3 GKH |
1579 | /* |
1580 | * Turn off the RTS and DTR and loopbcck and then only turn on what was | |
1581 | * asked for | |
1582 | */ | |
0c212603 | 1583 | mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP); |
a9ea2266 | 1584 | if (value & TIOCM_RTS) |
0c212603 | 1585 | mcr |= SERIAL_MCR_RTS; |
a9ea2266 | 1586 | if (value & TIOCM_DTR) |
0c212603 | 1587 | mcr |= SERIAL_MCR_DTR; |
a9ea2266 | 1588 | if (value & TIOCM_LOOP) |
0c212603 | 1589 | mcr |= SERIAL_MCR_LOOP; |
a9ea2266 GKH |
1590 | |
1591 | status = | |
0c212603 AC |
1592 | BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, |
1593 | mcr); | |
a9ea2266 GKH |
1594 | if (status < 0) |
1595 | return -ESPIPE; | |
1596 | else | |
1597 | return 0; | |
5ca0121f GKH |
1598 | } |
1599 | ||
1600 | static int serial_tiocmget(struct tty_struct *tty, struct file *file) | |
1601 | { | |
1602 | ||
0c212603 | 1603 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 1604 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 1605 | int retval = -ENODEV; |
0c212603 | 1606 | unsigned int index; |
c23ee6c3 | 1607 | mydbg("In %s \n", __func__); |
5ca0121f | 1608 | |
a9ea2266 GKH |
1609 | if (!serial) |
1610 | return -ENODEV; | |
5ca0121f | 1611 | |
0c212603 | 1612 | index = tty->index - serial->minor; |
5ca0121f | 1613 | |
a9ea2266 | 1614 | down(&port->sem); |
5ca0121f | 1615 | |
c23ee6c3 GKH |
1616 | mydbg("%s - port %d\n", __func__, port->number); |
1617 | mydbg("%s - port->RxHolding = %d\n", __func__, port->RxHolding); | |
5ca0121f | 1618 | |
a9ea2266 | 1619 | if (!port->open_count) { |
c23ee6c3 | 1620 | mydbg("%s - port not open\n", __func__); |
a9ea2266 GKH |
1621 | goto exit; |
1622 | } | |
5ca0121f | 1623 | |
0c212603 | 1624 | retval = qt_tiocmget(tty, port, file); |
5ca0121f | 1625 | |
a9ea2266 GKH |
1626 | exit: |
1627 | up(&port->sem); | |
1628 | return retval; | |
5ca0121f GKH |
1629 | } |
1630 | ||
0c212603 AC |
1631 | static int qt_tiocmget(struct tty_struct *tty, |
1632 | struct usb_serial_port *port, struct file *file) | |
5ca0121f GKH |
1633 | { |
1634 | ||
0c212603 AC |
1635 | u8 mcr; |
1636 | u8 msr; | |
a9ea2266 GKH |
1637 | unsigned int result = 0; |
1638 | int status; | |
0c212603 | 1639 | unsigned int index; |
5ca0121f | 1640 | |
c23ee6c3 | 1641 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 GKH |
1642 | if (serial == NULL) |
1643 | return -ENODEV; | |
5ca0121f | 1644 | |
c23ee6c3 | 1645 | mydbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty); |
5ca0121f GKH |
1646 | |
1647 | /**************************************************************************************/ | |
1648 | /** TIOCMGET | |
1649 | */ | |
0c212603 | 1650 | index = tty->index - serial->minor; |
a9ea2266 | 1651 | status = |
0c212603 AC |
1652 | BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, |
1653 | &mcr); | |
a9ea2266 GKH |
1654 | if (status >= 0) { |
1655 | status = | |
0c212603 AC |
1656 | BoxGetRegister(port->serial, index, |
1657 | MODEM_STATUS_REGISTER, &msr); | |
5ca0121f | 1658 | |
a9ea2266 | 1659 | } |
5ca0121f | 1660 | |
a9ea2266 | 1661 | if (status >= 0) { |
0c212603 | 1662 | result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0) |
c23ee6c3 | 1663 | /* DTR IS SET */ |
0c212603 | 1664 | | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0) |
c23ee6c3 | 1665 | /* RTS IS SET */ |
0c212603 | 1666 | | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0) |
c23ee6c3 | 1667 | /* CTS is set */ |
0c212603 | 1668 | | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0) |
c23ee6c3 | 1669 | /* Carrier detect is set */ |
0c212603 | 1670 | | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0) |
c23ee6c3 | 1671 | /* Ring indicator set */ |
0c212603 | 1672 | | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0); |
c23ee6c3 | 1673 | /* DSR is set */ |
a9ea2266 | 1674 | return result; |
5ca0121f | 1675 | |
a9ea2266 GKH |
1676 | } else |
1677 | return -ESPIPE; | |
a9ea2266 | 1678 | } |
5ca0121f | 1679 | |
a9ea2266 GKH |
1680 | static int serial_ioctl(struct tty_struct *tty, struct file *file, |
1681 | unsigned int cmd, unsigned long arg) | |
1682 | { | |
5ca0121f | 1683 | |
0c212603 | 1684 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 1685 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 1686 | int retval = -ENODEV; |
0c212603 | 1687 | unsigned int index; |
c23ee6c3 | 1688 | mydbg("In %s \n", __func__); |
5ca0121f | 1689 | |
a9ea2266 GKH |
1690 | if (!serial) |
1691 | return -ENODEV; | |
5ca0121f | 1692 | |
0c212603 | 1693 | index = tty->index - serial->minor; |
5ca0121f | 1694 | |
a9ea2266 | 1695 | down(&port->sem); |
5ca0121f | 1696 | |
c23ee6c3 GKH |
1697 | mydbg("%s - port %d, cmd 0x%.4x\n", __func__, port->number, cmd); |
1698 | mydbg("%s - port->RxHolding = %d\n", __func__, port->RxHolding); | |
5ca0121f | 1699 | |
a9ea2266 | 1700 | if (!port->open_count) { |
c23ee6c3 | 1701 | mydbg("%s - port not open\n", __func__); |
a9ea2266 GKH |
1702 | goto exit; |
1703 | } | |
5ca0121f | 1704 | |
0c212603 | 1705 | retval = qt_ioctl(tty, port, file, cmd, arg); |
5ca0121f | 1706 | |
a9ea2266 GKH |
1707 | exit: |
1708 | up(&port->sem); | |
1709 | return retval; | |
5ca0121f | 1710 | } |
0c212603 AC |
1711 | static int qt_ioctl(struct tty_struct *tty, struct usb_serial_port *port, |
1712 | struct file *file, unsigned int cmd, unsigned long arg) | |
5ca0121f | 1713 | { |
0c212603 AC |
1714 | __u8 mcr; |
1715 | __u8 msr; | |
1716 | unsigned short prev_msr; | |
a9ea2266 GKH |
1717 | unsigned int value, result = 0; |
1718 | int status; | |
0c212603 | 1719 | unsigned int index; |
a9ea2266 | 1720 | |
c23ee6c3 | 1721 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 GKH |
1722 | if (serial == NULL) |
1723 | return -ENODEV; | |
5ca0121f | 1724 | |
c23ee6c3 | 1725 | mydbg("%s - port %d, tty =0x%p\n", __func__, port->number, tty); |
5ca0121f | 1726 | |
c23ee6c3 | 1727 | /* TIOCMGET */ |
0c212603 | 1728 | index = tty->index - serial->minor; |
5ca0121f | 1729 | |
a9ea2266 GKH |
1730 | if (cmd == TIOCMIWAIT) { |
1731 | DECLARE_WAITQUEUE(wait, current); | |
0c212603 | 1732 | prev_msr = port->shadowMSR & SERIAL_MSR_MASK; |
a9ea2266 GKH |
1733 | while (1) { |
1734 | add_wait_queue(&port->wait, &wait); | |
1735 | set_current_state(TASK_INTERRUPTIBLE); | |
1736 | schedule(); | |
1737 | remove_wait_queue(&port->wait, &wait); | |
1738 | /* see if a signal woke us up */ | |
1739 | if (signal_pending(current)) | |
1740 | return -ERESTARTSYS; | |
0c212603 AC |
1741 | msr = port->shadowMSR & SERIAL_MSR_MASK; |
1742 | if (msr == prev_msr) | |
c23ee6c3 | 1743 | return -EIO; /* no change error */ |
a9ea2266 GKH |
1744 | |
1745 | if ((arg & TIOCM_RNG | |
0c212603 AC |
1746 | && ((prev_msr & SERIAL_MSR_RI) == |
1747 | (msr & SERIAL_MSR_RI))) | |
a9ea2266 | 1748 | || (arg & TIOCM_DSR |
0c212603 AC |
1749 | && ((prev_msr & SERIAL_MSR_DSR) == |
1750 | (msr & SERIAL_MSR_DSR))) | |
a9ea2266 | 1751 | || (arg & TIOCM_CD |
0c212603 AC |
1752 | && ((prev_msr & SERIAL_MSR_CD) == |
1753 | (msr & SERIAL_MSR_CD))) | |
a9ea2266 | 1754 | || (arg & TIOCM_CTS |
0c212603 AC |
1755 | && ((prev_msr & SERIAL_MSR_CTS) == |
1756 | (msr & SERIAL_MSR_CTS)))) { | |
a9ea2266 GKH |
1757 | return 0; |
1758 | } | |
1759 | ||
c23ee6c3 | 1760 | } |
a9ea2266 GKH |
1761 | |
1762 | } | |
c23ee6c3 | 1763 | mydbg("%s -No ioctl for that one. port = %d\n", __func__, |
a9ea2266 GKH |
1764 | port->number); |
1765 | ||
1766 | return -ENOIOCTLCMD; | |
5ca0121f GKH |
1767 | } |
1768 | ||
a9ea2266 | 1769 | static void serial_set_termios(struct tty_struct *tty, struct ktermios *old) |
5ca0121f | 1770 | { |
a9ea2266 | 1771 | struct usb_serial_port *port = |
0c212603 | 1772 | tty->driver_data; |
c23ee6c3 | 1773 | struct usb_serial *serial = get_usb_serial(port, __func__); |
5ca0121f GKH |
1774 | |
1775 | if (!serial) | |
1776 | return; | |
1777 | ||
a9ea2266 | 1778 | down(&port->sem); |
5ca0121f | 1779 | |
c23ee6c3 | 1780 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f GKH |
1781 | |
1782 | if (!port->open_count) { | |
c23ee6c3 | 1783 | mydbg("%s - port not open\n", __func__); |
5ca0121f GKH |
1784 | goto exit; |
1785 | } | |
1786 | ||
1787 | /* pass on to the driver specific version of this function if it is available */ | |
0c212603 | 1788 | qt_set_termios(tty, port, old); |
5ca0121f GKH |
1789 | |
1790 | exit: | |
a9ea2266 | 1791 | up(&port->sem); |
5ca0121f GKH |
1792 | } |
1793 | ||
0c212603 AC |
1794 | static void qt_set_termios(struct tty_struct *tty, |
1795 | struct usb_serial_port *port, | |
a9ea2266 | 1796 | struct ktermios *old_termios) |
5ca0121f | 1797 | { |
a9ea2266 GKH |
1798 | unsigned int cflag; |
1799 | int baud, divisor, remainder; | |
0c212603 | 1800 | unsigned char new_LCR = 0; |
a9ea2266 GKH |
1801 | int status; |
1802 | struct usb_serial *serial; | |
0c212603 | 1803 | __u16 index; |
a9ea2266 GKH |
1804 | __u16 tmp, tmp2; |
1805 | ||
c23ee6c3 | 1806 | mydbg("%s - port %d\n", __func__, port->number); |
a9ea2266 GKH |
1807 | |
1808 | tmp = port->tty->index; | |
c23ee6c3 | 1809 | mydbg("%s - MINOR(port->tty->index) = %d\n", __func__, tmp); |
a9ea2266 GKH |
1810 | |
1811 | serial = port->serial; | |
1812 | tmp2 = serial->minor; | |
c23ee6c3 | 1813 | mydbg("%s - serial->minor = %d\n", __func__, tmp2); |
a9ea2266 | 1814 | |
0c212603 | 1815 | index = port->tty->index - serial->minor; |
a9ea2266 GKH |
1816 | |
1817 | cflag = tty->termios->c_cflag; | |
1818 | ||
c23ee6c3 | 1819 | mydbg("%s - 3\n", __func__); |
a9ea2266 GKH |
1820 | |
1821 | switch (cflag) { | |
1822 | case CS5: | |
0c212603 | 1823 | new_LCR |= SERIAL_5_DATA; |
a9ea2266 GKH |
1824 | break; |
1825 | case CS6: | |
0c212603 | 1826 | new_LCR |= SERIAL_6_DATA; |
a9ea2266 GKH |
1827 | break; |
1828 | case CS7: | |
0c212603 | 1829 | new_LCR |= SERIAL_7_DATA; |
a9ea2266 GKH |
1830 | break; |
1831 | default: | |
1832 | case CS8: | |
0c212603 | 1833 | new_LCR |= SERIAL_8_DATA; |
a9ea2266 GKH |
1834 | break; |
1835 | } | |
1836 | ||
c23ee6c3 | 1837 | /* Parity stuff */ |
a9ea2266 GKH |
1838 | if (cflag & PARENB) { |
1839 | if (cflag & PARODD) | |
0c212603 | 1840 | new_LCR |= SERIAL_ODD_PARITY; |
a9ea2266 | 1841 | else |
0c212603 | 1842 | new_LCR |= SERIAL_EVEN_PARITY; |
a9ea2266 GKH |
1843 | } |
1844 | if (cflag & CSTOPB) | |
0c212603 | 1845 | new_LCR |= SERIAL_TWO_STOPB; |
a9ea2266 | 1846 | else |
0c212603 | 1847 | new_LCR |= SERIAL_TWO_STOPB; |
a9ea2266 | 1848 | |
c23ee6c3 GKH |
1849 | mydbg("%s - 4\n", __func__); |
1850 | /* Thats the LCR stuff, go ahead and set it */ | |
a9ea2266 | 1851 | baud = tty_get_baud_rate(tty); |
0c212603 | 1852 | if (!baud) |
a9ea2266 GKH |
1853 | /* pick a default, any default... */ |
1854 | baud = 9600; | |
a9ea2266 | 1855 | |
c23ee6c3 | 1856 | mydbg("%s - got baud = %d\n", __func__, baud); |
a9ea2266 GKH |
1857 | |
1858 | divisor = MAX_BAUD_RATE / baud; | |
1859 | remainder = MAX_BAUD_RATE % baud; | |
c23ee6c3 | 1860 | /* Round to nearest divisor */ |
a9ea2266 GKH |
1861 | if (((remainder * 2) >= baud) && (baud != 110)) |
1862 | divisor++; | |
1863 | ||
c23ee6c3 GKH |
1864 | /* |
1865 | * Set Baud rate to default and turn off (default)flow control here | |
1866 | */ | |
0c212603 | 1867 | status = BoxSetUart(serial, index, (unsigned short)divisor, new_LCR); |
a9ea2266 GKH |
1868 | if (status < 0) { |
1869 | mydbg(__FILE__ "BoxSetUart failed\n"); | |
1870 | return; | |
1871 | } | |
5ca0121f | 1872 | |
c23ee6c3 | 1873 | /* Now determine flow control */ |
a9ea2266 | 1874 | if (cflag & CRTSCTS) { |
c23ee6c3 | 1875 | mydbg("%s - Enabling HW flow control port %d\n", __func__, |
a9ea2266 | 1876 | port->number); |
5ca0121f | 1877 | |
c23ee6c3 | 1878 | /* Enable RTS/CTS flow control */ |
0c212603 | 1879 | status = BoxSetHW_FlowCtrl(serial, index, 1); |
5ca0121f | 1880 | |
a9ea2266 GKH |
1881 | if (status < 0) { |
1882 | mydbg(__FILE__ "BoxSetHW_FlowCtrl failed\n"); | |
1883 | return; | |
1884 | } | |
1885 | } else { | |
c23ee6c3 GKH |
1886 | /* Disable RTS/CTS flow control */ |
1887 | mydbg("%s - disabling HW flow control port %d\n", __func__, | |
a9ea2266 GKH |
1888 | port->number); |
1889 | ||
0c212603 | 1890 | status = BoxSetHW_FlowCtrl(serial, index, 0); |
a9ea2266 GKH |
1891 | if (status < 0) { |
1892 | mydbg(__FILE__ "BoxSetHW_FlowCtrl failed\n"); | |
1893 | return; | |
1894 | } | |
1895 | ||
1896 | } | |
5ca0121f | 1897 | |
c23ee6c3 GKH |
1898 | /* if we are implementing XON/XOFF, set the start and stop character in |
1899 | * the device */ | |
a9ea2266 GKH |
1900 | if (I_IXOFF(tty) || I_IXON(tty)) { |
1901 | unsigned char stop_char = STOP_CHAR(tty); | |
1902 | unsigned char start_char = START_CHAR(tty); | |
1903 | status = | |
0c212603 | 1904 | BoxSetSW_FlowCtrl(serial, index, stop_char, |
a9ea2266 GKH |
1905 | start_char); |
1906 | if (status < 0) | |
1907 | mydbg(__FILE__ "BoxSetSW_FlowCtrl (enabled) failed\n"); | |
1908 | ||
1909 | } else { | |
c23ee6c3 | 1910 | /* disable SW flow control */ |
0c212603 | 1911 | status = BoxDisable_SW_FlowCtrl(serial, index); |
a9ea2266 GKH |
1912 | if (status < 0) |
1913 | mydbg(__FILE__ "BoxSetSW_FlowCtrl (diabling) failed\n"); | |
1914 | ||
1915 | } | |
0c212603 AC |
1916 | tty->termios->c_cflag &= ~CMSPAR; |
1917 | /* FIXME: Error cases should be returning the actual bits changed only */ | |
5ca0121f | 1918 | } |
a9ea2266 | 1919 | |
5ca0121f GKH |
1920 | /**************************************************************************** |
1921 | * BoxGetRegister | |
1922 | * issuse a GET_REGISTER vendor-spcific request on the default control pipe | |
1923 | * If successful, fills in the pValue with the register value asked for | |
1924 | ****************************************************************************/ | |
a9ea2266 | 1925 | static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number, |
c23ee6c3 | 1926 | unsigned short Register_Num, __u8 *pValue) |
5ca0121f | 1927 | { |
a9ea2266 GKH |
1928 | int result; |
1929 | __u16 current_length; | |
5ca0121f | 1930 | |
a9ea2266 | 1931 | current_length = sizeof(struct qt_get_device_data); |
5ca0121f | 1932 | |
a9ea2266 GKH |
1933 | result = |
1934 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), | |
1935 | QT_GET_SET_REGISTER, 0xC0, Register_Num, | |
1936 | Uart_Number, (void *)pValue, sizeof(*pValue), 300); | |
5ca0121f | 1937 | |
a9ea2266 | 1938 | return result; |
5ca0121f | 1939 | } |
a9ea2266 | 1940 | |
5ca0121f GKH |
1941 | /**************************************************************************** |
1942 | * BoxSetRegister | |
1943 | * issuse a GET_REGISTER vendor-spcific request on the default control pipe | |
1944 | * If successful, fills in the pValue with the register value asked for | |
1945 | ****************************************************************************/ | |
a9ea2266 GKH |
1946 | static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number, |
1947 | unsigned short Register_Num, unsigned short Value) | |
5ca0121f | 1948 | { |
a9ea2266 GKH |
1949 | int result; |
1950 | unsigned short RegAndByte; | |
5ca0121f | 1951 | |
a9ea2266 GKH |
1952 | RegAndByte = Value; |
1953 | RegAndByte = RegAndByte << 8; | |
1954 | RegAndByte = RegAndByte + Register_Num; | |
5ca0121f | 1955 | |
c23ee6c3 GKH |
1956 | /* |
1957 | result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
1958 | QT_GET_SET_REGISTER, 0xC0, Register_Num, | |
1959 | Uart_Number, NULL, 0, 300); | |
1960 | */ | |
5ca0121f | 1961 | |
a9ea2266 GKH |
1962 | result = |
1963 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
1964 | QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number, | |
1965 | NULL, 0, 300); | |
5ca0121f | 1966 | |
a9ea2266 | 1967 | return result; |
5ca0121f GKH |
1968 | } |
1969 | ||
5ca0121f GKH |
1970 | /** |
1971 | * box_get_device | |
1972 | * Issue a GET_DEVICE vendor-specific request on the default control pipe If | |
1973 | * successful, fills in the qt_get_device_data structure pointed to by | |
1974 | * device_data, otherwise return a negative error number of the problem. | |
1975 | */ | |
1976 | static int box_get_device(struct usb_serial *serial, | |
1977 | struct qt_get_device_data *device_data) | |
1978 | { | |
1979 | int result; | |
1980 | __u16 current_length; | |
1981 | unsigned char *transfer_buffer; | |
1982 | ||
1983 | current_length = sizeof(struct qt_get_device_data); | |
1984 | transfer_buffer = kmalloc(current_length, GFP_KERNEL); | |
1985 | if (!transfer_buffer) | |
1986 | return -ENOMEM; | |
1987 | ||
1988 | result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), | |
1989 | QT_SET_GET_DEVICE, 0xc0, 0, 0, | |
1990 | transfer_buffer, current_length, 300); | |
1991 | if (result > 0) | |
1992 | memcpy(device_data, transfer_buffer, current_length); | |
1993 | kfree(transfer_buffer); | |
1994 | ||
1995 | return result; | |
1996 | } | |
1997 | ||
1998 | /** | |
1999 | * box_set_device | |
2000 | * Issue a SET_DEVICE vendor-specific request on the default control pipe If | |
2001 | * successful returns the number of bytes written, otherwise it returns a | |
2002 | * negative error number of the problem. | |
2003 | */ | |
a9ea2266 GKH |
2004 | static int box_set_device(struct usb_serial *serial, |
2005 | struct qt_get_device_data *device_data) | |
5ca0121f GKH |
2006 | { |
2007 | int result; | |
2008 | __u16 length; | |
2009 | __u16 PortSettings; | |
2010 | ||
a9ea2266 | 2011 | PortSettings = ((__u16) (device_data->portb)); |
5ca0121f | 2012 | PortSettings = (PortSettings << 8); |
a9ea2266 | 2013 | PortSettings += ((__u16) (device_data->porta)); |
5ca0121f GKH |
2014 | |
2015 | length = sizeof(struct qt_get_device_data); | |
c23ee6c3 | 2016 | mydbg("%s - PortSettings = 0x%x\n", __func__, PortSettings); |
5ca0121f GKH |
2017 | |
2018 | result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2019 | QT_SET_GET_DEVICE, 0x40, PortSettings, | |
2020 | 0, NULL, 0, 300); | |
2021 | return result; | |
2022 | } | |
2023 | ||
2024 | /**************************************************************************** | |
2025 | * BoxOPenCloseChannel | |
2026 | * This funciotn notifies the device that the device driver wishes to open a particular UART channel. its | |
2027 | * purpose is to allow the device driver and the device to synchronize state information. | |
2028 | * OpenClose = 1 for open , 0 for close | |
2029 | ****************************************************************************/ | |
a9ea2266 GKH |
2030 | static int BoxOPenCloseChannel(struct usb_serial *serial, __u16 Uart_Number, |
2031 | __u16 OpenClose, | |
2032 | struct qt_open_channel_data *pDeviceData) | |
5ca0121f | 2033 | { |
a9ea2266 GKH |
2034 | int result; |
2035 | __u16 length; | |
2036 | __u8 Direcion; | |
2037 | unsigned int pipe; | |
2038 | length = sizeof(struct qt_open_channel_data); | |
5ca0121f | 2039 | |
c23ee6c3 GKH |
2040 | /* if opening... */ |
2041 | if (OpenClose == 1) { | |
a9ea2266 GKH |
2042 | Direcion = USBD_TRANSFER_DIRECTION_IN; |
2043 | pipe = usb_rcvctrlpipe(serial->dev, 0); | |
2044 | result = | |
2045 | usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL, | |
2046 | Direcion, OpenClose, Uart_Number, | |
2047 | pDeviceData, length, 300); | |
2048 | ||
2049 | } else { | |
2050 | Direcion = USBD_TRANSFER_DIRECTION_OUT; | |
2051 | pipe = usb_sndctrlpipe(serial->dev, 0); | |
2052 | result = | |
2053 | usb_control_msg(serial->dev, pipe, QT_OPEN_CLOSE_CHANNEL, | |
2054 | Direcion, OpenClose, Uart_Number, NULL, 0, | |
2055 | 300); | |
5ca0121f | 2056 | |
a9ea2266 | 2057 | } |
5ca0121f | 2058 | |
a9ea2266 | 2059 | return result; |
5ca0121f GKH |
2060 | } |
2061 | ||
2062 | /**************************************************************************** | |
2063 | * BoxSetPrebufferLevel | |
2064 | TELLS BOX WHEN TO ASSERT FLOW CONTROL | |
2065 | ****************************************************************************/ | |
2066 | static int BoxSetPrebufferLevel(struct usb_serial *serial) | |
2067 | { | |
2068 | int result; | |
2069 | __u16 buffer_length; | |
2070 | ||
2071 | buffer_length = PREFUFF_LEVEL_CONSERVATIVE; | |
2072 | result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2073 | QT_GET_SET_PREBUF_TRIG_LVL, 0x40, | |
2074 | buffer_length, 0, NULL, 0, 300); | |
2075 | return result; | |
2076 | } | |
2077 | ||
5ca0121f GKH |
2078 | /**************************************************************************** |
2079 | * BoxSetATC | |
2080 | TELLS BOX WHEN TO ASSERT automatic transmitter control | |
2081 | ****************************************************************************/ | |
2082 | static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode) | |
2083 | { | |
a9ea2266 GKH |
2084 | int result; |
2085 | __u16 buffer_length; | |
5ca0121f | 2086 | |
a9ea2266 | 2087 | buffer_length = PREFUFF_LEVEL_CONSERVATIVE; |
5ca0121f | 2088 | |
a9ea2266 GKH |
2089 | result = |
2090 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2091 | QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300); | |
5ca0121f | 2092 | |
a9ea2266 | 2093 | return result; |
5ca0121f GKH |
2094 | } |
2095 | ||
2096 | /**************************************************************************** | |
2097 | * BoxSetUart | |
2098 | * issuse a SET_UART vendor-spcific request on the default control pipe | |
2099 | * If successful sets baud rate divisor and LCR value | |
2100 | ****************************************************************************/ | |
a9ea2266 GKH |
2101 | static int BoxSetUart(struct usb_serial *serial, unsigned short Uart_Number, |
2102 | unsigned short default_divisor, unsigned char default_LCR) | |
5ca0121f | 2103 | { |
a9ea2266 GKH |
2104 | int result; |
2105 | unsigned short UartNumandLCR; | |
5ca0121f | 2106 | |
a9ea2266 | 2107 | UartNumandLCR = (default_LCR << 8) + Uart_Number; |
5ca0121f | 2108 | |
a9ea2266 GKH |
2109 | result = |
2110 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2111 | QT_GET_SET_UART, 0x40, default_divisor, | |
2112 | UartNumandLCR, NULL, 0, 300); | |
5ca0121f | 2113 | |
a9ea2266 | 2114 | return result; |
5ca0121f GKH |
2115 | } |
2116 | ||
0c212603 | 2117 | static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index, |
a9ea2266 | 2118 | int bSet) |
5ca0121f | 2119 | { |
0c212603 AC |
2120 | __u8 mcr = 0; |
2121 | __u8 msr = 0, MOUT_Value = 0; | |
a9ea2266 GKH |
2122 | struct usb_serial_port *port; |
2123 | unsigned int status; | |
5ca0121f | 2124 | |
a9ea2266 | 2125 | port = serial->port; |
5ca0121f | 2126 | |
a9ea2266 | 2127 | if (bSet == 1) { |
c23ee6c3 | 2128 | /* flow control, box will clear RTS line to prevent remote */ |
0c212603 | 2129 | mcr = SERIAL_MCR_RTS; |
c23ee6c3 GKH |
2130 | } /* device from xmitting more chars */ |
2131 | else { | |
2132 | /* no flow control to remote device */ | |
0c212603 | 2133 | mcr = 0; |
5ca0121f | 2134 | |
a9ea2266 | 2135 | } |
0c212603 | 2136 | MOUT_Value = mcr << 8; |
5ca0121f | 2137 | |
a9ea2266 | 2138 | if (bSet == 1) { |
c23ee6c3 GKH |
2139 | /* flow control, box will inhibit xmit data if CTS line is |
2140 | * asserted */ | |
0c212603 | 2141 | msr = SERIAL_MSR_CTS; |
a9ea2266 | 2142 | } else { |
c23ee6c3 | 2143 | /* Box will not inhimbe xmit data due to CTS line */ |
0c212603 | 2144 | msr = 0; |
a9ea2266 | 2145 | } |
0c212603 | 2146 | MOUT_Value |= msr; |
5ca0121f | 2147 | |
a9ea2266 GKH |
2148 | status = |
2149 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2150 | QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, | |
0c212603 | 2151 | index, NULL, 0, 300); |
a9ea2266 | 2152 | return status; |
5ca0121f GKH |
2153 | |
2154 | } | |
2155 | ||
0c212603 | 2156 | static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index, |
a9ea2266 | 2157 | unsigned char stop_char, unsigned char start_char) |
5ca0121f | 2158 | { |
a9ea2266 GKH |
2159 | __u16 nSWflowout; |
2160 | int result; | |
5ca0121f | 2161 | |
a9ea2266 GKH |
2162 | nSWflowout = start_char << 8; |
2163 | nSWflowout = (unsigned short)stop_char; | |
5ca0121f | 2164 | |
a9ea2266 GKH |
2165 | result = |
2166 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2167 | QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, | |
0c212603 | 2168 | index, NULL, 0, 300); |
a9ea2266 | 2169 | return result; |
5ca0121f GKH |
2170 | |
2171 | } | |
0c212603 | 2172 | static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index) |
5ca0121f | 2173 | { |
a9ea2266 | 2174 | int result; |
5ca0121f | 2175 | |
a9ea2266 GKH |
2176 | result = |
2177 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
0c212603 | 2178 | QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index, |
a9ea2266 GKH |
2179 | NULL, 0, 300); |
2180 | return result; | |
5ca0121f GKH |
2181 | |
2182 | } | |
2183 | ||
a9ea2266 | 2184 | static void serial_throttle(struct tty_struct *tty) |
5ca0121f | 2185 | { |
a9ea2266 | 2186 | struct usb_serial_port *port = |
0c212603 | 2187 | tty->driver_data; |
c23ee6c3 GKH |
2188 | struct usb_serial *serial = get_usb_serial(port, __func__); |
2189 | mydbg("%s - port %d\n", __func__, port->number); | |
5ca0121f GKH |
2190 | |
2191 | if (!serial) | |
2192 | return; | |
2193 | ||
a9ea2266 | 2194 | down(&port->sem); |
5ca0121f GKH |
2195 | |
2196 | if (!port->open_count) { | |
c23ee6c3 | 2197 | mydbg("%s - port not open\n", __func__); |
5ca0121f GKH |
2198 | goto exit; |
2199 | } | |
c23ee6c3 GKH |
2200 | /* shut down any bulk reads that may be going on */ |
2201 | /* usb_unlink_urb (port->read_urb); */ | |
5ca0121f | 2202 | /* pass on to the driver specific version of this function */ |
a9ea2266 | 2203 | port->RxHolding = 1; |
c23ee6c3 | 2204 | mydbg("%s - port->RxHolding = 1\n", __func__); |
5ca0121f GKH |
2205 | |
2206 | exit: | |
a9ea2266 GKH |
2207 | up(&port->sem); |
2208 | return; | |
5ca0121f GKH |
2209 | } |
2210 | ||
a9ea2266 | 2211 | static void serial_unthrottle(struct tty_struct *tty) |
5ca0121f | 2212 | { |
a9ea2266 | 2213 | struct usb_serial_port *port = |
0c212603 | 2214 | tty->driver_data; |
c23ee6c3 | 2215 | struct usb_serial *serial = get_usb_serial(port, __func__); |
a9ea2266 | 2216 | unsigned int result; |
5ca0121f GKH |
2217 | |
2218 | if (!serial) | |
2219 | return; | |
a9ea2266 | 2220 | down(&port->sem); |
5ca0121f | 2221 | |
c23ee6c3 | 2222 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f GKH |
2223 | |
2224 | if (!port->open_count) { | |
c23ee6c3 | 2225 | mydbg("%s - port not open\n", __func__); |
5ca0121f GKH |
2226 | goto exit; |
2227 | } | |
2228 | ||
a9ea2266 | 2229 | if (port->RxHolding == 1) { |
c23ee6c3 | 2230 | mydbg("%s -port->RxHolding == 1\n", __func__); |
a9ea2266 GKH |
2231 | |
2232 | port->RxHolding = 0; | |
c23ee6c3 | 2233 | mydbg("%s - port->RxHolding = 0\n", __func__); |
a9ea2266 GKH |
2234 | |
2235 | /* if we have a bulk endpoint, start it up */ | |
2236 | if ((serial->num_bulk_in) && (port->ReadBulkStopped == 1)) { | |
2237 | /* Start reading from the device */ | |
2238 | usb_fill_bulk_urb(port->read_urb, serial->dev, | |
2239 | usb_rcvbulkpipe(serial->dev, | |
2240 | port-> | |
2241 | bulk_in_endpointAddress), | |
2242 | port->read_urb->transfer_buffer, | |
2243 | port->read_urb-> | |
2244 | transfer_buffer_length, | |
2245 | qt_read_bulk_callback, port); | |
2246 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); | |
2247 | if (result) | |
2248 | err("%s - failed restarting read urb, error %d", | |
c23ee6c3 | 2249 | __func__, result); |
a9ea2266 GKH |
2250 | } |
2251 | } | |
2252 | exit: | |
2253 | up(&port->sem); | |
2254 | return; | |
5ca0121f GKH |
2255 | |
2256 | } | |
2257 | ||
a9ea2266 | 2258 | static int serial_break(struct tty_struct *tty, int break_state) |
5ca0121f | 2259 | { |
0c212603 | 2260 | struct usb_serial_port *port = tty->driver_data; |
c23ee6c3 | 2261 | struct usb_serial *serial = get_usb_serial(port, __func__); |
0c212603 | 2262 | u16 index, onoff; |
a9ea2266 | 2263 | unsigned int result; |
5ca0121f | 2264 | |
0c212603 | 2265 | index = tty->index - serial->minor; |
5ca0121f GKH |
2266 | if (!serial) |
2267 | return -ENODEV; | |
2268 | ||
a9ea2266 | 2269 | if (break_state == -1) |
0c212603 | 2270 | onoff = 1; |
a9ea2266 | 2271 | else |
0c212603 | 2272 | onoff = 0; |
5ca0121f | 2273 | |
a9ea2266 | 2274 | down(&port->sem); |
5ca0121f | 2275 | |
c23ee6c3 | 2276 | mydbg("%s - port %d\n", __func__, port->number); |
5ca0121f GKH |
2277 | |
2278 | if (!port->open_count) { | |
c23ee6c3 | 2279 | mydbg("%s - port not open\n", __func__); |
5ca0121f GKH |
2280 | goto exit; |
2281 | } | |
2282 | ||
a9ea2266 GKH |
2283 | result = |
2284 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
0c212603 | 2285 | QT_BREAK_CONTROL, 0x40, onoff, index, |
a9ea2266 | 2286 | NULL, 0, 300); |
5ca0121f GKH |
2287 | |
2288 | exit: | |
a9ea2266 | 2289 | up(&port->sem); |
5ca0121f GKH |
2290 | return 0; |
2291 | } | |
2292 | ||
c23ee6c3 | 2293 | static int ioctl_serial_usb(struct inode *innod, struct file *filp, unsigned int cmd, |
a9ea2266 | 2294 | unsigned long arg) |
5ca0121f GKH |
2295 | { |
2296 | ||
a9ea2266 GKH |
2297 | unsigned err; |
2298 | unsigned ucOPR_NewValue, uc_Value; | |
2299 | int *p_Num_of_adapters, counts, index, *p_QMCR_Value; | |
2300 | struct identity *p_Identity_of; | |
2301 | struct identity Identity_of; | |
2302 | struct usb_serial *lastserial, *serial; | |
2303 | ||
2304 | mydbg(KERN_DEBUG "ioctl_serial_usb cmd =\n"); | |
2305 | if (_IOC_TYPE(cmd) != SERIALQT_PCI_IOC_MAGIC) | |
2306 | return -ENOTTY; | |
2307 | if (_IOC_NR(cmd) > SERIALQT_IOC_MAXNR) | |
2308 | return -ENOTTY; | |
2309 | mydbg(KERN_DEBUG "ioctl_serial_usb cmd = 0x%x\n", cmd); | |
2310 | err = 0; | |
2311 | switch (cmd) { | |
2312 | ||
2313 | case SERIALQT_WRITE_QMCR: | |
c23ee6c3 | 2314 | err = -ENOTTY; |
a9ea2266 GKH |
2315 | index = arg >> 16; |
2316 | counts = 0; | |
2317 | ||
2318 | ucOPR_NewValue = arg; | |
2319 | ||
2320 | err = EmulateWriteQMCR_Reg(index, ucOPR_NewValue); | |
2321 | break; | |
2322 | ||
2323 | case SERIALQT_READ_QMCR: | |
c23ee6c3 | 2324 | err = -ENOTTY; |
a9ea2266 GKH |
2325 | p_QMCR_Value = (int *)arg; |
2326 | index = arg >> 16; | |
2327 | counts = 0; | |
2328 | ||
2329 | err = EmulateReadQMCR_Reg(index, &uc_Value); | |
c23ee6c3 | 2330 | if (err == 0) |
a9ea2266 | 2331 | err = put_user(uc_Value, p_QMCR_Value); |
a9ea2266 GKH |
2332 | break; |
2333 | ||
2334 | case SERIALQT_GET_NUMOF_UNITS: | |
2335 | p_Num_of_adapters = (int *)arg; | |
c23ee6c3 GKH |
2336 | counts = 0; /* Initialize counts to zero */ |
2337 | /* struct usb_serial *lastserial = serial_table[0], *serial; */ | |
a9ea2266 GKH |
2338 | lastserial = serial_table[0]; |
2339 | ||
2340 | mydbg(KERN_DEBUG "SERIALQT_GET_NUMOF_UNITS \n"); | |
c23ee6c3 | 2341 | /* if first pointer is nonull, we at least have one box */ |
a9ea2266 | 2342 | if (lastserial) |
c23ee6c3 | 2343 | counts = 1; /* we at least have one box */ |
a9ea2266 GKH |
2344 | |
2345 | for (index = 1; index < SERIAL_TTY_MINORS; index++) { | |
2346 | serial = serial_table[index]; | |
2347 | if (serial) { | |
2348 | if (serial != lastserial) { | |
c23ee6c3 GKH |
2349 | /* we had a change in the array, hence |
2350 | * another box is there */ | |
a9ea2266 GKH |
2351 | lastserial = serial; |
2352 | counts++; | |
2353 | } | |
2354 | } else | |
2355 | break; | |
2356 | } | |
5ca0121f | 2357 | |
a9ea2266 GKH |
2358 | mydbg(KERN_DEBUG "ioctl_serial_usb writting counts = %d", |
2359 | counts); | |
2360 | ||
2361 | err = put_user(counts, p_Num_of_adapters); | |
2362 | ||
2363 | break; | |
2364 | case SERIALQT_GET_THIS_UNIT: | |
2365 | counts = 0; | |
2366 | p_Identity_of = (struct identity *)arg; | |
c23ee6c3 | 2367 | /* copy user structure to local variable */ |
a9ea2266 GKH |
2368 | get_user(Identity_of.index, &p_Identity_of->index); |
2369 | mydbg(KERN_DEBUG "SERIALQT_GET_THIS_UNIT Identity_of.index\n"); | |
2370 | mydbg(KERN_DEBUG | |
2371 | "SERIALQT_GET_THIS_UNIT Identity_of.index= 0x%x\n", | |
2372 | Identity_of.index); | |
2373 | ||
c23ee6c3 | 2374 | err = -ENOTTY; |
a9ea2266 GKH |
2375 | serial = find_the_box(Identity_of.index); |
2376 | if (serial) { | |
2377 | err = | |
2378 | put_user(serial->product, | |
2379 | &p_Identity_of->n_identity); | |
5ca0121f | 2380 | |
a9ea2266 GKH |
2381 | } |
2382 | break; | |
2383 | ||
2384 | case SERIALQT_IS422_EXTENDED: | |
c23ee6c3 | 2385 | err = -ENOTTY; |
a9ea2266 | 2386 | mydbg(KERN_DEBUG "SERIALQT_IS422_EXTENDED \n"); |
a9ea2266 GKH |
2387 | index = arg >> 16; |
2388 | ||
2389 | counts = 0; | |
2390 | ||
2391 | mydbg(KERN_DEBUG | |
2392 | "SERIALQT_IS422_EXTENDED, looking Identity_of.indext = 0x%x\n", | |
2393 | index); | |
2394 | serial = find_the_box(index); | |
2395 | if (serial) { | |
c23ee6c3 | 2396 | mydbg("%s index = 0x%x, serial = 0x%p\n", __func__, |
a9ea2266 GKH |
2397 | index, serial); |
2398 | for (counts = 0; serqt_422_table[counts] != 0; counts++) { | |
2399 | ||
2400 | mydbg | |
2401 | ("%s serial->product = = 0x%x, serqt_422_table[counts] = 0x%x\n", | |
c23ee6c3 | 2402 | __func__, serial->product, |
a9ea2266 GKH |
2403 | serqt_422_table[counts]); |
2404 | if (serial->product == serqt_422_table[counts]) { | |
2405 | err = 0; | |
2406 | ||
2407 | mydbg | |
2408 | ("%s found match for 422extended\n", | |
c23ee6c3 | 2409 | __func__); |
a9ea2266 GKH |
2410 | break; |
2411 | } | |
2412 | } | |
2413 | } | |
2414 | break; | |
5ca0121f | 2415 | |
a9ea2266 GKH |
2416 | default: |
2417 | err = -ENOTTY; | |
c23ee6c3 | 2418 | } |
5ca0121f | 2419 | |
c23ee6c3 | 2420 | mydbg("%s returning err = 0x%x\n", __func__, err); |
a9ea2266 GKH |
2421 | return err; |
2422 | } | |
5ca0121f | 2423 | |
a9ea2266 GKH |
2424 | static struct usb_serial *find_the_box(unsigned int index) |
2425 | { | |
2426 | struct usb_serial *lastserial, *foundserial, *serial; | |
2427 | int counts = 0, index2; | |
2428 | lastserial = serial_table[0]; | |
2429 | foundserial = NULL; | |
2430 | for (index2 = 0; index2 < SERIAL_TTY_MINORS; index2++) { | |
2431 | serial = serial_table[index2]; | |
2432 | ||
2433 | mydbg("%s index = 0x%x, index2 = 0x%x, serial = 0x%p\n", | |
c23ee6c3 | 2434 | __func__, index, index2, serial); |
a9ea2266 GKH |
2435 | |
2436 | if (serial) { | |
c23ee6c3 | 2437 | /* first see if this is the unit we'er looking for */ |
a9ea2266 GKH |
2438 | mydbg |
2439 | ("%s inside if(serial) counts = 0x%x , index = 0x%x\n", | |
c23ee6c3 | 2440 | __func__, counts, index); |
a9ea2266 | 2441 | if (counts == index) { |
c23ee6c3 GKH |
2442 | /* we found the one we're looking for, copythe |
2443 | * product Id to user */ | |
2444 | mydbg("%s we found the one we're looking for serial = 0x%p\n", | |
2445 | __func__, serial); | |
a9ea2266 GKH |
2446 | foundserial = serial; |
2447 | break; | |
2448 | } | |
2449 | ||
2450 | if (serial != lastserial) { | |
c23ee6c3 | 2451 | /* when we have a change in the pointer */ |
a9ea2266 GKH |
2452 | lastserial = serial; |
2453 | counts++; | |
2454 | } | |
2455 | } else | |
c23ee6c3 | 2456 | break; /* no matches */ |
a9ea2266 | 2457 | } |
5ca0121f | 2458 | |
c23ee6c3 | 2459 | mydbg("%s returning foundserial = 0x%p\n", __func__, foundserial); |
a9ea2266 GKH |
2460 | return foundserial; |
2461 | } | |
5ca0121f | 2462 | |
a9ea2266 GKH |
2463 | static int EmulateWriteQMCR_Reg(int index, unsigned uc_value) |
2464 | { | |
5ca0121f | 2465 | |
a9ea2266 GKH |
2466 | __u16 ATC_Mode = 0; |
2467 | struct usb_serial *serial; | |
2468 | int status; | |
2469 | struct qt_get_device_data DeviceData; | |
2470 | unsigned uc_temp = 0; | |
c23ee6c3 | 2471 | mydbg("Inside %s, uc_value = 0x%x\n", __func__, uc_value); |
a9ea2266 GKH |
2472 | |
2473 | DeviceData.porta = 0; | |
2474 | DeviceData.portb = 0; | |
2475 | serial = find_the_box(index); | |
c23ee6c3 | 2476 | /* Determine Duplex mode */ |
a9ea2266 GKH |
2477 | if (!(serial)) |
2478 | return -ENOTTY; | |
2479 | status = box_get_device(serial, &DeviceData); | |
2480 | if (status < 0) { | |
2481 | mydbg(__FILE__ "box_set_device failed\n"); | |
2482 | return status; | |
2483 | } | |
5ca0121f | 2484 | |
a9ea2266 GKH |
2485 | uc_temp = uc_value & QMCR_HALF_DUPLEX_MASK; |
2486 | switch (uc_temp) { | |
2487 | case QMCR_FULL_DUPLEX: | |
2488 | DeviceData.porta &= ~DUPMODE_BITS; | |
2489 | DeviceData.porta |= FULL_DUPLEX; | |
2490 | ATC_Mode = ATC_DISABLED; | |
2491 | break; | |
2492 | case QMCR_HALF_DUPLEX_RTS: | |
2493 | DeviceData.porta &= ~DUPMODE_BITS; | |
2494 | DeviceData.porta |= HALF_DUPLEX_RTS; | |
2495 | ATC_Mode = ATC_RTS_ENABLED; | |
2496 | break; | |
2497 | case QMCR_HALF_DUPLEX_DTR: | |
2498 | DeviceData.porta &= ~DUPMODE_BITS; | |
2499 | DeviceData.porta |= HALF_DUPLEX_DTR; | |
2500 | ATC_Mode = ATC_DTR_ENABLED; | |
2501 | break; | |
2502 | default: | |
2503 | break; | |
2504 | } | |
5ca0121f | 2505 | |
a9ea2266 GKH |
2506 | uc_temp = uc_value & QMCR_CONNECTOR_MASK; |
2507 | switch (uc_temp) { | |
2508 | case QMCR_MODEM_CONTROL: | |
c23ee6c3 | 2509 | DeviceData.portb &= ~LOOPMODE_BITS; /* reset connection bits */ |
a9ea2266 GKH |
2510 | DeviceData.portb |= MODEM_CTRL; |
2511 | break; | |
2512 | case QMCR_ALL_LOOPBACK: | |
c23ee6c3 | 2513 | DeviceData.portb &= ~LOOPMODE_BITS; /* reset connection bits */ |
a9ea2266 GKH |
2514 | DeviceData.portb |= ALL_LOOPBACK; |
2515 | break; | |
2516 | } | |
5ca0121f | 2517 | |
a9ea2266 GKH |
2518 | mydbg(__FILE__ "Calling box_set_device with failed\n"); |
2519 | status = box_set_device(serial, &DeviceData); | |
2520 | if (status < 0) { | |
2521 | mydbg(__FILE__ "box_set_device failed\n"); | |
2522 | return status; | |
2523 | } | |
5ca0121f | 2524 | |
c23ee6c3 GKH |
2525 | /* This bit (otherwise unused) i'll used to detect whether ATC is |
2526 | * selected */ | |
2527 | if (uc_value & QMCR_RX_EN_MASK) { | |
5ca0121f | 2528 | |
a9ea2266 GKH |
2529 | mydbg(__FILE__ |
2530 | "calling BoxsetATC with DeviceData.porta = 0x%x and DeviceData.portb = 0x%x\n", | |
2531 | DeviceData.porta, DeviceData.portb); | |
2532 | status = BoxSetATC(serial, ATC_Mode); | |
2533 | if (status < 0) { | |
2534 | mydbg(__FILE__ "BoxSetATC failed\n"); | |
2535 | return status; | |
2536 | } | |
2537 | } else { | |
2538 | ||
2539 | mydbg(__FILE__ | |
2540 | "calling BoxsetATC with DeviceData.porta = 0x%x and DeviceData.portb = 0x%x\n", | |
2541 | DeviceData.porta, DeviceData.portb); | |
2542 | status = BoxSetATC(serial, ATC_DISABLED); | |
2543 | if (status < 0) { | |
2544 | mydbg(__FILE__ "BoxSetATC failed\n"); | |
2545 | return status; | |
2546 | } | |
2547 | } | |
5ca0121f | 2548 | |
a9ea2266 | 2549 | return 0; |
5ca0121f | 2550 | |
5ca0121f GKH |
2551 | } |
2552 | ||
a9ea2266 GKH |
2553 | static int EmulateReadQMCR_Reg(int index, unsigned *uc_value) |
2554 | { | |
2555 | struct usb_serial *serial; | |
2556 | int status; | |
2557 | struct qt_get_device_data DeviceData; | |
2558 | __u8 uc_temp; | |
5ca0121f | 2559 | |
a9ea2266 | 2560 | *uc_value = 0; |
5ca0121f | 2561 | |
a9ea2266 GKH |
2562 | serial = find_the_box(index); |
2563 | if (!(serial)) | |
2564 | return -ENOTTY; | |
5ca0121f | 2565 | |
a9ea2266 GKH |
2566 | status = box_get_device(serial, &DeviceData); |
2567 | if (status < 0) { | |
2568 | mydbg(__FILE__ "box_get_device failed\n"); | |
2569 | return status; | |
2570 | } | |
2571 | uc_temp = DeviceData.porta & DUPMODE_BITS; | |
2572 | switch (uc_temp) { | |
2573 | case FULL_DUPLEX: | |
2574 | *uc_value |= QMCR_FULL_DUPLEX; | |
2575 | break; | |
2576 | case HALF_DUPLEX_RTS: | |
2577 | *uc_value |= QMCR_HALF_DUPLEX_RTS; | |
2578 | break; | |
2579 | case HALF_DUPLEX_DTR: | |
2580 | *uc_value |= QMCR_HALF_DUPLEX_DTR; | |
2581 | break; | |
2582 | default: | |
2583 | break; | |
2584 | } | |
5ca0121f | 2585 | |
c23ee6c3 GKH |
2586 | /* I use this for ATC control se */ |
2587 | uc_temp = DeviceData.portb & LOOPMODE_BITS; | |
5ca0121f | 2588 | |
a9ea2266 GKH |
2589 | switch (uc_temp) { |
2590 | case ALL_LOOPBACK: | |
2591 | *uc_value |= QMCR_ALL_LOOPBACK; | |
2592 | break; | |
2593 | case MODEM_CTRL: | |
2594 | *uc_value |= QMCR_MODEM_CONTROL; | |
2595 | break; | |
2596 | default: | |
2597 | break; | |
5ca0121f | 2598 | |
a9ea2266 GKH |
2599 | } |
2600 | return 0; | |
5ca0121f | 2601 | |
a9ea2266 | 2602 | } |
5ca0121f GKH |
2603 | |
2604 | static int __init serqt_usb_init(void) | |
2605 | { | |
2606 | int i, result; | |
2607 | int status = 0; | |
2608 | ||
c23ee6c3 | 2609 | mydbg("%s\n", __func__); |
5ca0121f GKH |
2610 | tty_set_operations(&serial_tty_driver, &serial_ops); |
2611 | result = tty_register_driver(&serial_tty_driver); | |
2612 | if (result) { | |
2613 | mydbg("tty_register_driver failed error = 0x%x", result); | |
2614 | return result; | |
2615 | } | |
2616 | ||
2617 | /* Initalize our global data */ | |
2618 | for (i = 0; i < SERIAL_TTY_MINORS; ++i) | |
2619 | serial_table[i] = NULL; | |
2620 | ||
2621 | /* register this driver with the USB subsystem */ | |
2622 | result = usb_register(&serqt_usb_driver); | |
2623 | if (result < 0) { | |
a9ea2266 GKH |
2624 | err("usb_register failed for the " __FILE__ |
2625 | " driver. Error number %d", result); | |
5ca0121f GKH |
2626 | return result; |
2627 | } | |
c23ee6c3 | 2628 | status = 0; /* Dynamic assignment of major number */ |
a9ea2266 GKH |
2629 | major_number = |
2630 | register_chrdev(status, "SerialQT_USB", &serialqt_usb_fops); | |
5ca0121f | 2631 | if (major_number < 0) { |
a9ea2266 | 2632 | mydbg(KERN_DEBUG "No devices found \n\n"); |
5ca0121f GKH |
2633 | return -EBUSY; |
2634 | } else | |
a9ea2266 GKH |
2635 | mydbg(KERN_DEBUG "SerQT_USB major number assignment = %d \n\n", |
2636 | major_number); | |
5ca0121f GKH |
2637 | |
2638 | printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION); | |
2639 | return 0; | |
2640 | } | |
2641 | ||
2642 | static void __exit serqt_usb_exit(void) | |
2643 | { | |
a9ea2266 GKH |
2644 | /* deregister this driver with the USB subsystem */ |
2645 | usb_deregister(&serqt_usb_driver); | |
2646 | tty_unregister_driver(&serial_tty_driver); | |
2647 | unregister_chrdev(major_number, "SerialQT_USB"); | |
5ca0121f GKH |
2648 | } |
2649 | ||
2650 | module_init(serqt_usb_init); | |
2651 | module_exit(serqt_usb_exit); | |
2652 | ||
2653 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
2654 | MODULE_DESCRIPTION(DRIVER_DESC); | |
2655 | MODULE_LICENSE("GPL"); |