Commit | Line | Data |
---|---|---|
3f542974 PS |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
15 | * | |
16 | * Clean ups from Moschip version and a few ioctl implementations by: | |
17 | * Paul B Schroeder <pschroeder "at" uplogix "dot" com> | |
18 | * | |
19 | * Originally based on drivers/usb/serial/io_edgeport.c which is: | |
20 | * Copyright (C) 2000 Inside Out Networks, All rights reserved. | |
21 | * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <linux/kernel.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/tty.h> | |
30 | #include <linux/tty_driver.h> | |
31 | #include <linux/tty_flip.h> | |
32 | #include <linux/module.h> | |
33 | #include <linux/serial.h> | |
34 | #include <linux/usb.h> | |
35 | #include <linux/usb/serial.h> | |
880af9db | 36 | #include <linux/uaccess.h> |
3f542974 PS |
37 | |
38 | /* | |
39 | * Version Information | |
40 | */ | |
37768adf | 41 | #define DRIVER_VERSION "1.3.2" |
3f542974 PS |
42 | #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" |
43 | ||
44 | /* | |
45 | * 16C50 UART register defines | |
46 | */ | |
47 | ||
48 | #define LCR_BITS_5 0x00 /* 5 bits/char */ | |
49 | #define LCR_BITS_6 0x01 /* 6 bits/char */ | |
50 | #define LCR_BITS_7 0x02 /* 7 bits/char */ | |
51 | #define LCR_BITS_8 0x03 /* 8 bits/char */ | |
52 | #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ | |
53 | ||
54 | #define LCR_STOP_1 0x00 /* 1 stop bit */ | |
55 | #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ | |
56 | #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ | |
57 | #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ | |
58 | ||
59 | #define LCR_PAR_NONE 0x00 /* No parity */ | |
60 | #define LCR_PAR_ODD 0x08 /* Odd parity */ | |
61 | #define LCR_PAR_EVEN 0x18 /* Even parity */ | |
62 | #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ | |
63 | #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ | |
64 | #define LCR_PAR_MASK 0x38 /* Mask for parity field */ | |
65 | ||
66 | #define LCR_SET_BREAK 0x40 /* Set Break condition */ | |
67 | #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ | |
68 | ||
69 | #define MCR_DTR 0x01 /* Assert DTR */ | |
70 | #define MCR_RTS 0x02 /* Assert RTS */ | |
71 | #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ | |
72 | #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ | |
73 | #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ | |
74 | #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ | |
75 | ||
76 | #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ | |
77 | #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ | |
78 | #define MOS7840_MSR_RI 0x40 /* Current state of RI */ | |
79 | #define MOS7840_MSR_CD 0x80 /* Current state of CD */ | |
80 | ||
81 | /* | |
82 | * Defines used for sending commands to port | |
83 | */ | |
84 | ||
1e658489 | 85 | #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ |
3f542974 PS |
86 | |
87 | #define MOS_PORT1 0x0200 | |
88 | #define MOS_PORT2 0x0300 | |
89 | #define MOS_VENREG 0x0000 | |
90 | #define MOS_MAX_PORT 0x02 | |
91 | #define MOS_WRITE 0x0E | |
92 | #define MOS_READ 0x0D | |
93 | ||
94 | /* Requests */ | |
95 | #define MCS_RD_RTYPE 0xC0 | |
96 | #define MCS_WR_RTYPE 0x40 | |
97 | #define MCS_RDREQ 0x0D | |
98 | #define MCS_WRREQ 0x0E | |
99 | #define MCS_CTRL_TIMEOUT 500 | |
100 | #define VENDOR_READ_LENGTH (0x01) | |
101 | ||
102 | #define MAX_NAME_LEN 64 | |
103 | ||
880af9db AC |
104 | #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ |
105 | #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ | |
3f542974 PS |
106 | |
107 | /* For higher baud Rates use TIOCEXBAUD */ | |
108 | #define TIOCEXBAUD 0x5462 | |
109 | ||
110 | /* vendor id and device id defines */ | |
111 | ||
11e1abb4 | 112 | /* The native mos7840/7820 component */ |
3f542974 PS |
113 | #define USB_VENDOR_ID_MOSCHIP 0x9710 |
114 | #define MOSCHIP_DEVICE_ID_7840 0x7840 | |
115 | #define MOSCHIP_DEVICE_ID_7820 0x7820 | |
0eafe4de | 116 | #define MOSCHIP_DEVICE_ID_7810 0x7810 |
11e1abb4 DL |
117 | /* The native component can have its vendor/device id's overridden |
118 | * in vendor-specific implementations. Such devices can be handled | |
68e24113 | 119 | * by making a change here, in id_table. |
11e1abb4 | 120 | */ |
870408c8 DL |
121 | #define USB_VENDOR_ID_BANDB 0x0856 |
122 | #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 | |
123 | #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 | |
124 | #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 | |
125 | #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 | |
126 | #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 | |
127 | #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 | |
128 | #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 | |
129 | #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 | |
130 | #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 | |
131 | #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 | |
132 | #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 | |
133 | #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 | |
134 | #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 | |
3f542974 | 135 | |
9d498bea RL |
136 | /* This driver also supports |
137 | * ATEN UC2324 device using Moschip MCS7840 | |
138 | * ATEN UC2322 device using Moschip MCS7820 | |
139 | */ | |
e9b8cffa TC |
140 | #define USB_VENDOR_ID_ATENINTL 0x0557 |
141 | #define ATENINTL_DEVICE_ID_UC2324 0x2011 | |
9d498bea | 142 | #define ATENINTL_DEVICE_ID_UC2322 0x7820 |
e9b8cffa | 143 | |
11e1abb4 | 144 | /* Interrupt Routine Defines */ |
3f542974 PS |
145 | |
146 | #define SERIAL_IIR_RLS 0x06 | |
147 | #define SERIAL_IIR_MS 0x00 | |
148 | ||
149 | /* | |
150 | * Emulation of the bit mask on the LINE STATUS REGISTER. | |
151 | */ | |
152 | #define SERIAL_LSR_DR 0x0001 | |
153 | #define SERIAL_LSR_OE 0x0002 | |
154 | #define SERIAL_LSR_PE 0x0004 | |
155 | #define SERIAL_LSR_FE 0x0008 | |
156 | #define SERIAL_LSR_BI 0x0010 | |
157 | ||
158 | #define MOS_MSR_DELTA_CTS 0x10 | |
159 | #define MOS_MSR_DELTA_DSR 0x20 | |
160 | #define MOS_MSR_DELTA_RI 0x40 | |
161 | #define MOS_MSR_DELTA_CD 0x80 | |
162 | ||
880af9db | 163 | /* Serial Port register Address */ |
3f542974 PS |
164 | #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) |
165 | #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) | |
166 | #define LINE_CONTROL_REGISTER ((__u16)(0x03)) | |
167 | #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) | |
168 | #define LINE_STATUS_REGISTER ((__u16)(0x05)) | |
169 | #define MODEM_STATUS_REGISTER ((__u16)(0x06)) | |
170 | #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) | |
171 | #define DIVISOR_LATCH_LSB ((__u16)(0x00)) | |
172 | #define DIVISOR_LATCH_MSB ((__u16)(0x01)) | |
173 | ||
174 | #define CLK_MULTI_REGISTER ((__u16)(0x02)) | |
175 | #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) | |
093ea2d3 | 176 | #define GPIO_REGISTER ((__u16)(0x07)) |
3f542974 PS |
177 | |
178 | #define SERIAL_LCR_DLAB ((__u16)(0x0080)) | |
179 | ||
180 | /* | |
181 | * URB POOL related defines | |
182 | */ | |
183 | #define NUM_URBS 16 /* URB Count */ | |
184 | #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ | |
185 | ||
0eafe4de D |
186 | /* LED on/off milliseconds*/ |
187 | #define LED_ON_MS 500 | |
188 | #define LED_OFF_MS 500 | |
189 | ||
190 | static int device_type; | |
3f542974 | 191 | |
b9c87663 | 192 | static const struct usb_device_id id_table[] = { |
3f542974 PS |
193 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, |
194 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, | |
0eafe4de | 195 | {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, |
acf509ae | 196 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, |
870408c8 | 197 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, |
acf509ae | 198 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, |
870408c8 | 199 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, |
acf509ae CB |
200 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, |
201 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, | |
202 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, | |
203 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, | |
11e1abb4 | 204 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, |
870408c8 | 205 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, |
acf509ae | 206 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, |
870408c8 | 207 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, |
27f1281d | 208 | {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, |
e9b8cffa | 209 | {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, |
9d498bea | 210 | {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, |
3f542974 PS |
211 | {} /* terminating entry */ |
212 | }; | |
68e24113 | 213 | MODULE_DEVICE_TABLE(usb, id_table); |
3f542974 PS |
214 | |
215 | /* This structure holds all of the local port information */ | |
216 | ||
217 | struct moschip_port { | |
218 | int port_num; /*Actual port number in the device(1,2,etc) */ | |
219 | struct urb *write_urb; /* write URB for this port */ | |
220 | struct urb *read_urb; /* read URB for this port */ | |
0de9a702 | 221 | struct urb *int_urb; |
3f542974 PS |
222 | __u8 shadowLCR; /* last LCR value received */ |
223 | __u8 shadowMCR; /* last MCR value received */ | |
224 | char open; | |
0de9a702 ON |
225 | char open_ports; |
226 | char zombie; | |
3f542974 PS |
227 | wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ |
228 | wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ | |
229 | int delta_msr_cond; | |
230 | struct async_icount icount; | |
231 | struct usb_serial_port *port; /* loop back to the owner of this object */ | |
232 | ||
880af9db | 233 | /* Offsets */ |
3f542974 PS |
234 | __u8 SpRegOffset; |
235 | __u8 ControlRegOffset; | |
236 | __u8 DcrRegOffset; | |
880af9db | 237 | /* for processing control URBS in interrupt context */ |
3f542974 | 238 | struct urb *control_urb; |
0de9a702 | 239 | struct usb_ctrlrequest *dr; |
3f542974 PS |
240 | char *ctrl_buf; |
241 | int MsrLsr; | |
242 | ||
0de9a702 | 243 | spinlock_t pool_lock; |
3f542974 | 244 | struct urb *write_urb_pool[NUM_URBS]; |
0de9a702 | 245 | char busy[NUM_URBS]; |
50de36f7 | 246 | bool read_urb_busy; |
3f542974 | 247 | |
0eafe4de D |
248 | /* For device(s) with LED indicator */ |
249 | bool has_led; | |
250 | bool led_flag; | |
251 | struct timer_list led_timer1; /* Timer for LED on */ | |
252 | struct timer_list led_timer2; /* Timer for LED off */ | |
253 | }; | |
3f542974 | 254 | |
3f542974 PS |
255 | /* |
256 | * mos7840_set_reg_sync | |
257 | * To set the Control register by calling usb_fill_control_urb function | |
258 | * by passing usb_sndctrlpipe function as parameter. | |
259 | */ | |
260 | ||
261 | static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, | |
262 | __u16 val) | |
263 | { | |
264 | struct usb_device *dev = port->serial->dev; | |
265 | val = val & 0x00ff; | |
9c134a14 | 266 | dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val); |
3f542974 PS |
267 | |
268 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, | |
269 | MCS_WR_RTYPE, val, reg, NULL, 0, | |
270 | MOS_WDR_TIMEOUT); | |
271 | } | |
272 | ||
273 | /* | |
274 | * mos7840_get_reg_sync | |
275 | * To set the Uart register by calling usb_fill_control_urb function by | |
276 | * passing usb_rcvctrlpipe function as parameter. | |
277 | */ | |
278 | ||
279 | static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, | |
880af9db | 280 | __u16 *val) |
3f542974 PS |
281 | { |
282 | struct usb_device *dev = port->serial->dev; | |
283 | int ret = 0; | |
9e221a35 JH |
284 | u8 *buf; |
285 | ||
286 | buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); | |
287 | if (!buf) | |
288 | return -ENOMEM; | |
3f542974 PS |
289 | |
290 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, | |
9e221a35 | 291 | MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, |
3f542974 | 292 | MOS_WDR_TIMEOUT); |
9e221a35 | 293 | *val = buf[0]; |
9c134a14 | 294 | dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val); |
9e221a35 JH |
295 | |
296 | kfree(buf); | |
3f542974 PS |
297 | return ret; |
298 | } | |
299 | ||
300 | /* | |
301 | * mos7840_set_uart_reg | |
302 | * To set the Uart register by calling usb_fill_control_urb function by | |
303 | * passing usb_sndctrlpipe function as parameter. | |
304 | */ | |
305 | ||
306 | static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, | |
307 | __u16 val) | |
308 | { | |
309 | ||
310 | struct usb_device *dev = port->serial->dev; | |
311 | val = val & 0x00ff; | |
880af9db AC |
312 | /* For the UART control registers, the application number need |
313 | to be Or'ed */ | |
0de9a702 | 314 | if (port->serial->num_ports == 4) { |
880af9db AC |
315 | val |= (((__u16) port->number - |
316 | (__u16) (port->serial->minor)) + 1) << 8; | |
3f542974 PS |
317 | } else { |
318 | if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { | |
880af9db | 319 | val |= (((__u16) port->number - |
3f542974 | 320 | (__u16) (port->serial->minor)) + 1) << 8; |
3f542974 | 321 | } else { |
9c134a14 | 322 | val |= (((__u16) port->number - |
3f542974 | 323 | (__u16) (port->serial->minor)) + 2) << 8; |
3f542974 PS |
324 | } |
325 | } | |
9c134a14 | 326 | dev_dbg(&port->dev, "%s application number is %x\n", __func__, val); |
3f542974 PS |
327 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, |
328 | MCS_WR_RTYPE, val, reg, NULL, 0, | |
329 | MOS_WDR_TIMEOUT); | |
330 | ||
331 | } | |
332 | ||
333 | /* | |
334 | * mos7840_get_uart_reg | |
335 | * To set the Control register by calling usb_fill_control_urb function | |
336 | * by passing usb_rcvctrlpipe function as parameter. | |
337 | */ | |
338 | static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, | |
880af9db | 339 | __u16 *val) |
3f542974 PS |
340 | { |
341 | struct usb_device *dev = port->serial->dev; | |
342 | int ret = 0; | |
343 | __u16 Wval; | |
9e221a35 JH |
344 | u8 *buf; |
345 | ||
346 | buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); | |
347 | if (!buf) | |
348 | return -ENOMEM; | |
3f542974 | 349 | |
880af9db | 350 | /* Wval is same as application number */ |
0de9a702 | 351 | if (port->serial->num_ports == 4) { |
3f542974 PS |
352 | Wval = |
353 | (((__u16) port->number - (__u16) (port->serial->minor)) + | |
354 | 1) << 8; | |
3f542974 PS |
355 | } else { |
356 | if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { | |
880af9db | 357 | Wval = (((__u16) port->number - |
3f542974 | 358 | (__u16) (port->serial->minor)) + 1) << 8; |
3f542974 | 359 | } else { |
880af9db | 360 | Wval = (((__u16) port->number - |
3f542974 | 361 | (__u16) (port->serial->minor)) + 2) << 8; |
3f542974 PS |
362 | } |
363 | } | |
9c134a14 | 364 | dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval); |
3f542974 | 365 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, |
9e221a35 | 366 | MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, |
3f542974 | 367 | MOS_WDR_TIMEOUT); |
9e221a35 JH |
368 | *val = buf[0]; |
369 | ||
370 | kfree(buf); | |
3f542974 PS |
371 | return ret; |
372 | } | |
373 | ||
9c134a14 GKH |
374 | static void mos7840_dump_serial_port(struct usb_serial_port *port, |
375 | struct moschip_port *mos7840_port) | |
3f542974 PS |
376 | { |
377 | ||
9c134a14 GKH |
378 | dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset); |
379 | dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset); | |
380 | dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset); | |
3f542974 PS |
381 | |
382 | } | |
383 | ||
384 | /************************************************************************/ | |
385 | /************************************************************************/ | |
386 | /* I N T E R F A C E F U N C T I O N S */ | |
387 | /* I N T E R F A C E F U N C T I O N S */ | |
388 | /************************************************************************/ | |
389 | /************************************************************************/ | |
390 | ||
391 | static inline void mos7840_set_port_private(struct usb_serial_port *port, | |
392 | struct moschip_port *data) | |
393 | { | |
394 | usb_set_serial_port_data(port, (void *)data); | |
395 | } | |
396 | ||
397 | static inline struct moschip_port *mos7840_get_port_private(struct | |
398 | usb_serial_port | |
399 | *port) | |
400 | { | |
401 | return (struct moschip_port *)usb_get_serial_port_data(port); | |
402 | } | |
403 | ||
0de9a702 | 404 | static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) |
3f542974 PS |
405 | { |
406 | struct moschip_port *mos7840_port; | |
407 | struct async_icount *icount; | |
408 | mos7840_port = port; | |
409 | icount = &mos7840_port->icount; | |
410 | if (new_msr & | |
411 | (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | | |
412 | MOS_MSR_DELTA_CD)) { | |
413 | icount = &mos7840_port->icount; | |
414 | ||
415 | /* update input line counters */ | |
416 | if (new_msr & MOS_MSR_DELTA_CTS) { | |
417 | icount->cts++; | |
0de9a702 | 418 | smp_wmb(); |
3f542974 PS |
419 | } |
420 | if (new_msr & MOS_MSR_DELTA_DSR) { | |
421 | icount->dsr++; | |
0de9a702 | 422 | smp_wmb(); |
3f542974 PS |
423 | } |
424 | if (new_msr & MOS_MSR_DELTA_CD) { | |
425 | icount->dcd++; | |
0de9a702 | 426 | smp_wmb(); |
3f542974 PS |
427 | } |
428 | if (new_msr & MOS_MSR_DELTA_RI) { | |
429 | icount->rng++; | |
0de9a702 | 430 | smp_wmb(); |
3f542974 PS |
431 | } |
432 | } | |
3f542974 PS |
433 | } |
434 | ||
0de9a702 | 435 | static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) |
3f542974 PS |
436 | { |
437 | struct async_icount *icount; | |
438 | ||
3f542974 | 439 | if (new_lsr & SERIAL_LSR_BI) { |
880af9db AC |
440 | /* |
441 | * Parity and Framing errors only count if they | |
442 | * occur exclusive of a break being | |
443 | * received. | |
444 | */ | |
3f542974 PS |
445 | new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); |
446 | } | |
447 | ||
448 | /* update input line counters */ | |
449 | icount = &port->icount; | |
450 | if (new_lsr & SERIAL_LSR_BI) { | |
451 | icount->brk++; | |
0de9a702 | 452 | smp_wmb(); |
3f542974 PS |
453 | } |
454 | if (new_lsr & SERIAL_LSR_OE) { | |
455 | icount->overrun++; | |
0de9a702 | 456 | smp_wmb(); |
3f542974 PS |
457 | } |
458 | if (new_lsr & SERIAL_LSR_PE) { | |
459 | icount->parity++; | |
0de9a702 | 460 | smp_wmb(); |
3f542974 PS |
461 | } |
462 | if (new_lsr & SERIAL_LSR_FE) { | |
463 | icount->frame++; | |
0de9a702 | 464 | smp_wmb(); |
3f542974 | 465 | } |
3f542974 PS |
466 | } |
467 | ||
468 | /************************************************************************/ | |
469 | /************************************************************************/ | |
470 | /* U S B C A L L B A C K F U N C T I O N S */ | |
471 | /* U S B C A L L B A C K F U N C T I O N S */ | |
472 | /************************************************************************/ | |
473 | /************************************************************************/ | |
474 | ||
7d12e780 | 475 | static void mos7840_control_callback(struct urb *urb) |
3f542974 PS |
476 | { |
477 | unsigned char *data; | |
478 | struct moschip_port *mos7840_port; | |
9c134a14 | 479 | struct device *dev = &urb->dev->dev; |
3f542974 | 480 | __u8 regval = 0x0; |
0de9a702 | 481 | int result = 0; |
0643c724 | 482 | int status = urb->status; |
3f542974 | 483 | |
cdc97792 | 484 | mos7840_port = urb->context; |
0de9a702 | 485 | |
0643c724 | 486 | switch (status) { |
3f542974 PS |
487 | case 0: |
488 | /* success */ | |
489 | break; | |
490 | case -ECONNRESET: | |
491 | case -ENOENT: | |
492 | case -ESHUTDOWN: | |
493 | /* this urb is terminated, clean up */ | |
9c134a14 | 494 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); |
3f542974 PS |
495 | return; |
496 | default: | |
9c134a14 | 497 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); |
3f542974 PS |
498 | goto exit; |
499 | } | |
500 | ||
9c134a14 GKH |
501 | dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length); |
502 | dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__, | |
503 | mos7840_port->MsrLsr, mos7840_port->port_num); | |
3f542974 PS |
504 | data = urb->transfer_buffer; |
505 | regval = (__u8) data[0]; | |
9c134a14 | 506 | dev_dbg(dev, "%s data is %x\n", __func__, regval); |
3f542974 PS |
507 | if (mos7840_port->MsrLsr == 0) |
508 | mos7840_handle_new_msr(mos7840_port, regval); | |
509 | else if (mos7840_port->MsrLsr == 1) | |
510 | mos7840_handle_new_lsr(mos7840_port, regval); | |
511 | ||
0de9a702 ON |
512 | exit: |
513 | spin_lock(&mos7840_port->pool_lock); | |
514 | if (!mos7840_port->zombie) | |
515 | result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC); | |
516 | spin_unlock(&mos7840_port->pool_lock); | |
517 | if (result) { | |
9c134a14 | 518 | dev_err(dev, "%s - Error %d submitting interrupt urb\n", |
441b62c1 | 519 | __func__, result); |
0de9a702 | 520 | } |
3f542974 PS |
521 | } |
522 | ||
523 | static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, | |
880af9db | 524 | __u16 *val) |
3f542974 PS |
525 | { |
526 | struct usb_device *dev = mcs->port->serial->dev; | |
0de9a702 ON |
527 | struct usb_ctrlrequest *dr = mcs->dr; |
528 | unsigned char *buffer = mcs->ctrl_buf; | |
529 | int ret; | |
3f542974 | 530 | |
3f542974 PS |
531 | dr->bRequestType = MCS_RD_RTYPE; |
532 | dr->bRequest = MCS_RDREQ; | |
880af9db | 533 | dr->wValue = cpu_to_le16(Wval); /* 0 */ |
3f542974 PS |
534 | dr->wIndex = cpu_to_le16(reg); |
535 | dr->wLength = cpu_to_le16(2); | |
536 | ||
537 | usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), | |
538 | (unsigned char *)dr, buffer, 2, | |
539 | mos7840_control_callback, mcs); | |
540 | mcs->control_urb->transfer_buffer_length = 2; | |
541 | ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); | |
542 | return ret; | |
543 | } | |
544 | ||
0eafe4de D |
545 | static void mos7840_set_led_callback(struct urb *urb) |
546 | { | |
547 | switch (urb->status) { | |
548 | case 0: | |
549 | /* Success */ | |
550 | break; | |
551 | case -ECONNRESET: | |
552 | case -ENOENT: | |
553 | case -ESHUTDOWN: | |
554 | /* This urb is terminated, clean up */ | |
9c134a14 GKH |
555 | dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d", |
556 | __func__, urb->status); | |
0eafe4de D |
557 | break; |
558 | default: | |
9c134a14 GKH |
559 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d", |
560 | __func__, urb->status); | |
0eafe4de D |
561 | } |
562 | } | |
563 | ||
564 | static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, | |
565 | __u16 reg) | |
566 | { | |
567 | struct usb_device *dev = mcs->port->serial->dev; | |
568 | struct usb_ctrlrequest *dr = mcs->dr; | |
569 | ||
570 | dr->bRequestType = MCS_WR_RTYPE; | |
571 | dr->bRequest = MCS_WRREQ; | |
572 | dr->wValue = cpu_to_le16(wval); | |
573 | dr->wIndex = cpu_to_le16(reg); | |
574 | dr->wLength = cpu_to_le16(0); | |
575 | ||
576 | usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0), | |
577 | (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); | |
578 | ||
579 | usb_submit_urb(mcs->control_urb, GFP_ATOMIC); | |
580 | } | |
581 | ||
582 | static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, | |
583 | __u16 val) | |
584 | { | |
585 | struct usb_device *dev = port->serial->dev; | |
586 | ||
587 | usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, | |
588 | val, reg, NULL, 0, MOS_WDR_TIMEOUT); | |
589 | } | |
590 | ||
591 | static void mos7840_led_off(unsigned long arg) | |
592 | { | |
593 | struct moschip_port *mcs = (struct moschip_port *) arg; | |
594 | ||
595 | /* Turn off LED */ | |
596 | mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); | |
597 | mod_timer(&mcs->led_timer2, | |
598 | jiffies + msecs_to_jiffies(LED_OFF_MS)); | |
599 | } | |
600 | ||
601 | static void mos7840_led_flag_off(unsigned long arg) | |
602 | { | |
603 | struct moschip_port *mcs = (struct moschip_port *) arg; | |
604 | ||
605 | mcs->led_flag = false; | |
606 | } | |
607 | ||
3f542974 PS |
608 | /***************************************************************************** |
609 | * mos7840_interrupt_callback | |
610 | * this is the callback function for when we have received data on the | |
611 | * interrupt endpoint. | |
612 | *****************************************************************************/ | |
613 | ||
7d12e780 | 614 | static void mos7840_interrupt_callback(struct urb *urb) |
3f542974 PS |
615 | { |
616 | int result; | |
617 | int length; | |
618 | struct moschip_port *mos7840_port; | |
619 | struct usb_serial *serial; | |
620 | __u16 Data; | |
621 | unsigned char *data; | |
622 | __u8 sp[5], st; | |
0de9a702 ON |
623 | int i, rv = 0; |
624 | __u16 wval, wreg = 0; | |
0643c724 | 625 | int status = urb->status; |
3f542974 | 626 | |
0643c724 | 627 | switch (status) { |
3f542974 PS |
628 | case 0: |
629 | /* success */ | |
630 | break; | |
631 | case -ECONNRESET: | |
632 | case -ENOENT: | |
633 | case -ESHUTDOWN: | |
634 | /* this urb is terminated, clean up */ | |
9c134a14 GKH |
635 | dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", |
636 | __func__, status); | |
3f542974 PS |
637 | return; |
638 | default: | |
9c134a14 GKH |
639 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", |
640 | __func__, status); | |
3f542974 PS |
641 | goto exit; |
642 | } | |
643 | ||
644 | length = urb->actual_length; | |
645 | data = urb->transfer_buffer; | |
646 | ||
cdc97792 | 647 | serial = urb->context; |
3f542974 PS |
648 | |
649 | /* Moschip get 5 bytes | |
650 | * Byte 1 IIR Port 1 (port.number is 0) | |
651 | * Byte 2 IIR Port 2 (port.number is 1) | |
652 | * Byte 3 IIR Port 3 (port.number is 2) | |
653 | * Byte 4 IIR Port 4 (port.number is 3) | |
654 | * Byte 5 FIFO status for both */ | |
655 | ||
656 | if (length && length > 5) { | |
9c134a14 | 657 | dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n"); |
3f542974 PS |
658 | return; |
659 | } | |
660 | ||
661 | sp[0] = (__u8) data[0]; | |
662 | sp[1] = (__u8) data[1]; | |
663 | sp[2] = (__u8) data[2]; | |
664 | sp[3] = (__u8) data[3]; | |
665 | st = (__u8) data[4]; | |
666 | ||
667 | for (i = 0; i < serial->num_ports; i++) { | |
668 | mos7840_port = mos7840_get_port_private(serial->port[i]); | |
669 | wval = | |
670 | (((__u16) serial->port[i]->number - | |
671 | (__u16) (serial->minor)) + 1) << 8; | |
672 | if (mos7840_port->open) { | |
673 | if (sp[i] & 0x01) { | |
9c134a14 | 674 | dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i); |
3f542974 PS |
675 | } else { |
676 | switch (sp[i] & 0x0f) { | |
677 | case SERIAL_IIR_RLS: | |
9c134a14 GKH |
678 | dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i); |
679 | dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n"); | |
3f542974 | 680 | mos7840_port->MsrLsr = 1; |
0de9a702 | 681 | wreg = LINE_STATUS_REGISTER; |
3f542974 PS |
682 | break; |
683 | case SERIAL_IIR_MS: | |
9c134a14 | 684 | dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i); |
3f542974 | 685 | mos7840_port->MsrLsr = 0; |
0de9a702 | 686 | wreg = MODEM_STATUS_REGISTER; |
3f542974 PS |
687 | break; |
688 | } | |
0de9a702 ON |
689 | spin_lock(&mos7840_port->pool_lock); |
690 | if (!mos7840_port->zombie) { | |
691 | rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); | |
692 | } else { | |
693 | spin_unlock(&mos7840_port->pool_lock); | |
694 | return; | |
695 | } | |
696 | spin_unlock(&mos7840_port->pool_lock); | |
3f542974 PS |
697 | } |
698 | } | |
699 | } | |
880af9db AC |
700 | if (!(rv < 0)) |
701 | /* the completion handler for the control urb will resubmit */ | |
0de9a702 ON |
702 | return; |
703 | exit: | |
3f542974 PS |
704 | result = usb_submit_urb(urb, GFP_ATOMIC); |
705 | if (result) { | |
706 | dev_err(&urb->dev->dev, | |
707 | "%s - Error %d submitting interrupt urb\n", | |
441b62c1 | 708 | __func__, result); |
3f542974 | 709 | } |
3f542974 PS |
710 | } |
711 | ||
712 | static int mos7840_port_paranoia_check(struct usb_serial_port *port, | |
713 | const char *function) | |
714 | { | |
715 | if (!port) { | |
9c134a14 | 716 | pr_debug("%s - port == NULL\n", function); |
3f542974 PS |
717 | return -1; |
718 | } | |
719 | if (!port->serial) { | |
9c134a14 | 720 | pr_debug("%s - port->serial == NULL\n", function); |
3f542974 PS |
721 | return -1; |
722 | } | |
723 | ||
724 | return 0; | |
725 | } | |
726 | ||
727 | /* Inline functions to check the sanity of a pointer that is passed to us */ | |
728 | static int mos7840_serial_paranoia_check(struct usb_serial *serial, | |
729 | const char *function) | |
730 | { | |
731 | if (!serial) { | |
9c134a14 | 732 | pr_debug("%s - serial == NULL\n", function); |
3f542974 PS |
733 | return -1; |
734 | } | |
735 | if (!serial->type) { | |
9c134a14 | 736 | pr_debug("%s - serial->type == NULL!\n", function); |
3f542974 PS |
737 | return -1; |
738 | } | |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
743 | static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, | |
744 | const char *function) | |
745 | { | |
746 | /* if no port was specified, or it fails a paranoia check */ | |
747 | if (!port || | |
748 | mos7840_port_paranoia_check(port, function) || | |
749 | mos7840_serial_paranoia_check(port->serial, function)) { | |
880af9db AC |
750 | /* then say that we don't have a valid usb_serial thing, |
751 | * which will end up genrating -ENODEV return values */ | |
3f542974 PS |
752 | return NULL; |
753 | } | |
754 | ||
755 | return port->serial; | |
756 | } | |
757 | ||
758 | /***************************************************************************** | |
759 | * mos7840_bulk_in_callback | |
760 | * this is the callback function for when we have received data on the | |
761 | * bulk in endpoint. | |
762 | *****************************************************************************/ | |
763 | ||
7d12e780 | 764 | static void mos7840_bulk_in_callback(struct urb *urb) |
3f542974 | 765 | { |
0643c724 | 766 | int retval; |
3f542974 PS |
767 | unsigned char *data; |
768 | struct usb_serial *serial; | |
769 | struct usb_serial_port *port; | |
770 | struct moschip_port *mos7840_port; | |
771 | struct tty_struct *tty; | |
0643c724 | 772 | int status = urb->status; |
3f542974 | 773 | |
cdc97792 | 774 | mos7840_port = urb->context; |
9c134a14 | 775 | if (!mos7840_port) |
50de36f7 | 776 | return; |
50de36f7 GKH |
777 | |
778 | if (status) { | |
9c134a14 | 779 | dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); |
50de36f7 | 780 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
781 | return; |
782 | } | |
783 | ||
9c134a14 | 784 | port = mos7840_port->port; |
441b62c1 | 785 | if (mos7840_port_paranoia_check(port, __func__)) { |
50de36f7 | 786 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
787 | return; |
788 | } | |
789 | ||
441b62c1 | 790 | serial = mos7840_get_usb_serial(port, __func__); |
3f542974 | 791 | if (!serial) { |
50de36f7 | 792 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
793 | return; |
794 | } | |
795 | ||
3f542974 | 796 | data = urb->transfer_buffer; |
59d33f2f | 797 | usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); |
3f542974 | 798 | |
3f542974 | 799 | if (urb->actual_length) { |
4a90f09b | 800 | tty = tty_port_tty_get(&mos7840_port->port->port); |
3f542974 | 801 | if (tty) { |
3f542974 | 802 | tty_insert_flip_string(tty, data, urb->actual_length); |
3f542974 | 803 | tty_flip_buffer_push(tty); |
4a90f09b | 804 | tty_kref_put(tty); |
3f542974 PS |
805 | } |
806 | mos7840_port->icount.rx += urb->actual_length; | |
0de9a702 | 807 | smp_wmb(); |
9c134a14 | 808 | dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx); |
3f542974 PS |
809 | } |
810 | ||
811 | if (!mos7840_port->read_urb) { | |
9c134a14 | 812 | dev_dbg(&port->dev, "%s", "URB KILLED !!!\n"); |
50de36f7 | 813 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
814 | return; |
815 | } | |
816 | ||
0eafe4de D |
817 | /* Turn on LED */ |
818 | if (mos7840_port->has_led && !mos7840_port->led_flag) { | |
819 | mos7840_port->led_flag = true; | |
820 | mos7840_set_led_async(mos7840_port, 0x0301, | |
821 | MODEM_CONTROL_REGISTER); | |
822 | mod_timer(&mos7840_port->led_timer1, | |
823 | jiffies + msecs_to_jiffies(LED_ON_MS)); | |
824 | } | |
3f542974 | 825 | |
50de36f7 | 826 | mos7840_port->read_urb_busy = true; |
0643c724 | 827 | retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
0de9a702 | 828 | |
0643c724 | 829 | if (retval) { |
9c134a14 | 830 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); |
50de36f7 | 831 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
832 | } |
833 | } | |
834 | ||
835 | /***************************************************************************** | |
836 | * mos7840_bulk_out_data_callback | |
880af9db AC |
837 | * this is the callback function for when we have finished sending |
838 | * serial data on the bulk out endpoint. | |
3f542974 PS |
839 | *****************************************************************************/ |
840 | ||
7d12e780 | 841 | static void mos7840_bulk_out_data_callback(struct urb *urb) |
3f542974 PS |
842 | { |
843 | struct moschip_port *mos7840_port; | |
9c134a14 | 844 | struct usb_serial_port *port; |
3f542974 | 845 | struct tty_struct *tty; |
0643c724 | 846 | int status = urb->status; |
0de9a702 ON |
847 | int i; |
848 | ||
cdc97792 | 849 | mos7840_port = urb->context; |
9c134a14 | 850 | port = mos7840_port->port; |
0de9a702 ON |
851 | spin_lock(&mos7840_port->pool_lock); |
852 | for (i = 0; i < NUM_URBS; i++) { | |
853 | if (urb == mos7840_port->write_urb_pool[i]) { | |
854 | mos7840_port->busy[i] = 0; | |
855 | break; | |
856 | } | |
857 | } | |
858 | spin_unlock(&mos7840_port->pool_lock); | |
859 | ||
0643c724 | 860 | if (status) { |
9c134a14 | 861 | dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); |
3f542974 PS |
862 | return; |
863 | } | |
864 | ||
9c134a14 | 865 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 866 | return; |
3f542974 | 867 | |
9c134a14 | 868 | tty = tty_port_tty_get(&port->port); |
b963a844 JS |
869 | if (tty && mos7840_port->open) |
870 | tty_wakeup(tty); | |
4a90f09b | 871 | tty_kref_put(tty); |
3f542974 PS |
872 | |
873 | } | |
874 | ||
875 | /************************************************************************/ | |
876 | /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */ | |
877 | /************************************************************************/ | |
878 | #ifdef MCSSerialProbe | |
879 | static int mos7840_serial_probe(struct usb_serial *serial, | |
880 | const struct usb_device_id *id) | |
881 | { | |
882 | ||
883 | /*need to implement the mode_reg reading and updating\ | |
884 | structures usb_serial_ device_type\ | |
885 | (i.e num_ports, num_bulkin,bulkout etc) */ | |
886 | /* Also we can update the changes attach */ | |
887 | return 1; | |
888 | } | |
889 | #endif | |
890 | ||
891 | /***************************************************************************** | |
892 | * mos7840_open | |
893 | * this function is called by the tty driver when a port is opened | |
894 | * If successful, we return 0 | |
895 | * Otherwise we return a negative error number. | |
896 | *****************************************************************************/ | |
897 | ||
a509a7e4 | 898 | static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) |
3f542974 PS |
899 | { |
900 | int response; | |
901 | int j; | |
902 | struct usb_serial *serial; | |
903 | struct urb *urb; | |
904 | __u16 Data; | |
905 | int status; | |
906 | struct moschip_port *mos7840_port; | |
0de9a702 | 907 | struct moschip_port *port0; |
3f542974 | 908 | |
9c134a14 | 909 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 910 | return -ENODEV; |
3f542974 | 911 | |
3f542974 PS |
912 | serial = port->serial; |
913 | ||
9c134a14 | 914 | if (mos7840_serial_paranoia_check(serial, __func__)) |
3f542974 | 915 | return -ENODEV; |
3f542974 PS |
916 | |
917 | mos7840_port = mos7840_get_port_private(port); | |
0de9a702 | 918 | port0 = mos7840_get_port_private(serial->port[0]); |
3f542974 | 919 | |
0de9a702 | 920 | if (mos7840_port == NULL || port0 == NULL) |
3f542974 PS |
921 | return -ENODEV; |
922 | ||
923 | usb_clear_halt(serial->dev, port->write_urb->pipe); | |
924 | usb_clear_halt(serial->dev, port->read_urb->pipe); | |
0de9a702 | 925 | port0->open_ports++; |
3f542974 PS |
926 | |
927 | /* Initialising the write urb pool */ | |
928 | for (j = 0; j < NUM_URBS; ++j) { | |
0de9a702 | 929 | urb = usb_alloc_urb(0, GFP_KERNEL); |
3f542974 PS |
930 | mos7840_port->write_urb_pool[j] = urb; |
931 | ||
932 | if (urb == NULL) { | |
194343d9 | 933 | dev_err(&port->dev, "No more urbs???\n"); |
3f542974 PS |
934 | continue; |
935 | } | |
936 | ||
880af9db AC |
937 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
938 | GFP_KERNEL); | |
3f542974 | 939 | if (!urb->transfer_buffer) { |
0de9a702 ON |
940 | usb_free_urb(urb); |
941 | mos7840_port->write_urb_pool[j] = NULL; | |
194343d9 GKH |
942 | dev_err(&port->dev, |
943 | "%s-out of memory for urb buffers.\n", | |
944 | __func__); | |
3f542974 PS |
945 | continue; |
946 | } | |
947 | } | |
948 | ||
949 | /***************************************************************************** | |
950 | * Initialize MCS7840 -- Write Init values to corresponding Registers | |
951 | * | |
952 | * Register Index | |
953 | * 1 : IER | |
954 | * 2 : FCR | |
955 | * 3 : LCR | |
956 | * 4 : MCR | |
957 | * | |
958 | * 0x08 : SP1/2 Control Reg | |
959 | *****************************************************************************/ | |
960 | ||
880af9db | 961 | /* NEED to check the following Block */ |
3f542974 | 962 | |
3f542974 PS |
963 | Data = 0x0; |
964 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); | |
965 | if (status < 0) { | |
9c134a14 | 966 | dev_dbg(&port->dev, "Reading Spreg failed\n"); |
3f542974 PS |
967 | return -1; |
968 | } | |
969 | Data |= 0x80; | |
970 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); | |
971 | if (status < 0) { | |
9c134a14 | 972 | dev_dbg(&port->dev, "writing Spreg failed\n"); |
3f542974 PS |
973 | return -1; |
974 | } | |
975 | ||
976 | Data &= ~0x80; | |
977 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); | |
978 | if (status < 0) { | |
9c134a14 | 979 | dev_dbg(&port->dev, "writing Spreg failed\n"); |
3f542974 PS |
980 | return -1; |
981 | } | |
880af9db | 982 | /* End of block to be checked */ |
3f542974 | 983 | |
3f542974 | 984 | Data = 0x0; |
880af9db AC |
985 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
986 | &Data); | |
3f542974 | 987 | if (status < 0) { |
9c134a14 | 988 | dev_dbg(&port->dev, "Reading Controlreg failed\n"); |
3f542974 PS |
989 | return -1; |
990 | } | |
880af9db AC |
991 | Data |= 0x08; /* Driver done bit */ |
992 | Data |= 0x20; /* rx_disable */ | |
993 | status = mos7840_set_reg_sync(port, | |
994 | mos7840_port->ControlRegOffset, Data); | |
3f542974 | 995 | if (status < 0) { |
9c134a14 | 996 | dev_dbg(&port->dev, "writing Controlreg failed\n"); |
3f542974 PS |
997 | return -1; |
998 | } | |
880af9db AC |
999 | /* do register settings here */ |
1000 | /* Set all regs to the device default values. */ | |
1001 | /*********************************** | |
1002 | * First Disable all interrupts. | |
1003 | ***********************************/ | |
3f542974 | 1004 | Data = 0x00; |
3f542974 PS |
1005 | status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
1006 | if (status < 0) { | |
9c134a14 | 1007 | dev_dbg(&port->dev, "disabling interrupts failed\n"); |
3f542974 PS |
1008 | return -1; |
1009 | } | |
880af9db | 1010 | /* Set FIFO_CONTROL_REGISTER to the default value */ |
3f542974 | 1011 | Data = 0x00; |
3f542974 PS |
1012 | status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
1013 | if (status < 0) { | |
9c134a14 | 1014 | dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); |
3f542974 PS |
1015 | return -1; |
1016 | } | |
1017 | ||
1018 | Data = 0xcf; | |
3f542974 PS |
1019 | status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); |
1020 | if (status < 0) { | |
9c134a14 | 1021 | dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); |
3f542974 PS |
1022 | return -1; |
1023 | } | |
1024 | ||
1025 | Data = 0x03; | |
3f542974 PS |
1026 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
1027 | mos7840_port->shadowLCR = Data; | |
1028 | ||
1029 | Data = 0x0b; | |
3f542974 PS |
1030 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); |
1031 | mos7840_port->shadowMCR = Data; | |
1032 | ||
1033 | Data = 0x00; | |
3f542974 PS |
1034 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
1035 | mos7840_port->shadowLCR = Data; | |
1036 | ||
880af9db | 1037 | Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ |
3f542974 PS |
1038 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
1039 | ||
1040 | Data = 0x0c; | |
3f542974 PS |
1041 | status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); |
1042 | ||
1043 | Data = 0x0; | |
3f542974 PS |
1044 | status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); |
1045 | ||
1046 | Data = 0x00; | |
3f542974 PS |
1047 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
1048 | ||
1049 | Data = Data & ~SERIAL_LCR_DLAB; | |
3f542974 PS |
1050 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
1051 | mos7840_port->shadowLCR = Data; | |
1052 | ||
880af9db | 1053 | /* clearing Bulkin and Bulkout Fifo */ |
3f542974 | 1054 | Data = 0x0; |
3f542974 PS |
1055 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); |
1056 | ||
1057 | Data = Data | 0x0c; | |
3f542974 PS |
1058 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
1059 | ||
1060 | Data = Data & ~0x0c; | |
3f542974 | 1061 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); |
880af9db | 1062 | /* Finally enable all interrupts */ |
3f542974 | 1063 | Data = 0x0c; |
3f542974 PS |
1064 | status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); |
1065 | ||
880af9db | 1066 | /* clearing rx_disable */ |
3f542974 | 1067 | Data = 0x0; |
880af9db AC |
1068 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
1069 | &Data); | |
3f542974 | 1070 | Data = Data & ~0x20; |
880af9db AC |
1071 | status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, |
1072 | Data); | |
3f542974 | 1073 | |
880af9db | 1074 | /* rx_negate */ |
3f542974 | 1075 | Data = 0x0; |
880af9db AC |
1076 | status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, |
1077 | &Data); | |
3f542974 | 1078 | Data = Data | 0x10; |
880af9db AC |
1079 | status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, |
1080 | Data); | |
3f542974 | 1081 | |
880af9db AC |
1082 | /* Check to see if we've set up our endpoint info yet * |
1083 | * (can't set it up in mos7840_startup as the structures * | |
1084 | * were not set up at that time.) */ | |
0de9a702 | 1085 | if (port0->open_ports == 1) { |
3f542974 | 1086 | if (serial->port[0]->interrupt_in_buffer == NULL) { |
3f542974 | 1087 | /* set up interrupt urb */ |
3f542974 | 1088 | usb_fill_int_urb(serial->port[0]->interrupt_in_urb, |
880af9db AC |
1089 | serial->dev, |
1090 | usb_rcvintpipe(serial->dev, | |
1091 | serial->port[0]->interrupt_in_endpointAddress), | |
1092 | serial->port[0]->interrupt_in_buffer, | |
1093 | serial->port[0]->interrupt_in_urb-> | |
1094 | transfer_buffer_length, | |
1095 | mos7840_interrupt_callback, | |
1096 | serial, | |
1097 | serial->port[0]->interrupt_in_urb->interval); | |
3f542974 PS |
1098 | |
1099 | /* start interrupt read for mos7840 * | |
1100 | * will continue as long as mos7840 is connected */ | |
1101 | ||
1102 | response = | |
1103 | usb_submit_urb(serial->port[0]->interrupt_in_urb, | |
1104 | GFP_KERNEL); | |
1105 | if (response) { | |
194343d9 GKH |
1106 | dev_err(&port->dev, "%s - Error %d submitting " |
1107 | "interrupt urb\n", __func__, response); | |
3f542974 PS |
1108 | } |
1109 | ||
1110 | } | |
1111 | ||
1112 | } | |
1113 | ||
1114 | /* see if we've set up our endpoint info yet * | |
1115 | * (can't set it up in mos7840_startup as the * | |
1116 | * structures were not set up at that time.) */ | |
1117 | ||
9c134a14 GKH |
1118 | dev_dbg(&port->dev, "port number is %d\n", port->number); |
1119 | dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor); | |
1120 | dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); | |
1121 | dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); | |
1122 | dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress); | |
1123 | dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num); | |
3f542974 PS |
1124 | mos7840_port->read_urb = port->read_urb; |
1125 | ||
1126 | /* set up our bulk in urb */ | |
093ea2d3 DL |
1127 | if ((serial->num_ports == 2) |
1128 | && ((((__u16)port->number - | |
1129 | (__u16)(port->serial->minor)) % 2) != 0)) { | |
1130 | usb_fill_bulk_urb(mos7840_port->read_urb, | |
1131 | serial->dev, | |
1132 | usb_rcvbulkpipe(serial->dev, | |
1133 | (port->bulk_in_endpointAddress) + 2), | |
1134 | port->bulk_in_buffer, | |
1135 | mos7840_port->read_urb->transfer_buffer_length, | |
1136 | mos7840_bulk_in_callback, mos7840_port); | |
1137 | } else { | |
1138 | usb_fill_bulk_urb(mos7840_port->read_urb, | |
1139 | serial->dev, | |
1140 | usb_rcvbulkpipe(serial->dev, | |
1141 | port->bulk_in_endpointAddress), | |
1142 | port->bulk_in_buffer, | |
1143 | mos7840_port->read_urb->transfer_buffer_length, | |
1144 | mos7840_bulk_in_callback, mos7840_port); | |
1145 | } | |
3f542974 | 1146 | |
9c134a14 | 1147 | dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress); |
50de36f7 | 1148 | mos7840_port->read_urb_busy = true; |
3f542974 PS |
1149 | response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); |
1150 | if (response) { | |
194343d9 GKH |
1151 | dev_err(&port->dev, "%s - Error %d submitting control urb\n", |
1152 | __func__, response); | |
50de36f7 | 1153 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
1154 | } |
1155 | ||
1156 | /* initialize our wait queues */ | |
1157 | init_waitqueue_head(&mos7840_port->wait_chase); | |
1158 | init_waitqueue_head(&mos7840_port->delta_msr_wait); | |
1159 | ||
1160 | /* initialize our icount structure */ | |
1161 | memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount)); | |
1162 | ||
1163 | /* initialize our port settings */ | |
880af9db AC |
1164 | /* Must set to enable ints! */ |
1165 | mos7840_port->shadowMCR = MCR_MASTER_IE; | |
3f542974 PS |
1166 | /* send a open port command */ |
1167 | mos7840_port->open = 1; | |
880af9db | 1168 | /* mos7840_change_port_settings(mos7840_port,old_termios); */ |
3f542974 PS |
1169 | mos7840_port->icount.tx = 0; |
1170 | mos7840_port->icount.rx = 0; | |
1171 | ||
3f542974 | 1172 | return 0; |
3f542974 PS |
1173 | } |
1174 | ||
1175 | /***************************************************************************** | |
1176 | * mos7840_chars_in_buffer | |
1177 | * this function is called by the tty driver when it wants to know how many | |
1178 | * bytes of data we currently have outstanding in the port (data that has | |
1179 | * been written, but hasn't made it out the port yet) | |
1180 | * If successful, we return the number of bytes left to be written in the | |
1181 | * system, | |
95da310e | 1182 | * Otherwise we return zero. |
3f542974 PS |
1183 | *****************************************************************************/ |
1184 | ||
95da310e | 1185 | static int mos7840_chars_in_buffer(struct tty_struct *tty) |
3f542974 | 1186 | { |
95da310e | 1187 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1188 | int i; |
1189 | int chars = 0; | |
0de9a702 | 1190 | unsigned long flags; |
3f542974 PS |
1191 | struct moschip_port *mos7840_port; |
1192 | ||
9c134a14 | 1193 | if (mos7840_port_paranoia_check(port, __func__)) |
95da310e | 1194 | return 0; |
3f542974 PS |
1195 | |
1196 | mos7840_port = mos7840_get_port_private(port); | |
3363155b | 1197 | if (mos7840_port == NULL) |
95da310e | 1198 | return 0; |
3f542974 | 1199 | |
880af9db | 1200 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
5c263b92 MF |
1201 | for (i = 0; i < NUM_URBS; ++i) { |
1202 | if (mos7840_port->busy[i]) { | |
1203 | struct urb *urb = mos7840_port->write_urb_pool[i]; | |
1204 | chars += urb->transfer_buffer_length; | |
1205 | } | |
1206 | } | |
880af9db | 1207 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
9c134a14 | 1208 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); |
0de9a702 | 1209 | return chars; |
3f542974 PS |
1210 | |
1211 | } | |
1212 | ||
3f542974 PS |
1213 | /***************************************************************************** |
1214 | * mos7840_close | |
1215 | * this function is called by the tty driver when a port is closed | |
1216 | *****************************************************************************/ | |
1217 | ||
335f8514 | 1218 | static void mos7840_close(struct usb_serial_port *port) |
3f542974 PS |
1219 | { |
1220 | struct usb_serial *serial; | |
1221 | struct moschip_port *mos7840_port; | |
0de9a702 | 1222 | struct moschip_port *port0; |
3f542974 PS |
1223 | int j; |
1224 | __u16 Data; | |
1225 | ||
9c134a14 | 1226 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1227 | return; |
3f542974 | 1228 | |
441b62c1 | 1229 | serial = mos7840_get_usb_serial(port, __func__); |
9c134a14 | 1230 | if (!serial) |
3f542974 | 1231 | return; |
3f542974 PS |
1232 | |
1233 | mos7840_port = mos7840_get_port_private(port); | |
0de9a702 | 1234 | port0 = mos7840_get_port_private(serial->port[0]); |
3f542974 | 1235 | |
0de9a702 | 1236 | if (mos7840_port == NULL || port0 == NULL) |
3f542974 | 1237 | return; |
3f542974 PS |
1238 | |
1239 | for (j = 0; j < NUM_URBS; ++j) | |
1240 | usb_kill_urb(mos7840_port->write_urb_pool[j]); | |
1241 | ||
1242 | /* Freeing Write URBs */ | |
1243 | for (j = 0; j < NUM_URBS; ++j) { | |
1244 | if (mos7840_port->write_urb_pool[j]) { | |
1245 | if (mos7840_port->write_urb_pool[j]->transfer_buffer) | |
1246 | kfree(mos7840_port->write_urb_pool[j]-> | |
1247 | transfer_buffer); | |
1248 | ||
1249 | usb_free_urb(mos7840_port->write_urb_pool[j]); | |
1250 | } | |
1251 | } | |
1252 | ||
3f542974 PS |
1253 | /* While closing port, shutdown all bulk read, write * |
1254 | * and interrupt read if they exists */ | |
1255 | if (serial->dev) { | |
3f542974 | 1256 | if (mos7840_port->write_urb) { |
9c134a14 | 1257 | dev_dbg(&port->dev, "%s", "Shutdown bulk write\n"); |
3f542974 PS |
1258 | usb_kill_urb(mos7840_port->write_urb); |
1259 | } | |
3f542974 | 1260 | if (mos7840_port->read_urb) { |
9c134a14 | 1261 | dev_dbg(&port->dev, "%s", "Shutdown bulk read\n"); |
3f542974 | 1262 | usb_kill_urb(mos7840_port->read_urb); |
50de36f7 | 1263 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
1264 | } |
1265 | if ((&mos7840_port->control_urb)) { | |
9c134a14 | 1266 | dev_dbg(&port->dev, "%s", "Shutdown control read\n"); |
880af9db | 1267 | /*/ usb_kill_urb (mos7840_port->control_urb); */ |
3f542974 PS |
1268 | } |
1269 | } | |
880af9db AC |
1270 | /* if(mos7840_port->ctrl_buf != NULL) */ |
1271 | /* kfree(mos7840_port->ctrl_buf); */ | |
0de9a702 | 1272 | port0->open_ports--; |
9c134a14 | 1273 | dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number); |
0de9a702 | 1274 | if (port0->open_ports == 0) { |
3f542974 | 1275 | if (serial->port[0]->interrupt_in_urb) { |
9c134a14 | 1276 | dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); |
0de9a702 | 1277 | usb_kill_urb(serial->port[0]->interrupt_in_urb); |
3f542974 PS |
1278 | } |
1279 | } | |
1280 | ||
1281 | if (mos7840_port->write_urb) { | |
1282 | /* if this urb had a transfer buffer already (old tx) free it */ | |
880af9db | 1283 | if (mos7840_port->write_urb->transfer_buffer != NULL) |
3f542974 | 1284 | kfree(mos7840_port->write_urb->transfer_buffer); |
3f542974 PS |
1285 | usb_free_urb(mos7840_port->write_urb); |
1286 | } | |
1287 | ||
1288 | Data = 0x0; | |
1289 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); | |
1290 | ||
1291 | Data = 0x00; | |
1292 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); | |
1293 | ||
1294 | mos7840_port->open = 0; | |
3f542974 PS |
1295 | } |
1296 | ||
1297 | /************************************************************************ | |
1298 | * | |
1299 | * mos7840_block_until_chase_response | |
1300 | * | |
1301 | * This function will block the close until one of the following: | |
1302 | * 1. Response to our Chase comes from mos7840 | |
dc0d5c1e | 1303 | * 2. A timeout of 10 seconds without activity has expired |
3f542974 PS |
1304 | * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty) |
1305 | * | |
1306 | ************************************************************************/ | |
1307 | ||
95da310e AC |
1308 | static void mos7840_block_until_chase_response(struct tty_struct *tty, |
1309 | struct moschip_port *mos7840_port) | |
3f542974 | 1310 | { |
1e658489 | 1311 | int timeout = msecs_to_jiffies(1000); |
3f542974 PS |
1312 | int wait = 10; |
1313 | int count; | |
1314 | ||
1315 | while (1) { | |
95da310e | 1316 | count = mos7840_chars_in_buffer(tty); |
3f542974 PS |
1317 | |
1318 | /* Check for Buffer status */ | |
880af9db | 1319 | if (count <= 0) |
3f542974 | 1320 | return; |
3f542974 PS |
1321 | |
1322 | /* Block the thread for a while */ | |
1323 | interruptible_sleep_on_timeout(&mos7840_port->wait_chase, | |
1324 | timeout); | |
1325 | /* No activity.. count down section */ | |
1326 | wait--; | |
1327 | if (wait == 0) { | |
9c134a14 | 1328 | dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__); |
3f542974 PS |
1329 | return; |
1330 | } else { | |
dc0d5c1e | 1331 | /* Reset timeout value back to seconds */ |
3f542974 PS |
1332 | wait = 10; |
1333 | } | |
1334 | } | |
1335 | ||
1336 | } | |
1337 | ||
1338 | /***************************************************************************** | |
1339 | * mos7840_break | |
1340 | * this function sends a break to the port | |
1341 | *****************************************************************************/ | |
95da310e | 1342 | static void mos7840_break(struct tty_struct *tty, int break_state) |
3f542974 | 1343 | { |
95da310e | 1344 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1345 | unsigned char data; |
1346 | struct usb_serial *serial; | |
1347 | struct moschip_port *mos7840_port; | |
1348 | ||
9c134a14 | 1349 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1350 | return; |
3f542974 | 1351 | |
441b62c1 | 1352 | serial = mos7840_get_usb_serial(port, __func__); |
9c134a14 | 1353 | if (!serial) |
3f542974 | 1354 | return; |
3f542974 PS |
1355 | |
1356 | mos7840_port = mos7840_get_port_private(port); | |
1357 | ||
880af9db | 1358 | if (mos7840_port == NULL) |
3f542974 | 1359 | return; |
3f542974 | 1360 | |
95da310e | 1361 | if (serial->dev) |
3f542974 | 1362 | /* flush and block until tx is empty */ |
95da310e | 1363 | mos7840_block_until_chase_response(tty, mos7840_port); |
3f542974 | 1364 | |
95da310e | 1365 | if (break_state == -1) |
3f542974 | 1366 | data = mos7840_port->shadowLCR | LCR_SET_BREAK; |
95da310e | 1367 | else |
3f542974 | 1368 | data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; |
3f542974 | 1369 | |
6b447f04 | 1370 | /* FIXME: no locking on shadowLCR anywhere in driver */ |
3f542974 | 1371 | mos7840_port->shadowLCR = data; |
9c134a14 | 1372 | dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); |
3f542974 PS |
1373 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, |
1374 | mos7840_port->shadowLCR); | |
3f542974 PS |
1375 | } |
1376 | ||
1377 | /***************************************************************************** | |
1378 | * mos7840_write_room | |
1379 | * this function is called by the tty driver when it wants to know how many | |
1380 | * bytes of data we can accept for a specific port. | |
1381 | * If successful, we return the amount of room that we have for this port | |
1382 | * Otherwise we return a negative error number. | |
1383 | *****************************************************************************/ | |
1384 | ||
95da310e | 1385 | static int mos7840_write_room(struct tty_struct *tty) |
3f542974 | 1386 | { |
95da310e | 1387 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1388 | int i; |
1389 | int room = 0; | |
0de9a702 | 1390 | unsigned long flags; |
3f542974 PS |
1391 | struct moschip_port *mos7840_port; |
1392 | ||
9c134a14 | 1393 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1394 | return -1; |
3f542974 PS |
1395 | |
1396 | mos7840_port = mos7840_get_port_private(port); | |
9c134a14 | 1397 | if (mos7840_port == NULL) |
3f542974 | 1398 | return -1; |
3f542974 | 1399 | |
0de9a702 | 1400 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
3f542974 | 1401 | for (i = 0; i < NUM_URBS; ++i) { |
880af9db | 1402 | if (!mos7840_port->busy[i]) |
3f542974 | 1403 | room += URB_TRANSFER_BUFFER_SIZE; |
3f542974 | 1404 | } |
0de9a702 | 1405 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
3f542974 | 1406 | |
0de9a702 | 1407 | room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; |
9c134a14 | 1408 | dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room); |
0de9a702 | 1409 | return room; |
3f542974 PS |
1410 | |
1411 | } | |
1412 | ||
1413 | /***************************************************************************** | |
1414 | * mos7840_write | |
1415 | * this function is called by the tty driver when data should be written to | |
1416 | * the port. | |
1417 | * If successful, we return the number of bytes written, otherwise we | |
1418 | * return a negative error number. | |
1419 | *****************************************************************************/ | |
1420 | ||
95da310e | 1421 | static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, |
3f542974 PS |
1422 | const unsigned char *data, int count) |
1423 | { | |
1424 | int status; | |
1425 | int i; | |
1426 | int bytes_sent = 0; | |
1427 | int transfer_size; | |
0de9a702 | 1428 | unsigned long flags; |
3f542974 PS |
1429 | |
1430 | struct moschip_port *mos7840_port; | |
1431 | struct usb_serial *serial; | |
1432 | struct urb *urb; | |
880af9db | 1433 | /* __u16 Data; */ |
3f542974 PS |
1434 | const unsigned char *current_position = data; |
1435 | unsigned char *data1; | |
3f542974 PS |
1436 | |
1437 | #ifdef NOTMOS7840 | |
1438 | Data = 0x00; | |
3f542974 PS |
1439 | status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); |
1440 | mos7840_port->shadowLCR = Data; | |
9c134a14 GKH |
1441 | dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data); |
1442 | dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); | |
3f542974 | 1443 | |
880af9db AC |
1444 | /* Data = 0x03; */ |
1445 | /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */ | |
1446 | /* mos7840_port->shadowLCR=Data;//Need to add later */ | |
3f542974 | 1447 | |
880af9db | 1448 | Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ |
3f542974 PS |
1449 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
1450 | ||
880af9db AC |
1451 | /* Data = 0x0c; */ |
1452 | /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ | |
3f542974 | 1453 | Data = 0x00; |
3f542974 | 1454 | status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); |
9c134a14 | 1455 | dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data); |
3f542974 PS |
1456 | |
1457 | Data = 0x0; | |
3f542974 | 1458 | status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); |
9c134a14 | 1459 | dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data); |
3f542974 PS |
1460 | |
1461 | Data = Data & ~SERIAL_LCR_DLAB; | |
9c134a14 | 1462 | dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); |
3f542974 PS |
1463 | status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); |
1464 | #endif | |
1465 | ||
9c134a14 | 1466 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1467 | return -1; |
3f542974 PS |
1468 | |
1469 | serial = port->serial; | |
9c134a14 | 1470 | if (mos7840_serial_paranoia_check(serial, __func__)) |
3f542974 | 1471 | return -1; |
3f542974 PS |
1472 | |
1473 | mos7840_port = mos7840_get_port_private(port); | |
9c134a14 | 1474 | if (mos7840_port == NULL) |
3f542974 | 1475 | return -1; |
3f542974 PS |
1476 | |
1477 | /* try to find a free urb in the list */ | |
1478 | urb = NULL; | |
1479 | ||
0de9a702 | 1480 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); |
3f542974 | 1481 | for (i = 0; i < NUM_URBS; ++i) { |
0de9a702 ON |
1482 | if (!mos7840_port->busy[i]) { |
1483 | mos7840_port->busy[i] = 1; | |
3f542974 | 1484 | urb = mos7840_port->write_urb_pool[i]; |
9c134a14 | 1485 | dev_dbg(&port->dev, "URB:%d\n", i); |
3f542974 PS |
1486 | break; |
1487 | } | |
1488 | } | |
0de9a702 | 1489 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); |
3f542974 PS |
1490 | |
1491 | if (urb == NULL) { | |
9c134a14 | 1492 | dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); |
3f542974 PS |
1493 | goto exit; |
1494 | } | |
1495 | ||
1496 | if (urb->transfer_buffer == NULL) { | |
1497 | urb->transfer_buffer = | |
1498 | kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL); | |
1499 | ||
1500 | if (urb->transfer_buffer == NULL) { | |
22a416c4 | 1501 | dev_err_console(port, "%s no more kernel memory...\n", |
194343d9 | 1502 | __func__); |
3f542974 PS |
1503 | goto exit; |
1504 | } | |
1505 | } | |
1506 | transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); | |
1507 | ||
97c4965d | 1508 | memcpy(urb->transfer_buffer, current_position, transfer_size); |
3f542974 PS |
1509 | |
1510 | /* fill urb with data and submit */ | |
093ea2d3 DL |
1511 | if ((serial->num_ports == 2) |
1512 | && ((((__u16)port->number - | |
1513 | (__u16)(port->serial->minor)) % 2) != 0)) { | |
1514 | usb_fill_bulk_urb(urb, | |
1515 | serial->dev, | |
1516 | usb_sndbulkpipe(serial->dev, | |
1517 | (port->bulk_out_endpointAddress) + 2), | |
1518 | urb->transfer_buffer, | |
1519 | transfer_size, | |
1520 | mos7840_bulk_out_data_callback, mos7840_port); | |
1521 | } else { | |
1522 | usb_fill_bulk_urb(urb, | |
1523 | serial->dev, | |
1524 | usb_sndbulkpipe(serial->dev, | |
1525 | port->bulk_out_endpointAddress), | |
1526 | urb->transfer_buffer, | |
1527 | transfer_size, | |
1528 | mos7840_bulk_out_data_callback, mos7840_port); | |
1529 | } | |
3f542974 PS |
1530 | |
1531 | data1 = urb->transfer_buffer; | |
9c134a14 | 1532 | dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress); |
3f542974 | 1533 | |
0eafe4de D |
1534 | /* Turn on LED */ |
1535 | if (mos7840_port->has_led && !mos7840_port->led_flag) { | |
1536 | mos7840_port->led_flag = true; | |
1537 | mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301); | |
1538 | mod_timer(&mos7840_port->led_timer1, | |
1539 | jiffies + msecs_to_jiffies(LED_ON_MS)); | |
1540 | } | |
1541 | ||
3f542974 PS |
1542 | /* send it down the pipe */ |
1543 | status = usb_submit_urb(urb, GFP_ATOMIC); | |
1544 | ||
1545 | if (status) { | |
0de9a702 | 1546 | mos7840_port->busy[i] = 0; |
22a416c4 | 1547 | dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " |
194343d9 | 1548 | "with status = %d\n", __func__, status); |
3f542974 PS |
1549 | bytes_sent = status; |
1550 | goto exit; | |
1551 | } | |
1552 | bytes_sent = transfer_size; | |
1553 | mos7840_port->icount.tx += transfer_size; | |
0de9a702 | 1554 | smp_wmb(); |
9c134a14 | 1555 | dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx); |
95da310e | 1556 | exit: |
3f542974 PS |
1557 | return bytes_sent; |
1558 | ||
1559 | } | |
1560 | ||
1561 | /***************************************************************************** | |
1562 | * mos7840_throttle | |
1563 | * this function is called by the tty driver when it wants to stop the data | |
1564 | * being read from the port. | |
1565 | *****************************************************************************/ | |
1566 | ||
95da310e | 1567 | static void mos7840_throttle(struct tty_struct *tty) |
3f542974 | 1568 | { |
95da310e | 1569 | struct usb_serial_port *port = tty->driver_data; |
3f542974 | 1570 | struct moschip_port *mos7840_port; |
3f542974 PS |
1571 | int status; |
1572 | ||
9c134a14 | 1573 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1574 | return; |
3f542974 PS |
1575 | |
1576 | mos7840_port = mos7840_get_port_private(port); | |
1577 | ||
1578 | if (mos7840_port == NULL) | |
1579 | return; | |
1580 | ||
1581 | if (!mos7840_port->open) { | |
9c134a14 | 1582 | dev_dbg(&port->dev, "%s", "port not opened\n"); |
3f542974 PS |
1583 | return; |
1584 | } | |
1585 | ||
3f542974 PS |
1586 | /* if we are implementing XON/XOFF, send the stop character */ |
1587 | if (I_IXOFF(tty)) { | |
1588 | unsigned char stop_char = STOP_CHAR(tty); | |
95da310e AC |
1589 | status = mos7840_write(tty, port, &stop_char, 1); |
1590 | if (status <= 0) | |
3f542974 | 1591 | return; |
3f542974 | 1592 | } |
3f542974 | 1593 | /* if we are implementing RTS/CTS, toggle that line */ |
adc8d746 | 1594 | if (tty->termios.c_cflag & CRTSCTS) { |
3f542974 | 1595 | mos7840_port->shadowMCR &= ~MCR_RTS; |
95da310e | 1596 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
3f542974 | 1597 | mos7840_port->shadowMCR); |
95da310e | 1598 | if (status < 0) |
3f542974 | 1599 | return; |
3f542974 | 1600 | } |
3f542974 PS |
1601 | } |
1602 | ||
1603 | /***************************************************************************** | |
1604 | * mos7840_unthrottle | |
880af9db AC |
1605 | * this function is called by the tty driver when it wants to resume |
1606 | * the data being read from the port (called after mos7840_throttle is | |
1607 | * called) | |
3f542974 | 1608 | *****************************************************************************/ |
95da310e | 1609 | static void mos7840_unthrottle(struct tty_struct *tty) |
3f542974 | 1610 | { |
95da310e | 1611 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1612 | int status; |
1613 | struct moschip_port *mos7840_port = mos7840_get_port_private(port); | |
1614 | ||
9c134a14 | 1615 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1616 | return; |
3f542974 PS |
1617 | |
1618 | if (mos7840_port == NULL) | |
1619 | return; | |
1620 | ||
1621 | if (!mos7840_port->open) { | |
9c134a14 | 1622 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
3f542974 PS |
1623 | return; |
1624 | } | |
1625 | ||
3f542974 PS |
1626 | /* if we are implementing XON/XOFF, send the start character */ |
1627 | if (I_IXOFF(tty)) { | |
1628 | unsigned char start_char = START_CHAR(tty); | |
95da310e AC |
1629 | status = mos7840_write(tty, port, &start_char, 1); |
1630 | if (status <= 0) | |
3f542974 | 1631 | return; |
3f542974 PS |
1632 | } |
1633 | ||
1634 | /* if we are implementing RTS/CTS, toggle that line */ | |
adc8d746 | 1635 | if (tty->termios.c_cflag & CRTSCTS) { |
3f542974 | 1636 | mos7840_port->shadowMCR |= MCR_RTS; |
95da310e | 1637 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
3f542974 | 1638 | mos7840_port->shadowMCR); |
95da310e | 1639 | if (status < 0) |
3f542974 | 1640 | return; |
3f542974 | 1641 | } |
3f542974 PS |
1642 | } |
1643 | ||
60b33c13 | 1644 | static int mos7840_tiocmget(struct tty_struct *tty) |
3f542974 | 1645 | { |
95da310e | 1646 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1647 | struct moschip_port *mos7840_port; |
1648 | unsigned int result; | |
1649 | __u16 msr; | |
1650 | __u16 mcr; | |
880af9db | 1651 | int status; |
3f542974 PS |
1652 | mos7840_port = mos7840_get_port_private(port); |
1653 | ||
3f542974 PS |
1654 | if (mos7840_port == NULL) |
1655 | return -ENODEV; | |
1656 | ||
1657 | status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); | |
1658 | status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); | |
1659 | result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) | |
1660 | | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) | |
1661 | | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) | |
1662 | | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) | |
1663 | | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) | |
1664 | | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) | |
1665 | | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); | |
1666 | ||
9c134a14 | 1667 | dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); |
3f542974 PS |
1668 | |
1669 | return result; | |
1670 | } | |
1671 | ||
20b9d177 | 1672 | static int mos7840_tiocmset(struct tty_struct *tty, |
3f542974 PS |
1673 | unsigned int set, unsigned int clear) |
1674 | { | |
95da310e | 1675 | struct usb_serial_port *port = tty->driver_data; |
3f542974 PS |
1676 | struct moschip_port *mos7840_port; |
1677 | unsigned int mcr; | |
87521c46 | 1678 | int status; |
3f542974 | 1679 | |
3f542974 PS |
1680 | mos7840_port = mos7840_get_port_private(port); |
1681 | ||
1682 | if (mos7840_port == NULL) | |
1683 | return -ENODEV; | |
1684 | ||
e2984494 | 1685 | /* FIXME: What locks the port registers ? */ |
3f542974 PS |
1686 | mcr = mos7840_port->shadowMCR; |
1687 | if (clear & TIOCM_RTS) | |
1688 | mcr &= ~MCR_RTS; | |
1689 | if (clear & TIOCM_DTR) | |
1690 | mcr &= ~MCR_DTR; | |
1691 | if (clear & TIOCM_LOOP) | |
1692 | mcr &= ~MCR_LOOPBACK; | |
1693 | ||
1694 | if (set & TIOCM_RTS) | |
1695 | mcr |= MCR_RTS; | |
1696 | if (set & TIOCM_DTR) | |
1697 | mcr |= MCR_DTR; | |
1698 | if (set & TIOCM_LOOP) | |
1699 | mcr |= MCR_LOOPBACK; | |
1700 | ||
1701 | mos7840_port->shadowMCR = mcr; | |
1702 | ||
3f542974 PS |
1703 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); |
1704 | if (status < 0) { | |
9c134a14 | 1705 | dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n"); |
87521c46 | 1706 | return status; |
3f542974 PS |
1707 | } |
1708 | ||
1709 | return 0; | |
1710 | } | |
1711 | ||
1712 | /***************************************************************************** | |
1713 | * mos7840_calc_baud_rate_divisor | |
1714 | * this function calculates the proper baud rate divisor for the specified | |
1715 | * baud rate. | |
1716 | *****************************************************************************/ | |
9c134a14 GKH |
1717 | static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port, |
1718 | int baudRate, int *divisor, | |
880af9db | 1719 | __u16 *clk_sel_val) |
3f542974 | 1720 | { |
9c134a14 | 1721 | dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate); |
3f542974 PS |
1722 | |
1723 | if (baudRate <= 115200) { | |
1724 | *divisor = 115200 / baudRate; | |
1725 | *clk_sel_val = 0x0; | |
1726 | } | |
1727 | if ((baudRate > 115200) && (baudRate <= 230400)) { | |
1728 | *divisor = 230400 / baudRate; | |
1729 | *clk_sel_val = 0x10; | |
1730 | } else if ((baudRate > 230400) && (baudRate <= 403200)) { | |
1731 | *divisor = 403200 / baudRate; | |
1732 | *clk_sel_val = 0x20; | |
1733 | } else if ((baudRate > 403200) && (baudRate <= 460800)) { | |
1734 | *divisor = 460800 / baudRate; | |
1735 | *clk_sel_val = 0x30; | |
1736 | } else if ((baudRate > 460800) && (baudRate <= 806400)) { | |
1737 | *divisor = 806400 / baudRate; | |
1738 | *clk_sel_val = 0x40; | |
1739 | } else if ((baudRate > 806400) && (baudRate <= 921600)) { | |
1740 | *divisor = 921600 / baudRate; | |
1741 | *clk_sel_val = 0x50; | |
1742 | } else if ((baudRate > 921600) && (baudRate <= 1572864)) { | |
1743 | *divisor = 1572864 / baudRate; | |
1744 | *clk_sel_val = 0x60; | |
1745 | } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { | |
1746 | *divisor = 3145728 / baudRate; | |
1747 | *clk_sel_val = 0x70; | |
1748 | } | |
1749 | return 0; | |
1750 | ||
1751 | #ifdef NOTMCS7840 | |
1752 | ||
1753 | for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) { | |
1754 | if (mos7840_divisor_table[i].BaudRate == baudrate) { | |
1755 | *divisor = mos7840_divisor_table[i].Divisor; | |
1756 | return 0; | |
1757 | } | |
1758 | } | |
1759 | ||
1760 | /* After trying for all the standard baud rates * | |
1761 | * Try calculating the divisor for this baud rate */ | |
1762 | ||
1763 | if (baudrate > 75 && baudrate < 230400) { | |
1764 | /* get the divisor */ | |
1765 | custom = (__u16) (230400L / baudrate); | |
1766 | ||
1767 | /* Check for round off */ | |
1768 | round1 = (__u16) (2304000L / baudrate); | |
1769 | round = (__u16) (round1 - (custom * 10)); | |
880af9db | 1770 | if (round > 4) |
3f542974 | 1771 | custom++; |
3f542974 PS |
1772 | *divisor = custom; |
1773 | ||
9c134a14 | 1774 | dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom); |
3f542974 PS |
1775 | return 0; |
1776 | } | |
1777 | ||
9c134a14 | 1778 | dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n"); |
3f542974 PS |
1779 | return -1; |
1780 | #endif | |
1781 | } | |
1782 | ||
1783 | /***************************************************************************** | |
1784 | * mos7840_send_cmd_write_baud_rate | |
1785 | * this function sends the proper command to change the baud rate of the | |
1786 | * specified port. | |
1787 | *****************************************************************************/ | |
1788 | ||
1789 | static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, | |
1790 | int baudRate) | |
1791 | { | |
1792 | int divisor = 0; | |
1793 | int status; | |
1794 | __u16 Data; | |
1795 | unsigned char number; | |
1796 | __u16 clk_sel_val; | |
1797 | struct usb_serial_port *port; | |
1798 | ||
1799 | if (mos7840_port == NULL) | |
1800 | return -1; | |
1801 | ||
9c134a14 GKH |
1802 | port = mos7840_port->port; |
1803 | if (mos7840_port_paranoia_check(port, __func__)) | |
3f542974 | 1804 | return -1; |
3f542974 | 1805 | |
9c134a14 | 1806 | if (mos7840_serial_paranoia_check(port->serial, __func__)) |
3f542974 | 1807 | return -1; |
3f542974 | 1808 | |
3f542974 PS |
1809 | number = mos7840_port->port->number - mos7840_port->port->serial->minor; |
1810 | ||
9c134a14 GKH |
1811 | dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__, |
1812 | mos7840_port->port->number, baudRate); | |
880af9db | 1813 | /* reset clk_uart_sel in spregOffset */ |
3f542974 PS |
1814 | if (baudRate > 115200) { |
1815 | #ifdef HW_flow_control | |
880af9db AC |
1816 | /* NOTE: need to see the pther register to modify */ |
1817 | /* setting h/w flow control bit to 1 */ | |
3f542974 PS |
1818 | Data = 0x2b; |
1819 | mos7840_port->shadowMCR = Data; | |
880af9db AC |
1820 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
1821 | Data); | |
3f542974 | 1822 | if (status < 0) { |
9c134a14 | 1823 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
3f542974 PS |
1824 | return -1; |
1825 | } | |
1826 | #endif | |
1827 | ||
1828 | } else { | |
1829 | #ifdef HW_flow_control | |
093ea2d3 | 1830 | /* setting h/w flow control bit to 0 */ |
3f542974 PS |
1831 | Data = 0xb; |
1832 | mos7840_port->shadowMCR = Data; | |
880af9db AC |
1833 | status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, |
1834 | Data); | |
3f542974 | 1835 | if (status < 0) { |
9c134a14 | 1836 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
3f542974 PS |
1837 | return -1; |
1838 | } | |
1839 | #endif | |
1840 | ||
1841 | } | |
1842 | ||
880af9db | 1843 | if (1) { /* baudRate <= 115200) */ |
3f542974 PS |
1844 | clk_sel_val = 0x0; |
1845 | Data = 0x0; | |
9c134a14 | 1846 | status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor, |
3f542974 | 1847 | &clk_sel_val); |
880af9db AC |
1848 | status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, |
1849 | &Data); | |
3f542974 | 1850 | if (status < 0) { |
9c134a14 | 1851 | dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n"); |
3f542974 PS |
1852 | return -1; |
1853 | } | |
1854 | Data = (Data & 0x8f) | clk_sel_val; | |
880af9db AC |
1855 | status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, |
1856 | Data); | |
3f542974 | 1857 | if (status < 0) { |
9c134a14 | 1858 | dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); |
3f542974 PS |
1859 | return -1; |
1860 | } | |
1861 | /* Calculate the Divisor */ | |
1862 | ||
1863 | if (status) { | |
194343d9 | 1864 | dev_err(&port->dev, "%s - bad baud rate\n", __func__); |
3f542974 PS |
1865 | return status; |
1866 | } | |
1867 | /* Enable access to divisor latch */ | |
1868 | Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; | |
1869 | mos7840_port->shadowLCR = Data; | |
1870 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); | |
1871 | ||
1872 | /* Write the divisor */ | |
1873 | Data = (unsigned char)(divisor & 0xff); | |
9c134a14 | 1874 | dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data); |
3f542974 PS |
1875 | mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); |
1876 | ||
1877 | Data = (unsigned char)((divisor & 0xff00) >> 8); | |
9c134a14 | 1878 | dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data); |
3f542974 PS |
1879 | mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); |
1880 | ||
1881 | /* Disable access to divisor latch */ | |
1882 | Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; | |
1883 | mos7840_port->shadowLCR = Data; | |
1884 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); | |
1885 | ||
1886 | } | |
3f542974 PS |
1887 | return status; |
1888 | } | |
1889 | ||
1890 | /***************************************************************************** | |
1891 | * mos7840_change_port_settings | |
1892 | * This routine is called to set the UART on the device to match | |
1893 | * the specified new settings. | |
1894 | *****************************************************************************/ | |
1895 | ||
95da310e AC |
1896 | static void mos7840_change_port_settings(struct tty_struct *tty, |
1897 | struct moschip_port *mos7840_port, struct ktermios *old_termios) | |
3f542974 | 1898 | { |
3f542974 PS |
1899 | int baud; |
1900 | unsigned cflag; | |
1901 | unsigned iflag; | |
1902 | __u8 lData; | |
1903 | __u8 lParity; | |
1904 | __u8 lStop; | |
1905 | int status; | |
1906 | __u16 Data; | |
1907 | struct usb_serial_port *port; | |
1908 | struct usb_serial *serial; | |
1909 | ||
1910 | if (mos7840_port == NULL) | |
1911 | return; | |
1912 | ||
9c134a14 | 1913 | port = mos7840_port->port; |
3f542974 | 1914 | |
9c134a14 | 1915 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 1916 | return; |
3f542974 | 1917 | |
9c134a14 | 1918 | if (mos7840_serial_paranoia_check(port->serial, __func__)) |
3f542974 | 1919 | return; |
3f542974 PS |
1920 | |
1921 | serial = port->serial; | |
1922 | ||
3f542974 | 1923 | if (!mos7840_port->open) { |
9c134a14 | 1924 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
3f542974 PS |
1925 | return; |
1926 | } | |
1927 | ||
3f542974 PS |
1928 | lData = LCR_BITS_8; |
1929 | lStop = LCR_STOP_1; | |
1930 | lParity = LCR_PAR_NONE; | |
1931 | ||
adc8d746 AC |
1932 | cflag = tty->termios.c_cflag; |
1933 | iflag = tty->termios.c_iflag; | |
3f542974 PS |
1934 | |
1935 | /* Change the number of bits */ | |
1936 | if (cflag & CSIZE) { | |
1937 | switch (cflag & CSIZE) { | |
1938 | case CS5: | |
1939 | lData = LCR_BITS_5; | |
1940 | break; | |
1941 | ||
1942 | case CS6: | |
1943 | lData = LCR_BITS_6; | |
1944 | break; | |
1945 | ||
1946 | case CS7: | |
1947 | lData = LCR_BITS_7; | |
1948 | break; | |
1949 | default: | |
1950 | case CS8: | |
1951 | lData = LCR_BITS_8; | |
1952 | break; | |
1953 | } | |
1954 | } | |
1955 | /* Change the Parity bit */ | |
1956 | if (cflag & PARENB) { | |
1957 | if (cflag & PARODD) { | |
1958 | lParity = LCR_PAR_ODD; | |
9c134a14 | 1959 | dev_dbg(&port->dev, "%s - parity = odd\n", __func__); |
3f542974 PS |
1960 | } else { |
1961 | lParity = LCR_PAR_EVEN; | |
9c134a14 | 1962 | dev_dbg(&port->dev, "%s - parity = even\n", __func__); |
3f542974 PS |
1963 | } |
1964 | ||
1965 | } else { | |
9c134a14 | 1966 | dev_dbg(&port->dev, "%s - parity = none\n", __func__); |
3f542974 PS |
1967 | } |
1968 | ||
880af9db | 1969 | if (cflag & CMSPAR) |
3f542974 | 1970 | lParity = lParity | 0x20; |
3f542974 PS |
1971 | |
1972 | /* Change the Stop bit */ | |
1973 | if (cflag & CSTOPB) { | |
1974 | lStop = LCR_STOP_2; | |
9c134a14 | 1975 | dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); |
3f542974 PS |
1976 | } else { |
1977 | lStop = LCR_STOP_1; | |
9c134a14 | 1978 | dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); |
3f542974 PS |
1979 | } |
1980 | ||
1981 | /* Update the LCR with the correct value */ | |
1982 | mos7840_port->shadowLCR &= | |
1983 | ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); | |
1984 | mos7840_port->shadowLCR |= (lData | lParity | lStop); | |
1985 | ||
9c134a14 GKH |
1986 | dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__, |
1987 | mos7840_port->shadowLCR); | |
3f542974 PS |
1988 | /* Disable Interrupts */ |
1989 | Data = 0x00; | |
1990 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); | |
1991 | ||
1992 | Data = 0x00; | |
1993 | mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); | |
1994 | ||
1995 | Data = 0xcf; | |
1996 | mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); | |
1997 | ||
1998 | /* Send the updated LCR value to the mos7840 */ | |
1999 | Data = mos7840_port->shadowLCR; | |
2000 | ||
2001 | mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); | |
2002 | ||
2003 | Data = 0x00b; | |
2004 | mos7840_port->shadowMCR = Data; | |
2005 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); | |
2006 | Data = 0x00b; | |
2007 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); | |
2008 | ||
2009 | /* set up the MCR register and send it to the mos7840 */ | |
2010 | ||
2011 | mos7840_port->shadowMCR = MCR_MASTER_IE; | |
880af9db | 2012 | if (cflag & CBAUD) |
3f542974 | 2013 | mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); |
3f542974 | 2014 | |
880af9db | 2015 | if (cflag & CRTSCTS) |
3f542974 | 2016 | mos7840_port->shadowMCR |= (MCR_XON_ANY); |
880af9db | 2017 | else |
3f542974 | 2018 | mos7840_port->shadowMCR &= ~(MCR_XON_ANY); |
3f542974 PS |
2019 | |
2020 | Data = mos7840_port->shadowMCR; | |
2021 | mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); | |
2022 | ||
2023 | /* Determine divisor based on baud rate */ | |
2024 | baud = tty_get_baud_rate(tty); | |
2025 | ||
2026 | if (!baud) { | |
2027 | /* pick a default, any default... */ | |
9c134a14 | 2028 | dev_dbg(&port->dev, "%s", "Picked default baud...\n"); |
3f542974 PS |
2029 | baud = 9600; |
2030 | } | |
2031 | ||
9c134a14 | 2032 | dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); |
3f542974 PS |
2033 | status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); |
2034 | ||
2035 | /* Enable Interrupts */ | |
2036 | Data = 0x0c; | |
2037 | mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); | |
2038 | ||
50de36f7 | 2039 | if (mos7840_port->read_urb_busy == false) { |
50de36f7 | 2040 | mos7840_port->read_urb_busy = true; |
3f542974 | 2041 | status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
3f542974 | 2042 | if (status) { |
9c134a14 | 2043 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", |
3f542974 | 2044 | status); |
50de36f7 | 2045 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
2046 | } |
2047 | } | |
2048 | wake_up(&mos7840_port->delta_msr_wait); | |
2049 | mos7840_port->delta_msr_cond = 1; | |
9c134a14 GKH |
2050 | dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__, |
2051 | mos7840_port->shadowLCR); | |
3f542974 PS |
2052 | } |
2053 | ||
2054 | /***************************************************************************** | |
2055 | * mos7840_set_termios | |
2056 | * this function is called by the tty driver when it wants to change | |
2057 | * the termios structure | |
2058 | *****************************************************************************/ | |
2059 | ||
95da310e AC |
2060 | static void mos7840_set_termios(struct tty_struct *tty, |
2061 | struct usb_serial_port *port, | |
606d099c | 2062 | struct ktermios *old_termios) |
3f542974 PS |
2063 | { |
2064 | int status; | |
2065 | unsigned int cflag; | |
2066 | struct usb_serial *serial; | |
2067 | struct moschip_port *mos7840_port; | |
3363155b | 2068 | |
9c134a14 | 2069 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 2070 | return; |
3f542974 PS |
2071 | |
2072 | serial = port->serial; | |
2073 | ||
9c134a14 | 2074 | if (mos7840_serial_paranoia_check(serial, __func__)) |
3f542974 | 2075 | return; |
3f542974 PS |
2076 | |
2077 | mos7840_port = mos7840_get_port_private(port); | |
2078 | ||
2079 | if (mos7840_port == NULL) | |
2080 | return; | |
2081 | ||
3f542974 | 2082 | if (!mos7840_port->open) { |
9c134a14 | 2083 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
3f542974 PS |
2084 | return; |
2085 | } | |
2086 | ||
9c134a14 | 2087 | dev_dbg(&port->dev, "%s", "setting termios - \n"); |
3f542974 | 2088 | |
adc8d746 | 2089 | cflag = tty->termios.c_cflag; |
3f542974 | 2090 | |
9c134a14 | 2091 | dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, |
d9a80746 | 2092 | tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag)); |
9c134a14 GKH |
2093 | dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, |
2094 | old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); | |
2095 | dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number); | |
3f542974 PS |
2096 | |
2097 | /* change the port settings to the new ones specified */ | |
2098 | ||
95da310e | 2099 | mos7840_change_port_settings(tty, mos7840_port, old_termios); |
3f542974 PS |
2100 | |
2101 | if (!mos7840_port->read_urb) { | |
9c134a14 | 2102 | dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n"); |
3f542974 PS |
2103 | return; |
2104 | } | |
2105 | ||
50de36f7 | 2106 | if (mos7840_port->read_urb_busy == false) { |
50de36f7 | 2107 | mos7840_port->read_urb_busy = true; |
3f542974 PS |
2108 | status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); |
2109 | if (status) { | |
9c134a14 | 2110 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", |
3f542974 | 2111 | status); |
50de36f7 | 2112 | mos7840_port->read_urb_busy = false; |
3f542974 PS |
2113 | } |
2114 | } | |
3f542974 PS |
2115 | } |
2116 | ||
2117 | /***************************************************************************** | |
2118 | * mos7840_get_lsr_info - get line status register info | |
2119 | * | |
2120 | * Purpose: Let user call ioctl() to get info when the UART physically | |
2121 | * is emptied. On bus types like RS485, the transmitter must | |
2122 | * release the bus after transmitting. This must be done when | |
2123 | * the transmit shift register is empty, not be done when the | |
2124 | * transmit holding register is empty. This functionality | |
2125 | * allows an RS485 driver to be written in user space. | |
2126 | *****************************************************************************/ | |
2127 | ||
95da310e | 2128 | static int mos7840_get_lsr_info(struct tty_struct *tty, |
97c4965d | 2129 | unsigned int __user *value) |
3f542974 PS |
2130 | { |
2131 | int count; | |
2132 | unsigned int result = 0; | |
2133 | ||
95da310e | 2134 | count = mos7840_chars_in_buffer(tty); |
9c134a14 | 2135 | if (count == 0) |
3f542974 | 2136 | result = TIOCSER_TEMT; |
3f542974 PS |
2137 | |
2138 | if (copy_to_user(value, &result, sizeof(int))) | |
2139 | return -EFAULT; | |
2140 | return 0; | |
2141 | } | |
2142 | ||
3f542974 PS |
2143 | /***************************************************************************** |
2144 | * mos7840_get_serial_info | |
2145 | * function to get information about serial port | |
2146 | *****************************************************************************/ | |
2147 | ||
2148 | static int mos7840_get_serial_info(struct moschip_port *mos7840_port, | |
97c4965d | 2149 | struct serial_struct __user *retinfo) |
3f542974 PS |
2150 | { |
2151 | struct serial_struct tmp; | |
2152 | ||
2153 | if (mos7840_port == NULL) | |
2154 | return -1; | |
2155 | ||
2156 | if (!retinfo) | |
2157 | return -EFAULT; | |
2158 | ||
2159 | memset(&tmp, 0, sizeof(tmp)); | |
2160 | ||
2161 | tmp.type = PORT_16550A; | |
2162 | tmp.line = mos7840_port->port->serial->minor; | |
2163 | tmp.port = mos7840_port->port->number; | |
2164 | tmp.irq = 0; | |
2165 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | |
2166 | tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; | |
2167 | tmp.baud_base = 9600; | |
2168 | tmp.close_delay = 5 * HZ; | |
2169 | tmp.closing_wait = 30 * HZ; | |
2170 | ||
2171 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) | |
2172 | return -EFAULT; | |
2173 | return 0; | |
2174 | } | |
2175 | ||
0bca1b91 AC |
2176 | static int mos7840_get_icount(struct tty_struct *tty, |
2177 | struct serial_icounter_struct *icount) | |
2178 | { | |
2179 | struct usb_serial_port *port = tty->driver_data; | |
2180 | struct moschip_port *mos7840_port; | |
2181 | struct async_icount cnow; | |
2182 | ||
2183 | mos7840_port = mos7840_get_port_private(port); | |
2184 | cnow = mos7840_port->icount; | |
2185 | ||
2186 | smp_rmb(); | |
2187 | icount->cts = cnow.cts; | |
2188 | icount->dsr = cnow.dsr; | |
2189 | icount->rng = cnow.rng; | |
2190 | icount->dcd = cnow.dcd; | |
2191 | icount->rx = cnow.rx; | |
2192 | icount->tx = cnow.tx; | |
2193 | icount->frame = cnow.frame; | |
2194 | icount->overrun = cnow.overrun; | |
2195 | icount->parity = cnow.parity; | |
2196 | icount->brk = cnow.brk; | |
2197 | icount->buf_overrun = cnow.buf_overrun; | |
2198 | ||
9c134a14 GKH |
2199 | dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__, |
2200 | icount->rx, icount->tx); | |
0bca1b91 AC |
2201 | return 0; |
2202 | } | |
2203 | ||
3f542974 PS |
2204 | /***************************************************************************** |
2205 | * SerialIoctl | |
2206 | * this function handles any ioctl calls to the driver | |
2207 | *****************************************************************************/ | |
2208 | ||
00a0d0d6 | 2209 | static int mos7840_ioctl(struct tty_struct *tty, |
3f542974 PS |
2210 | unsigned int cmd, unsigned long arg) |
2211 | { | |
95da310e | 2212 | struct usb_serial_port *port = tty->driver_data; |
97c4965d | 2213 | void __user *argp = (void __user *)arg; |
3f542974 | 2214 | struct moschip_port *mos7840_port; |
3f542974 PS |
2215 | |
2216 | struct async_icount cnow; | |
2217 | struct async_icount cprev; | |
3f542974 | 2218 | |
9c134a14 | 2219 | if (mos7840_port_paranoia_check(port, __func__)) |
3f542974 | 2220 | return -1; |
3f542974 PS |
2221 | |
2222 | mos7840_port = mos7840_get_port_private(port); | |
3f542974 PS |
2223 | |
2224 | if (mos7840_port == NULL) | |
2225 | return -1; | |
2226 | ||
9c134a14 | 2227 | dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd); |
3f542974 PS |
2228 | |
2229 | switch (cmd) { | |
2230 | /* return number of bytes available */ | |
2231 | ||
3f542974 | 2232 | case TIOCSERGETLSR: |
9c134a14 | 2233 | dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); |
95da310e | 2234 | return mos7840_get_lsr_info(tty, argp); |
3f542974 | 2235 | |
3f542974 | 2236 | case TIOCGSERIAL: |
9c134a14 | 2237 | dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); |
97c4965d | 2238 | return mos7840_get_serial_info(mos7840_port, argp); |
3f542974 PS |
2239 | |
2240 | case TIOCSSERIAL: | |
9c134a14 | 2241 | dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__); |
3f542974 PS |
2242 | break; |
2243 | ||
2244 | case TIOCMIWAIT: | |
9c134a14 | 2245 | dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__); |
3f542974 PS |
2246 | cprev = mos7840_port->icount; |
2247 | while (1) { | |
880af9db | 2248 | /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */ |
3f542974 PS |
2249 | mos7840_port->delta_msr_cond = 0; |
2250 | wait_event_interruptible(mos7840_port->delta_msr_wait, | |
2251 | (mos7840_port-> | |
2252 | delta_msr_cond == 1)); | |
2253 | ||
2254 | /* see if a signal did it */ | |
2255 | if (signal_pending(current)) | |
2256 | return -ERESTARTSYS; | |
2257 | cnow = mos7840_port->icount; | |
0de9a702 | 2258 | smp_rmb(); |
3f542974 PS |
2259 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && |
2260 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) | |
2261 | return -EIO; /* no change => error */ | |
2262 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || | |
2263 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || | |
2264 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || | |
2265 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { | |
2266 | return 0; | |
2267 | } | |
2268 | cprev = cnow; | |
2269 | } | |
2270 | /* NOTREACHED */ | |
2271 | break; | |
2272 | ||
3f542974 PS |
2273 | default: |
2274 | break; | |
2275 | } | |
3f542974 PS |
2276 | return -ENOIOCTLCMD; |
2277 | } | |
2278 | ||
0eafe4de D |
2279 | static int mos7810_check(struct usb_serial *serial) |
2280 | { | |
2281 | int i, pass_count = 0; | |
2282 | __u16 data = 0, mcr_data = 0; | |
2283 | __u16 test_pattern = 0x55AA; | |
2284 | ||
2285 | /* Store MCR setting */ | |
2286 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), | |
2287 | MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, | |
2288 | &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); | |
2289 | ||
2290 | for (i = 0; i < 16; i++) { | |
2291 | /* Send the 1-bit test pattern out to MCS7810 test pin */ | |
2292 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
2293 | MCS_WRREQ, MCS_WR_RTYPE, | |
2294 | (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), | |
2295 | MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); | |
2296 | ||
2297 | /* Read the test pattern back */ | |
2298 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), | |
2299 | MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, | |
2300 | VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); | |
2301 | ||
2302 | /* If this is a MCS7810 device, both test patterns must match */ | |
2303 | if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) | |
2304 | break; | |
2305 | ||
2306 | pass_count++; | |
2307 | } | |
2308 | ||
2309 | /* Restore MCR setting */ | |
2310 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, | |
2311 | MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, | |
2312 | 0, MOS_WDR_TIMEOUT); | |
2313 | ||
2314 | if (pass_count == 16) | |
2315 | return 1; | |
2316 | ||
2317 | return 0; | |
2318 | } | |
2319 | ||
3f542974 PS |
2320 | static int mos7840_calc_num_ports(struct usb_serial *serial) |
2321 | { | |
0eafe4de | 2322 | __u16 data = 0x00; |
093ea2d3 DL |
2323 | int mos7840_num_ports; |
2324 | ||
0eafe4de D |
2325 | usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
2326 | MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, | |
093ea2d3 DL |
2327 | VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); |
2328 | ||
0eafe4de D |
2329 | if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 || |
2330 | serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) { | |
2331 | device_type = serial->dev->descriptor.idProduct; | |
093ea2d3 | 2332 | } else { |
0eafe4de D |
2333 | /* For a MCS7840 device GPIO0 must be set to 1 */ |
2334 | if ((data & 0x01) == 1) | |
2335 | device_type = MOSCHIP_DEVICE_ID_7840; | |
2336 | else if (mos7810_check(serial)) | |
2337 | device_type = MOSCHIP_DEVICE_ID_7810; | |
2338 | else | |
2339 | device_type = MOSCHIP_DEVICE_ID_7820; | |
3f542974 | 2340 | } |
093ea2d3 | 2341 | |
0eafe4de D |
2342 | mos7840_num_ports = (device_type >> 4) & 0x000F; |
2343 | serial->num_bulk_in = mos7840_num_ports; | |
2344 | serial->num_bulk_out = mos7840_num_ports; | |
2345 | serial->num_ports = mos7840_num_ports; | |
2346 | ||
3f542974 PS |
2347 | return mos7840_num_ports; |
2348 | } | |
2349 | ||
2350 | /**************************************************************************** | |
2351 | * mos7840_startup | |
2352 | ****************************************************************************/ | |
2353 | ||
2354 | static int mos7840_startup(struct usb_serial *serial) | |
2355 | { | |
2356 | struct moschip_port *mos7840_port; | |
2357 | struct usb_device *dev; | |
2358 | int i, status; | |
3f542974 | 2359 | __u16 Data; |
3f542974 | 2360 | |
3f542974 PS |
2361 | dev = serial->dev; |
2362 | ||
3f542974 PS |
2363 | /* we set up the pointers to the endpoints in the mos7840_open * |
2364 | * function, as the structures aren't created yet. */ | |
2365 | ||
2366 | /* set up port private structures */ | |
2367 | for (i = 0; i < serial->num_ports; ++i) { | |
9c134a14 | 2368 | dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i); |
7ac9da10 | 2369 | mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); |
3f542974 | 2370 | if (mos7840_port == NULL) { |
194343d9 | 2371 | dev_err(&dev->dev, "%s - Out of memory\n", __func__); |
0de9a702 ON |
2372 | status = -ENOMEM; |
2373 | i--; /* don't follow NULL pointer cleaning up */ | |
2374 | goto error; | |
3f542974 | 2375 | } |
3f542974 | 2376 | |
880af9db AC |
2377 | /* Initialize all port interrupt end point to port 0 int |
2378 | * endpoint. Our device has only one interrupt end point | |
2379 | * common to all port */ | |
3f542974 PS |
2380 | |
2381 | mos7840_port->port = serial->port[i]; | |
2382 | mos7840_set_port_private(serial->port[i], mos7840_port); | |
0de9a702 | 2383 | spin_lock_init(&mos7840_port->pool_lock); |
3f542974 | 2384 | |
37768adf TC |
2385 | /* minor is not initialised until later by |
2386 | * usb-serial.c:get_free_serial() and cannot therefore be used | |
2387 | * to index device instances */ | |
2388 | mos7840_port->port_num = i + 1; | |
9c134a14 GKH |
2389 | dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number); |
2390 | dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor); | |
2391 | dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num); | |
2392 | dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor); | |
3f542974 PS |
2393 | |
2394 | if (mos7840_port->port_num == 1) { | |
2395 | mos7840_port->SpRegOffset = 0x0; | |
2396 | mos7840_port->ControlRegOffset = 0x1; | |
2397 | mos7840_port->DcrRegOffset = 0x4; | |
2398 | } else if ((mos7840_port->port_num == 2) | |
0de9a702 | 2399 | && (serial->num_ports == 4)) { |
3f542974 PS |
2400 | mos7840_port->SpRegOffset = 0x8; |
2401 | mos7840_port->ControlRegOffset = 0x9; | |
2402 | mos7840_port->DcrRegOffset = 0x16; | |
2403 | } else if ((mos7840_port->port_num == 2) | |
0de9a702 | 2404 | && (serial->num_ports == 2)) { |
3f542974 PS |
2405 | mos7840_port->SpRegOffset = 0xa; |
2406 | mos7840_port->ControlRegOffset = 0xb; | |
2407 | mos7840_port->DcrRegOffset = 0x19; | |
2408 | } else if ((mos7840_port->port_num == 3) | |
0de9a702 | 2409 | && (serial->num_ports == 4)) { |
3f542974 PS |
2410 | mos7840_port->SpRegOffset = 0xa; |
2411 | mos7840_port->ControlRegOffset = 0xb; | |
2412 | mos7840_port->DcrRegOffset = 0x19; | |
2413 | } else if ((mos7840_port->port_num == 4) | |
0de9a702 | 2414 | && (serial->num_ports == 4)) { |
3f542974 PS |
2415 | mos7840_port->SpRegOffset = 0xc; |
2416 | mos7840_port->ControlRegOffset = 0xd; | |
2417 | mos7840_port->DcrRegOffset = 0x1c; | |
2418 | } | |
9c134a14 | 2419 | mos7840_dump_serial_port(serial->port[i], mos7840_port); |
3f542974 PS |
2420 | mos7840_set_port_private(serial->port[i], mos7840_port); |
2421 | ||
880af9db AC |
2422 | /* enable rx_disable bit in control register */ |
2423 | status = mos7840_get_reg_sync(serial->port[i], | |
2424 | mos7840_port->ControlRegOffset, &Data); | |
3f542974 | 2425 | if (status < 0) { |
9c134a14 | 2426 | dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status); |
3f542974 PS |
2427 | break; |
2428 | } else | |
9c134a14 | 2429 | dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status); |
880af9db AC |
2430 | Data |= 0x08; /* setting driver done bit */ |
2431 | Data |= 0x04; /* sp1_bit to have cts change reflect in | |
2432 | modem status reg */ | |
3f542974 | 2433 | |
880af9db AC |
2434 | /* Data |= 0x20; //rx_disable bit */ |
2435 | status = mos7840_set_reg_sync(serial->port[i], | |
3f542974 PS |
2436 | mos7840_port->ControlRegOffset, Data); |
2437 | if (status < 0) { | |
9c134a14 | 2438 | dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status); |
3f542974 PS |
2439 | break; |
2440 | } else | |
9c134a14 | 2441 | dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status); |
3f542974 | 2442 | |
880af9db AC |
2443 | /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 |
2444 | and 0x24 in DCR3 */ | |
3f542974 | 2445 | Data = 0x01; |
880af9db AC |
2446 | status = mos7840_set_reg_sync(serial->port[i], |
2447 | (__u16) (mos7840_port->DcrRegOffset + 0), Data); | |
3f542974 | 2448 | if (status < 0) { |
9c134a14 | 2449 | dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status); |
3f542974 PS |
2450 | break; |
2451 | } else | |
9c134a14 | 2452 | dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status); |
3f542974 PS |
2453 | |
2454 | Data = 0x05; | |
880af9db AC |
2455 | status = mos7840_set_reg_sync(serial->port[i], |
2456 | (__u16) (mos7840_port->DcrRegOffset + 1), Data); | |
3f542974 | 2457 | if (status < 0) { |
9c134a14 | 2458 | dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status); |
3f542974 PS |
2459 | break; |
2460 | } else | |
9c134a14 | 2461 | dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status); |
3f542974 PS |
2462 | |
2463 | Data = 0x24; | |
880af9db AC |
2464 | status = mos7840_set_reg_sync(serial->port[i], |
2465 | (__u16) (mos7840_port->DcrRegOffset + 2), Data); | |
3f542974 | 2466 | if (status < 0) { |
9c134a14 | 2467 | dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status); |
3f542974 PS |
2468 | break; |
2469 | } else | |
9c134a14 | 2470 | dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status); |
3f542974 | 2471 | |
880af9db | 2472 | /* write values in clkstart0x0 and clkmulti 0x20 */ |
3f542974 | 2473 | Data = 0x0; |
880af9db | 2474 | status = mos7840_set_reg_sync(serial->port[i], |
3f542974 PS |
2475 | CLK_START_VALUE_REGISTER, Data); |
2476 | if (status < 0) { | |
9c134a14 | 2477 | dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); |
3f542974 PS |
2478 | break; |
2479 | } else | |
9c134a14 | 2480 | dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status); |
3f542974 PS |
2481 | |
2482 | Data = 0x20; | |
880af9db AC |
2483 | status = mos7840_set_reg_sync(serial->port[i], |
2484 | CLK_MULTI_REGISTER, Data); | |
3f542974 | 2485 | if (status < 0) { |
9c134a14 | 2486 | dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status); |
0de9a702 | 2487 | goto error; |
3f542974 | 2488 | } else |
9c134a14 | 2489 | dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status); |
3f542974 | 2490 | |
880af9db | 2491 | /* write value 0x0 to scratchpad register */ |
3f542974 | 2492 | Data = 0x00; |
880af9db AC |
2493 | status = mos7840_set_uart_reg(serial->port[i], |
2494 | SCRATCH_PAD_REGISTER, Data); | |
3f542974 | 2495 | if (status < 0) { |
9c134a14 | 2496 | dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status); |
3f542974 PS |
2497 | break; |
2498 | } else | |
9c134a14 | 2499 | dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status); |
3f542974 | 2500 | |
880af9db | 2501 | /* Zero Length flag register */ |
3f542974 | 2502 | if ((mos7840_port->port_num != 1) |
0de9a702 | 2503 | && (serial->num_ports == 2)) { |
3f542974 PS |
2504 | |
2505 | Data = 0xff; | |
3f542974 | 2506 | status = mos7840_set_reg_sync(serial->port[i], |
880af9db AC |
2507 | (__u16) (ZLP_REG1 + |
2508 | ((__u16)mos7840_port->port_num)), Data); | |
9c134a14 GKH |
2509 | dev_dbg(&dev->dev, "ZLIP offset %x\n", |
2510 | (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num))); | |
3f542974 | 2511 | if (status < 0) { |
9c134a14 | 2512 | dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status); |
3f542974 PS |
2513 | break; |
2514 | } else | |
9c134a14 | 2515 | dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status); |
3f542974 PS |
2516 | } else { |
2517 | Data = 0xff; | |
3f542974 | 2518 | status = mos7840_set_reg_sync(serial->port[i], |
880af9db AC |
2519 | (__u16) (ZLP_REG1 + |
2520 | ((__u16)mos7840_port->port_num) - 0x1), Data); | |
9c134a14 GKH |
2521 | dev_dbg(&dev->dev, "ZLIP offset %x\n", |
2522 | (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1)); | |
3f542974 | 2523 | if (status < 0) { |
9c134a14 | 2524 | dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status); |
3f542974 PS |
2525 | break; |
2526 | } else | |
9c134a14 | 2527 | dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status); |
3f542974 PS |
2528 | |
2529 | } | |
0de9a702 | 2530 | mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); |
3f542974 | 2531 | mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); |
880af9db AC |
2532 | mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), |
2533 | GFP_KERNEL); | |
2534 | if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || | |
2535 | !mos7840_port->dr) { | |
0de9a702 ON |
2536 | status = -ENOMEM; |
2537 | goto error; | |
2538 | } | |
0eafe4de D |
2539 | |
2540 | mos7840_port->has_led = false; | |
2541 | ||
2542 | /* Initialize LED timers */ | |
2543 | if (device_type == MOSCHIP_DEVICE_ID_7810) { | |
2544 | mos7840_port->has_led = true; | |
2545 | ||
2546 | init_timer(&mos7840_port->led_timer1); | |
2547 | mos7840_port->led_timer1.function = mos7840_led_off; | |
2548 | mos7840_port->led_timer1.expires = | |
2549 | jiffies + msecs_to_jiffies(LED_ON_MS); | |
2550 | mos7840_port->led_timer1.data = | |
2551 | (unsigned long)mos7840_port; | |
2552 | ||
2553 | init_timer(&mos7840_port->led_timer2); | |
2554 | mos7840_port->led_timer2.function = | |
2555 | mos7840_led_flag_off; | |
2556 | mos7840_port->led_timer2.expires = | |
2557 | jiffies + msecs_to_jiffies(LED_OFF_MS); | |
2558 | mos7840_port->led_timer2.data = | |
2559 | (unsigned long)mos7840_port; | |
2560 | ||
2561 | mos7840_port->led_flag = false; | |
2562 | ||
2563 | /* Turn off LED */ | |
2564 | mos7840_set_led_sync(serial->port[i], | |
2565 | MODEM_CONTROL_REGISTER, 0x0300); | |
2566 | } | |
3f542974 PS |
2567 | } |
2568 | ||
880af9db | 2569 | /* Zero Length flag enable */ |
3f542974 | 2570 | Data = 0x0f; |
3f542974 PS |
2571 | status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); |
2572 | if (status < 0) { | |
9c134a14 | 2573 | dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status); |
7ced46c3 | 2574 | goto error; |
3f542974 | 2575 | } else |
9c134a14 | 2576 | dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status); |
3f542974 PS |
2577 | |
2578 | /* setting configuration feature to one */ | |
2579 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), | |
1e658489 | 2580 | (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT); |
3f542974 | 2581 | return 0; |
0de9a702 ON |
2582 | error: |
2583 | for (/* nothing */; i >= 0; i--) { | |
2584 | mos7840_port = mos7840_get_port_private(serial->port[i]); | |
2585 | ||
2586 | kfree(mos7840_port->dr); | |
2587 | kfree(mos7840_port->ctrl_buf); | |
2588 | usb_free_urb(mos7840_port->control_urb); | |
2589 | kfree(mos7840_port); | |
2590 | serial->port[i] = NULL; | |
2591 | } | |
2592 | return status; | |
3f542974 PS |
2593 | } |
2594 | ||
2595 | /**************************************************************************** | |
f9c99bb8 | 2596 | * mos7840_disconnect |
3f542974 PS |
2597 | * This function is called whenever the device is removed from the usb bus. |
2598 | ****************************************************************************/ | |
2599 | ||
f9c99bb8 | 2600 | static void mos7840_disconnect(struct usb_serial *serial) |
3f542974 PS |
2601 | { |
2602 | int i; | |
0de9a702 | 2603 | unsigned long flags; |
3f542974 | 2604 | struct moschip_port *mos7840_port; |
3f542974 | 2605 | |
880af9db | 2606 | /* check for the ports to be closed,close the ports and disconnect */ |
3f542974 PS |
2607 | |
2608 | /* free private structure allocated for serial port * | |
2609 | * stop reads and writes on all ports */ | |
2610 | ||
2611 | for (i = 0; i < serial->num_ports; ++i) { | |
2612 | mos7840_port = mos7840_get_port_private(serial->port[i]); | |
37768adf TC |
2613 | if (mos7840_port) { |
2614 | spin_lock_irqsave(&mos7840_port->pool_lock, flags); | |
2615 | mos7840_port->zombie = 1; | |
2616 | spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); | |
2617 | usb_kill_urb(mos7840_port->control_urb); | |
f9c99bb8 AS |
2618 | } |
2619 | } | |
f9c99bb8 AS |
2620 | } |
2621 | ||
2622 | /**************************************************************************** | |
2623 | * mos7840_release | |
2624 | * This function is called when the usb_serial structure is freed. | |
2625 | ****************************************************************************/ | |
2626 | ||
2627 | static void mos7840_release(struct usb_serial *serial) | |
2628 | { | |
2629 | int i; | |
2630 | struct moschip_port *mos7840_port; | |
f9c99bb8 | 2631 | |
f9c99bb8 AS |
2632 | /* check for the ports to be closed,close the ports and disconnect */ |
2633 | ||
2634 | /* free private structure allocated for serial port * | |
2635 | * stop reads and writes on all ports */ | |
2636 | ||
2637 | for (i = 0; i < serial->num_ports; ++i) { | |
2638 | mos7840_port = mos7840_get_port_private(serial->port[i]); | |
f9c99bb8 | 2639 | if (mos7840_port) { |
0eafe4de D |
2640 | if (mos7840_port->has_led) { |
2641 | /* Turn off LED */ | |
2642 | mos7840_set_led_sync(mos7840_port->port, | |
2643 | MODEM_CONTROL_REGISTER, 0x0300); | |
2644 | ||
2645 | del_timer_sync(&mos7840_port->led_timer1); | |
2646 | del_timer_sync(&mos7840_port->led_timer2); | |
2647 | } | |
37768adf TC |
2648 | kfree(mos7840_port->ctrl_buf); |
2649 | kfree(mos7840_port->dr); | |
2650 | kfree(mos7840_port); | |
2651 | } | |
3f542974 | 2652 | } |
3f542974 PS |
2653 | } |
2654 | ||
2655 | static struct usb_serial_driver moschip7840_4port_device = { | |
2656 | .driver = { | |
2657 | .owner = THIS_MODULE, | |
2658 | .name = "mos7840", | |
2659 | }, | |
2660 | .description = DRIVER_DESC, | |
68e24113 | 2661 | .id_table = id_table, |
3f542974 | 2662 | .num_ports = 4, |
3f542974 PS |
2663 | .open = mos7840_open, |
2664 | .close = mos7840_close, | |
2665 | .write = mos7840_write, | |
2666 | .write_room = mos7840_write_room, | |
2667 | .chars_in_buffer = mos7840_chars_in_buffer, | |
2668 | .throttle = mos7840_throttle, | |
2669 | .unthrottle = mos7840_unthrottle, | |
2670 | .calc_num_ports = mos7840_calc_num_ports, | |
2671 | #ifdef MCSSerialProbe | |
2672 | .probe = mos7840_serial_probe, | |
2673 | #endif | |
2674 | .ioctl = mos7840_ioctl, | |
2675 | .set_termios = mos7840_set_termios, | |
2676 | .break_ctl = mos7840_break, | |
2677 | .tiocmget = mos7840_tiocmget, | |
2678 | .tiocmset = mos7840_tiocmset, | |
0bca1b91 | 2679 | .get_icount = mos7840_get_icount, |
3f542974 | 2680 | .attach = mos7840_startup, |
f9c99bb8 AS |
2681 | .disconnect = mos7840_disconnect, |
2682 | .release = mos7840_release, | |
3f542974 PS |
2683 | .read_bulk_callback = mos7840_bulk_in_callback, |
2684 | .read_int_callback = mos7840_interrupt_callback, | |
2685 | }; | |
2686 | ||
4d2a7aff AS |
2687 | static struct usb_serial_driver * const serial_drivers[] = { |
2688 | &moschip7840_4port_device, NULL | |
2689 | }; | |
2690 | ||
68e24113 | 2691 | module_usb_serial_driver(serial_drivers, id_table); |
3f542974 | 2692 | |
3f542974 PS |
2693 | MODULE_DESCRIPTION(DRIVER_DESC); |
2694 | MODULE_LICENSE("GPL"); |