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