This source file includes following definitions.
- mos7840_set_reg_sync
- mos7840_get_reg_sync
- mos7840_set_uart_reg
- mos7840_get_uart_reg
- mos7840_dump_serial_port
- mos7840_set_port_private
- mos7840_get_port_private
- mos7840_handle_new_msr
- mos7840_handle_new_lsr
- mos7840_control_callback
- mos7840_get_reg
- mos7840_set_led_callback
- mos7840_set_led_async
- mos7840_set_led_sync
- mos7840_led_off
- mos7840_led_flag_off
- mos7840_led_activity
- mos7840_interrupt_callback
- mos7840_port_paranoia_check
- mos7840_serial_paranoia_check
- mos7840_get_usb_serial
- mos7840_bulk_in_callback
- mos7840_bulk_out_data_callback
- mos7840_open
- mos7840_chars_in_buffer
- mos7840_close
- mos7840_break
- mos7840_write_room
- mos7840_write
- mos7840_throttle
- mos7840_unthrottle
- mos7840_tiocmget
- mos7840_tiocmset
- mos7840_calc_baud_rate_divisor
- mos7840_send_cmd_write_baud_rate
- mos7840_change_port_settings
- mos7840_set_termios
- mos7840_get_lsr_info
- mos7840_get_serial_info
- mos7840_ioctl
- mos7810_check
- mos7840_probe
- mos7840_calc_num_ports
- mos7840_port_probe
- mos7840_port_remove
1
2
3
4
5
6
7
8
9
10
11
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/serial.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
23
24 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
25
26
27
28
29
30 #define LCR_BITS_5 0x00
31 #define LCR_BITS_6 0x01
32 #define LCR_BITS_7 0x02
33 #define LCR_BITS_8 0x03
34 #define LCR_BITS_MASK 0x03
35
36 #define LCR_STOP_1 0x00
37 #define LCR_STOP_1_5 0x04
38 #define LCR_STOP_2 0x04
39 #define LCR_STOP_MASK 0x04
40
41 #define LCR_PAR_NONE 0x00
42 #define LCR_PAR_ODD 0x08
43 #define LCR_PAR_EVEN 0x18
44 #define LCR_PAR_MARK 0x28
45 #define LCR_PAR_SPACE 0x38
46 #define LCR_PAR_MASK 0x38
47
48 #define LCR_SET_BREAK 0x40
49 #define LCR_DL_ENABLE 0x80
50
51 #define MCR_DTR 0x01
52 #define MCR_RTS 0x02
53 #define MCR_OUT1 0x04
54 #define MCR_MASTER_IE 0x08
55 #define MCR_LOOPBACK 0x10
56 #define MCR_XON_ANY 0x20
57
58 #define MOS7840_MSR_CTS 0x10
59 #define MOS7840_MSR_DSR 0x20
60 #define MOS7840_MSR_RI 0x40
61 #define MOS7840_MSR_CD 0x80
62
63
64
65
66
67 #define MOS_WDR_TIMEOUT 5000
68
69 #define MOS_PORT1 0x0200
70 #define MOS_PORT2 0x0300
71 #define MOS_VENREG 0x0000
72 #define MOS_MAX_PORT 0x02
73 #define MOS_WRITE 0x0E
74 #define MOS_READ 0x0D
75
76
77 #define MCS_RD_RTYPE 0xC0
78 #define MCS_WR_RTYPE 0x40
79 #define MCS_RDREQ 0x0D
80 #define MCS_WRREQ 0x0E
81 #define MCS_CTRL_TIMEOUT 500
82 #define VENDOR_READ_LENGTH (0x01)
83
84 #define MAX_NAME_LEN 64
85
86 #define ZLP_REG1 0x3A
87 #define ZLP_REG5 0x3E
88
89
90 #define TIOCEXBAUD 0x5462
91
92
93
94
95 #define USB_VENDOR_ID_MOSCHIP 0x9710
96 #define MOSCHIP_DEVICE_ID_7840 0x7840
97 #define MOSCHIP_DEVICE_ID_7843 0x7843
98 #define MOSCHIP_DEVICE_ID_7820 0x7820
99 #define MOSCHIP_DEVICE_ID_7810 0x7810
100
101
102
103
104 #define USB_VENDOR_ID_BANDB 0x0856
105 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
106 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
107 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
108 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
109 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
110 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
111 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
112 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
113 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
114 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
115 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
116 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
117 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
118
119
120
121
122
123
124 #define USB_VENDOR_ID_ATENINTL 0x0557
125 #define ATENINTL_DEVICE_ID_UC2324 0x2011
126 #define ATENINTL_DEVICE_ID_UC2322 0x7820
127
128 #define USB_VENDOR_ID_MOXA 0x110a
129 #define MOXA_DEVICE_ID_2210 0x2210
130
131
132
133 #define SERIAL_IIR_RLS 0x06
134 #define SERIAL_IIR_MS 0x00
135
136
137
138
139 #define SERIAL_LSR_DR 0x0001
140 #define SERIAL_LSR_OE 0x0002
141 #define SERIAL_LSR_PE 0x0004
142 #define SERIAL_LSR_FE 0x0008
143 #define SERIAL_LSR_BI 0x0010
144
145 #define MOS_MSR_DELTA_CTS 0x10
146 #define MOS_MSR_DELTA_DSR 0x20
147 #define MOS_MSR_DELTA_RI 0x40
148 #define MOS_MSR_DELTA_CD 0x80
149
150
151 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
152 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
153 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
154 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
155 #define LINE_STATUS_REGISTER ((__u16)(0x05))
156 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
157 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
158 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
159 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
160
161 #define CLK_MULTI_REGISTER ((__u16)(0x02))
162 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
163 #define GPIO_REGISTER ((__u16)(0x07))
164
165 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
166
167
168
169
170 #define NUM_URBS 16
171 #define URB_TRANSFER_BUFFER_SIZE 32
172
173
174 #define LED_ON_MS 500
175 #define LED_OFF_MS 500
176
177 enum mos7840_flag {
178 MOS7840_FLAG_CTRL_BUSY,
179 MOS7840_FLAG_LED_BUSY,
180 };
181
182 static const struct usb_device_id id_table[] = {
183 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7843)},
185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
186 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
200 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
201 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
202 {USB_DEVICE(USB_VENDOR_ID_MOXA, MOXA_DEVICE_ID_2210)},
203 {}
204 };
205 MODULE_DEVICE_TABLE(usb, id_table);
206
207
208
209 struct moschip_port {
210 int port_num;
211 struct urb *read_urb;
212 __u8 shadowLCR;
213 __u8 shadowMCR;
214 char open;
215 char open_ports;
216 struct usb_serial_port *port;
217
218
219 __u8 SpRegOffset;
220 __u8 ControlRegOffset;
221 __u8 DcrRegOffset;
222
223 struct urb *control_urb;
224 struct usb_ctrlrequest *dr;
225 char *ctrl_buf;
226 int MsrLsr;
227
228 spinlock_t pool_lock;
229 struct urb *write_urb_pool[NUM_URBS];
230 char busy[NUM_URBS];
231 bool read_urb_busy;
232
233
234 bool has_led;
235 struct timer_list led_timer1;
236 struct timer_list led_timer2;
237 struct urb *led_urb;
238 struct usb_ctrlrequest *led_dr;
239
240 unsigned long flags;
241 };
242
243
244
245
246
247
248
249 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
250 __u16 val)
251 {
252 struct usb_device *dev = port->serial->dev;
253 val = val & 0x00ff;
254 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
255
256 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
257 MCS_WR_RTYPE, val, reg, NULL, 0,
258 MOS_WDR_TIMEOUT);
259 }
260
261
262
263
264
265
266
267 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
268 __u16 *val)
269 {
270 struct usb_device *dev = port->serial->dev;
271 int ret = 0;
272 u8 *buf;
273
274 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
275 if (!buf)
276 return -ENOMEM;
277
278 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
279 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
280 MOS_WDR_TIMEOUT);
281 if (ret < VENDOR_READ_LENGTH) {
282 if (ret >= 0)
283 ret = -EIO;
284 goto out;
285 }
286
287 *val = buf[0];
288 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
289 out:
290 kfree(buf);
291 return ret;
292 }
293
294
295
296
297
298
299
300 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
301 __u16 val)
302 {
303
304 struct usb_device *dev = port->serial->dev;
305 val = val & 0x00ff;
306
307
308 if (port->serial->num_ports == 2 && port->port_number != 0)
309 val |= ((__u16)port->port_number + 2) << 8;
310 else
311 val |= ((__u16)port->port_number + 1) << 8;
312 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
313 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
314 MCS_WR_RTYPE, val, reg, NULL, 0,
315 MOS_WDR_TIMEOUT);
316
317 }
318
319
320
321
322
323
324 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
325 __u16 *val)
326 {
327 struct usb_device *dev = port->serial->dev;
328 int ret = 0;
329 __u16 Wval;
330 u8 *buf;
331
332 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
333 if (!buf)
334 return -ENOMEM;
335
336
337 if (port->serial->num_ports == 2 && port->port_number != 0)
338 Wval = ((__u16)port->port_number + 2) << 8;
339 else
340 Wval = ((__u16)port->port_number + 1) << 8;
341 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
342 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
343 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
344 MOS_WDR_TIMEOUT);
345 if (ret < VENDOR_READ_LENGTH) {
346 if (ret >= 0)
347 ret = -EIO;
348 goto out;
349 }
350 *val = buf[0];
351 out:
352 kfree(buf);
353 return ret;
354 }
355
356 static void mos7840_dump_serial_port(struct usb_serial_port *port,
357 struct moschip_port *mos7840_port)
358 {
359
360 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
361 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
362 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
363
364 }
365
366
367
368
369
370
371
372
373 static inline void mos7840_set_port_private(struct usb_serial_port *port,
374 struct moschip_port *data)
375 {
376 usb_set_serial_port_data(port, (void *)data);
377 }
378
379 static inline struct moschip_port *mos7840_get_port_private(struct
380 usb_serial_port
381 *port)
382 {
383 return (struct moschip_port *)usb_get_serial_port_data(port);
384 }
385
386 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
387 {
388 struct moschip_port *mos7840_port;
389 struct async_icount *icount;
390 mos7840_port = port;
391 if (new_msr &
392 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
393 MOS_MSR_DELTA_CD)) {
394 icount = &mos7840_port->port->icount;
395
396
397 if (new_msr & MOS_MSR_DELTA_CTS)
398 icount->cts++;
399 if (new_msr & MOS_MSR_DELTA_DSR)
400 icount->dsr++;
401 if (new_msr & MOS_MSR_DELTA_CD)
402 icount->dcd++;
403 if (new_msr & MOS_MSR_DELTA_RI)
404 icount->rng++;
405
406 wake_up_interruptible(&port->port->port.delta_msr_wait);
407 }
408 }
409
410 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
411 {
412 struct async_icount *icount;
413
414 if (new_lsr & SERIAL_LSR_BI) {
415
416
417
418
419
420 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
421 }
422
423
424 icount = &port->port->icount;
425 if (new_lsr & SERIAL_LSR_BI)
426 icount->brk++;
427 if (new_lsr & SERIAL_LSR_OE)
428 icount->overrun++;
429 if (new_lsr & SERIAL_LSR_PE)
430 icount->parity++;
431 if (new_lsr & SERIAL_LSR_FE)
432 icount->frame++;
433 }
434
435
436
437
438
439
440
441
442 static void mos7840_control_callback(struct urb *urb)
443 {
444 unsigned char *data;
445 struct moschip_port *mos7840_port;
446 struct device *dev = &urb->dev->dev;
447 __u8 regval = 0x0;
448 int status = urb->status;
449
450 mos7840_port = urb->context;
451
452 switch (status) {
453 case 0:
454
455 break;
456 case -ECONNRESET:
457 case -ENOENT:
458 case -ESHUTDOWN:
459
460 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
461 goto out;
462 default:
463 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
464 goto out;
465 }
466
467 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
468 if (urb->actual_length < 1)
469 goto out;
470
471 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
472 mos7840_port->MsrLsr, mos7840_port->port_num);
473 data = urb->transfer_buffer;
474 regval = (__u8) data[0];
475 dev_dbg(dev, "%s data is %x\n", __func__, regval);
476 if (mos7840_port->MsrLsr == 0)
477 mos7840_handle_new_msr(mos7840_port, regval);
478 else if (mos7840_port->MsrLsr == 1)
479 mos7840_handle_new_lsr(mos7840_port, regval);
480 out:
481 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
482 }
483
484 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
485 __u16 *val)
486 {
487 struct usb_device *dev = mcs->port->serial->dev;
488 struct usb_ctrlrequest *dr = mcs->dr;
489 unsigned char *buffer = mcs->ctrl_buf;
490 int ret;
491
492 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
493 return -EBUSY;
494
495 dr->bRequestType = MCS_RD_RTYPE;
496 dr->bRequest = MCS_RDREQ;
497 dr->wValue = cpu_to_le16(Wval);
498 dr->wIndex = cpu_to_le16(reg);
499 dr->wLength = cpu_to_le16(2);
500
501 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
502 (unsigned char *)dr, buffer, 2,
503 mos7840_control_callback, mcs);
504 mcs->control_urb->transfer_buffer_length = 2;
505 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
506 if (ret)
507 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
508
509 return ret;
510 }
511
512 static void mos7840_set_led_callback(struct urb *urb)
513 {
514 switch (urb->status) {
515 case 0:
516
517 break;
518 case -ECONNRESET:
519 case -ENOENT:
520 case -ESHUTDOWN:
521
522 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
523 __func__, urb->status);
524 break;
525 default:
526 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
527 __func__, urb->status);
528 }
529 }
530
531 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
532 __u16 reg)
533 {
534 struct usb_device *dev = mcs->port->serial->dev;
535 struct usb_ctrlrequest *dr = mcs->led_dr;
536
537 dr->bRequestType = MCS_WR_RTYPE;
538 dr->bRequest = MCS_WRREQ;
539 dr->wValue = cpu_to_le16(wval);
540 dr->wIndex = cpu_to_le16(reg);
541 dr->wLength = cpu_to_le16(0);
542
543 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
544 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
545
546 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
547 }
548
549 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
550 __u16 val)
551 {
552 struct usb_device *dev = port->serial->dev;
553
554 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
555 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
556 }
557
558 static void mos7840_led_off(struct timer_list *t)
559 {
560 struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
561
562
563 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
564 mod_timer(&mcs->led_timer2,
565 jiffies + msecs_to_jiffies(LED_OFF_MS));
566 }
567
568 static void mos7840_led_flag_off(struct timer_list *t)
569 {
570 struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
571
572 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
573 }
574
575 static void mos7840_led_activity(struct usb_serial_port *port)
576 {
577 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
578
579 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
580 return;
581
582 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
583 mod_timer(&mos7840_port->led_timer1,
584 jiffies + msecs_to_jiffies(LED_ON_MS));
585 }
586
587
588
589
590
591
592
593 static void mos7840_interrupt_callback(struct urb *urb)
594 {
595 int result;
596 int length;
597 struct moschip_port *mos7840_port;
598 struct usb_serial *serial;
599 __u16 Data;
600 unsigned char *data;
601 __u8 sp[5];
602 int i, rv = 0;
603 __u16 wval, wreg = 0;
604 int status = urb->status;
605
606 switch (status) {
607 case 0:
608
609 break;
610 case -ECONNRESET:
611 case -ENOENT:
612 case -ESHUTDOWN:
613
614 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
615 __func__, status);
616 return;
617 default:
618 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
619 __func__, status);
620 goto exit;
621 }
622
623 length = urb->actual_length;
624 data = urb->transfer_buffer;
625
626 serial = urb->context;
627
628
629
630
631
632
633
634
635 if (length > 5) {
636 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
637 return;
638 }
639
640 sp[0] = (__u8) data[0];
641 sp[1] = (__u8) data[1];
642 sp[2] = (__u8) data[2];
643 sp[3] = (__u8) data[3];
644
645 for (i = 0; i < serial->num_ports; i++) {
646 mos7840_port = mos7840_get_port_private(serial->port[i]);
647 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
648 if (mos7840_port->open) {
649 if (sp[i] & 0x01) {
650 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
651 } else {
652 switch (sp[i] & 0x0f) {
653 case SERIAL_IIR_RLS:
654 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
655 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
656 mos7840_port->MsrLsr = 1;
657 wreg = LINE_STATUS_REGISTER;
658 break;
659 case SERIAL_IIR_MS:
660 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
661 mos7840_port->MsrLsr = 0;
662 wreg = MODEM_STATUS_REGISTER;
663 break;
664 }
665 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
666 }
667 }
668 }
669 if (!(rv < 0))
670
671 return;
672 exit:
673 result = usb_submit_urb(urb, GFP_ATOMIC);
674 if (result) {
675 dev_err(&urb->dev->dev,
676 "%s - Error %d submitting interrupt urb\n",
677 __func__, result);
678 }
679 }
680
681 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
682 const char *function)
683 {
684 if (!port) {
685 pr_debug("%s - port == NULL\n", function);
686 return -1;
687 }
688 if (!port->serial) {
689 pr_debug("%s - port->serial == NULL\n", function);
690 return -1;
691 }
692
693 return 0;
694 }
695
696
697 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
698 const char *function)
699 {
700 if (!serial) {
701 pr_debug("%s - serial == NULL\n", function);
702 return -1;
703 }
704 if (!serial->type) {
705 pr_debug("%s - serial->type == NULL!\n", function);
706 return -1;
707 }
708
709 return 0;
710 }
711
712 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
713 const char *function)
714 {
715
716 if (!port ||
717 mos7840_port_paranoia_check(port, function) ||
718 mos7840_serial_paranoia_check(port->serial, function)) {
719
720
721 return NULL;
722 }
723
724 return port->serial;
725 }
726
727
728
729
730
731
732
733 static void mos7840_bulk_in_callback(struct urb *urb)
734 {
735 int retval;
736 unsigned char *data;
737 struct usb_serial *serial;
738 struct usb_serial_port *port;
739 struct moschip_port *mos7840_port;
740 int status = urb->status;
741
742 mos7840_port = urb->context;
743 if (!mos7840_port)
744 return;
745
746 if (status) {
747 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
748 mos7840_port->read_urb_busy = false;
749 return;
750 }
751
752 port = mos7840_port->port;
753 if (mos7840_port_paranoia_check(port, __func__)) {
754 mos7840_port->read_urb_busy = false;
755 return;
756 }
757
758 serial = mos7840_get_usb_serial(port, __func__);
759 if (!serial) {
760 mos7840_port->read_urb_busy = false;
761 return;
762 }
763
764 data = urb->transfer_buffer;
765 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
766
767 if (urb->actual_length) {
768 struct tty_port *tport = &mos7840_port->port->port;
769 tty_insert_flip_string(tport, data, urb->actual_length);
770 tty_flip_buffer_push(tport);
771 port->icount.rx += urb->actual_length;
772 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
773 }
774
775 if (!mos7840_port->read_urb) {
776 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
777 mos7840_port->read_urb_busy = false;
778 return;
779 }
780
781 if (mos7840_port->has_led)
782 mos7840_led_activity(port);
783
784 mos7840_port->read_urb_busy = true;
785 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
786
787 if (retval) {
788 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
789 mos7840_port->read_urb_busy = false;
790 }
791 }
792
793
794
795
796
797
798
799 static void mos7840_bulk_out_data_callback(struct urb *urb)
800 {
801 struct moschip_port *mos7840_port;
802 struct usb_serial_port *port;
803 int status = urb->status;
804 unsigned long flags;
805 int i;
806
807 mos7840_port = urb->context;
808 port = mos7840_port->port;
809 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
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_irqrestore(&mos7840_port->pool_lock, flags);
817
818 if (status) {
819 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
820 return;
821 }
822
823 if (mos7840_port_paranoia_check(port, __func__))
824 return;
825
826 if (mos7840_port->open)
827 tty_port_tty_wakeup(&port->port);
828
829 }
830
831
832
833
834
835
836
837
838
839
840
841
842 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
843 {
844 int response;
845 int j;
846 struct usb_serial *serial;
847 struct urb *urb;
848 __u16 Data;
849 int status;
850 struct moschip_port *mos7840_port;
851 struct moschip_port *port0;
852
853 if (mos7840_port_paranoia_check(port, __func__))
854 return -ENODEV;
855
856 serial = port->serial;
857
858 if (mos7840_serial_paranoia_check(serial, __func__))
859 return -ENODEV;
860
861 mos7840_port = mos7840_get_port_private(port);
862 port0 = mos7840_get_port_private(serial->port[0]);
863
864 if (mos7840_port == NULL || port0 == NULL)
865 return -ENODEV;
866
867 usb_clear_halt(serial->dev, port->write_urb->pipe);
868 usb_clear_halt(serial->dev, port->read_urb->pipe);
869 port0->open_ports++;
870
871
872 for (j = 0; j < NUM_URBS; ++j) {
873 urb = usb_alloc_urb(0, GFP_KERNEL);
874 mos7840_port->write_urb_pool[j] = urb;
875 if (!urb)
876 continue;
877
878 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
879 GFP_KERNEL);
880 if (!urb->transfer_buffer) {
881 usb_free_urb(urb);
882 mos7840_port->write_urb_pool[j] = NULL;
883 continue;
884 }
885 }
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901 Data = 0x0;
902 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
903 if (status < 0) {
904 dev_dbg(&port->dev, "Reading Spreg failed\n");
905 goto err;
906 }
907 Data |= 0x80;
908 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
909 if (status < 0) {
910 dev_dbg(&port->dev, "writing Spreg failed\n");
911 goto err;
912 }
913
914 Data &= ~0x80;
915 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
916 if (status < 0) {
917 dev_dbg(&port->dev, "writing Spreg failed\n");
918 goto err;
919 }
920
921
922 Data = 0x0;
923 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
924 &Data);
925 if (status < 0) {
926 dev_dbg(&port->dev, "Reading Controlreg failed\n");
927 goto err;
928 }
929 Data |= 0x08;
930 Data |= 0x20;
931 status = mos7840_set_reg_sync(port,
932 mos7840_port->ControlRegOffset, Data);
933 if (status < 0) {
934 dev_dbg(&port->dev, "writing Controlreg failed\n");
935 goto err;
936 }
937
938
939
940
941
942 Data = 0x00;
943 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
944 if (status < 0) {
945 dev_dbg(&port->dev, "disabling interrupts failed\n");
946 goto err;
947 }
948
949 Data = 0x00;
950 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
951 if (status < 0) {
952 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
953 goto err;
954 }
955
956 Data = 0xcf;
957 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
958 if (status < 0) {
959 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
960 goto err;
961 }
962
963 Data = 0x03;
964 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
965 mos7840_port->shadowLCR = Data;
966
967 Data = 0x0b;
968 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
969 mos7840_port->shadowMCR = Data;
970
971 Data = 0x00;
972 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
973 mos7840_port->shadowLCR = Data;
974
975 Data |= SERIAL_LCR_DLAB;
976 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
977
978 Data = 0x0c;
979 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
980
981 Data = 0x0;
982 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
983
984 Data = 0x00;
985 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
986
987 Data = Data & ~SERIAL_LCR_DLAB;
988 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
989 mos7840_port->shadowLCR = Data;
990
991
992 Data = 0x0;
993 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
994
995 Data = Data | 0x0c;
996 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
997
998 Data = Data & ~0x0c;
999 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1000
1001 Data = 0x0c;
1002 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1003
1004
1005 Data = 0x0;
1006 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1007 &Data);
1008 Data = Data & ~0x20;
1009 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1010 Data);
1011
1012
1013 Data = 0x0;
1014 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1015 &Data);
1016 Data = Data | 0x10;
1017 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1018 Data);
1019
1020
1021
1022
1023 if (port0->open_ports == 1) {
1024
1025 if (serial->port[0]->interrupt_in_buffer == NULL) {
1026
1027 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1028 serial->dev,
1029 usb_rcvintpipe(serial->dev,
1030 serial->port[0]->interrupt_in_endpointAddress),
1031 serial->port[0]->interrupt_in_buffer,
1032 serial->port[0]->interrupt_in_urb->
1033 transfer_buffer_length,
1034 mos7840_interrupt_callback,
1035 serial,
1036 serial->port[0]->interrupt_in_urb->interval);
1037
1038
1039 response =
1040 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1041 GFP_KERNEL);
1042 if (response) {
1043 dev_err(&port->dev, "%s - Error %d submitting "
1044 "interrupt urb\n", __func__, response);
1045 }
1046
1047 }
1048
1049 }
1050
1051
1052
1053
1054
1055 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1056 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
1057 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1058 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1059 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1060 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1061 mos7840_port->read_urb = port->read_urb;
1062
1063
1064 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1065 usb_fill_bulk_urb(mos7840_port->read_urb,
1066 serial->dev,
1067 usb_rcvbulkpipe(serial->dev,
1068 (port->bulk_in_endpointAddress) + 2),
1069 port->bulk_in_buffer,
1070 mos7840_port->read_urb->transfer_buffer_length,
1071 mos7840_bulk_in_callback, mos7840_port);
1072 } else {
1073 usb_fill_bulk_urb(mos7840_port->read_urb,
1074 serial->dev,
1075 usb_rcvbulkpipe(serial->dev,
1076 port->bulk_in_endpointAddress),
1077 port->bulk_in_buffer,
1078 mos7840_port->read_urb->transfer_buffer_length,
1079 mos7840_bulk_in_callback, mos7840_port);
1080 }
1081
1082 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1083 mos7840_port->read_urb_busy = true;
1084 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1085 if (response) {
1086 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1087 __func__, response);
1088 mos7840_port->read_urb_busy = false;
1089 }
1090
1091
1092
1093 mos7840_port->shadowMCR = MCR_MASTER_IE;
1094
1095 mos7840_port->open = 1;
1096
1097
1098 return 0;
1099 err:
1100 for (j = 0; j < NUM_URBS; ++j) {
1101 urb = mos7840_port->write_urb_pool[j];
1102 if (!urb)
1103 continue;
1104 kfree(urb->transfer_buffer);
1105 usb_free_urb(urb);
1106 }
1107 return status;
1108 }
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1121 {
1122 struct usb_serial_port *port = tty->driver_data;
1123 int i;
1124 int chars = 0;
1125 unsigned long flags;
1126 struct moschip_port *mos7840_port;
1127
1128 if (mos7840_port_paranoia_check(port, __func__))
1129 return 0;
1130
1131 mos7840_port = mos7840_get_port_private(port);
1132 if (mos7840_port == NULL)
1133 return 0;
1134
1135 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1136 for (i = 0; i < NUM_URBS; ++i) {
1137 if (mos7840_port->busy[i]) {
1138 struct urb *urb = mos7840_port->write_urb_pool[i];
1139 chars += urb->transfer_buffer_length;
1140 }
1141 }
1142 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1143 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1144 return chars;
1145
1146 }
1147
1148
1149
1150
1151
1152
1153 static void mos7840_close(struct usb_serial_port *port)
1154 {
1155 struct usb_serial *serial;
1156 struct moschip_port *mos7840_port;
1157 struct moschip_port *port0;
1158 int j;
1159 __u16 Data;
1160
1161 if (mos7840_port_paranoia_check(port, __func__))
1162 return;
1163
1164 serial = mos7840_get_usb_serial(port, __func__);
1165 if (!serial)
1166 return;
1167
1168 mos7840_port = mos7840_get_port_private(port);
1169 port0 = mos7840_get_port_private(serial->port[0]);
1170
1171 if (mos7840_port == NULL || port0 == NULL)
1172 return;
1173
1174 for (j = 0; j < NUM_URBS; ++j)
1175 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1176
1177
1178 for (j = 0; j < NUM_URBS; ++j) {
1179 if (mos7840_port->write_urb_pool[j]) {
1180 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
1181 usb_free_urb(mos7840_port->write_urb_pool[j]);
1182 }
1183 }
1184
1185 usb_kill_urb(mos7840_port->read_urb);
1186 mos7840_port->read_urb_busy = false;
1187
1188 port0->open_ports--;
1189 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
1190 if (port0->open_ports == 0) {
1191 if (serial->port[0]->interrupt_in_urb) {
1192 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1193 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1194 }
1195 }
1196
1197 Data = 0x0;
1198 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1199
1200 Data = 0x00;
1201 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1202
1203 mos7840_port->open = 0;
1204 }
1205
1206
1207
1208
1209
1210 static void mos7840_break(struct tty_struct *tty, int break_state)
1211 {
1212 struct usb_serial_port *port = tty->driver_data;
1213 unsigned char data;
1214 struct usb_serial *serial;
1215 struct moschip_port *mos7840_port;
1216
1217 if (mos7840_port_paranoia_check(port, __func__))
1218 return;
1219
1220 serial = mos7840_get_usb_serial(port, __func__);
1221 if (!serial)
1222 return;
1223
1224 mos7840_port = mos7840_get_port_private(port);
1225
1226 if (mos7840_port == NULL)
1227 return;
1228
1229 if (break_state == -1)
1230 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1231 else
1232 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1233
1234
1235 mos7840_port->shadowLCR = data;
1236 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1237 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1238 mos7840_port->shadowLCR);
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249 static int mos7840_write_room(struct tty_struct *tty)
1250 {
1251 struct usb_serial_port *port = tty->driver_data;
1252 int i;
1253 int room = 0;
1254 unsigned long flags;
1255 struct moschip_port *mos7840_port;
1256
1257 if (mos7840_port_paranoia_check(port, __func__))
1258 return -1;
1259
1260 mos7840_port = mos7840_get_port_private(port);
1261 if (mos7840_port == NULL)
1262 return -1;
1263
1264 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1265 for (i = 0; i < NUM_URBS; ++i) {
1266 if (!mos7840_port->busy[i])
1267 room += URB_TRANSFER_BUFFER_SIZE;
1268 }
1269 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1270
1271 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1272 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1273 return room;
1274
1275 }
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1286 const unsigned char *data, int count)
1287 {
1288 int status;
1289 int i;
1290 int bytes_sent = 0;
1291 int transfer_size;
1292 unsigned long flags;
1293
1294 struct moschip_port *mos7840_port;
1295 struct usb_serial *serial;
1296 struct urb *urb;
1297
1298 const unsigned char *current_position = data;
1299
1300 if (mos7840_port_paranoia_check(port, __func__))
1301 return -1;
1302
1303 serial = port->serial;
1304 if (mos7840_serial_paranoia_check(serial, __func__))
1305 return -1;
1306
1307 mos7840_port = mos7840_get_port_private(port);
1308 if (mos7840_port == NULL)
1309 return -1;
1310
1311
1312 urb = NULL;
1313
1314 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1315 for (i = 0; i < NUM_URBS; ++i) {
1316 if (!mos7840_port->busy[i]) {
1317 mos7840_port->busy[i] = 1;
1318 urb = mos7840_port->write_urb_pool[i];
1319 dev_dbg(&port->dev, "URB:%d\n", i);
1320 break;
1321 }
1322 }
1323 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1324
1325 if (urb == NULL) {
1326 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1327 goto exit;
1328 }
1329
1330 if (urb->transfer_buffer == NULL) {
1331 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1332 GFP_ATOMIC);
1333 if (!urb->transfer_buffer)
1334 goto exit;
1335 }
1336 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1337
1338 memcpy(urb->transfer_buffer, current_position, transfer_size);
1339
1340
1341 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1342 usb_fill_bulk_urb(urb,
1343 serial->dev,
1344 usb_sndbulkpipe(serial->dev,
1345 (port->bulk_out_endpointAddress) + 2),
1346 urb->transfer_buffer,
1347 transfer_size,
1348 mos7840_bulk_out_data_callback, mos7840_port);
1349 } else {
1350 usb_fill_bulk_urb(urb,
1351 serial->dev,
1352 usb_sndbulkpipe(serial->dev,
1353 port->bulk_out_endpointAddress),
1354 urb->transfer_buffer,
1355 transfer_size,
1356 mos7840_bulk_out_data_callback, mos7840_port);
1357 }
1358
1359 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1360
1361 if (mos7840_port->has_led)
1362 mos7840_led_activity(port);
1363
1364
1365 status = usb_submit_urb(urb, GFP_ATOMIC);
1366
1367 if (status) {
1368 mos7840_port->busy[i] = 0;
1369 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1370 "with status = %d\n", __func__, status);
1371 bytes_sent = status;
1372 goto exit;
1373 }
1374 bytes_sent = transfer_size;
1375 port->icount.tx += transfer_size;
1376 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
1377 exit:
1378 return bytes_sent;
1379
1380 }
1381
1382
1383
1384
1385
1386
1387
1388 static void mos7840_throttle(struct tty_struct *tty)
1389 {
1390 struct usb_serial_port *port = tty->driver_data;
1391 struct moschip_port *mos7840_port;
1392 int status;
1393
1394 if (mos7840_port_paranoia_check(port, __func__))
1395 return;
1396
1397 mos7840_port = mos7840_get_port_private(port);
1398
1399 if (mos7840_port == NULL)
1400 return;
1401
1402 if (!mos7840_port->open) {
1403 dev_dbg(&port->dev, "%s", "port not opened\n");
1404 return;
1405 }
1406
1407
1408 if (I_IXOFF(tty)) {
1409 unsigned char stop_char = STOP_CHAR(tty);
1410 status = mos7840_write(tty, port, &stop_char, 1);
1411 if (status <= 0)
1412 return;
1413 }
1414
1415 if (C_CRTSCTS(tty)) {
1416 mos7840_port->shadowMCR &= ~MCR_RTS;
1417 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1418 mos7840_port->shadowMCR);
1419 if (status < 0)
1420 return;
1421 }
1422 }
1423
1424
1425
1426
1427
1428
1429
1430 static void mos7840_unthrottle(struct tty_struct *tty)
1431 {
1432 struct usb_serial_port *port = tty->driver_data;
1433 int status;
1434 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1435
1436 if (mos7840_port_paranoia_check(port, __func__))
1437 return;
1438
1439 if (mos7840_port == NULL)
1440 return;
1441
1442 if (!mos7840_port->open) {
1443 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1444 return;
1445 }
1446
1447
1448 if (I_IXOFF(tty)) {
1449 unsigned char start_char = START_CHAR(tty);
1450 status = mos7840_write(tty, port, &start_char, 1);
1451 if (status <= 0)
1452 return;
1453 }
1454
1455
1456 if (C_CRTSCTS(tty)) {
1457 mos7840_port->shadowMCR |= MCR_RTS;
1458 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1459 mos7840_port->shadowMCR);
1460 if (status < 0)
1461 return;
1462 }
1463 }
1464
1465 static int mos7840_tiocmget(struct tty_struct *tty)
1466 {
1467 struct usb_serial_port *port = tty->driver_data;
1468 struct moschip_port *mos7840_port;
1469 unsigned int result;
1470 __u16 msr;
1471 __u16 mcr;
1472 int status;
1473 mos7840_port = mos7840_get_port_private(port);
1474
1475 if (mos7840_port == NULL)
1476 return -ENODEV;
1477
1478 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1479 if (status < 0)
1480 return -EIO;
1481 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1482 if (status < 0)
1483 return -EIO;
1484 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1485 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1486 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1487 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1488 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1489 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1490 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1491
1492 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1493
1494 return result;
1495 }
1496
1497 static int mos7840_tiocmset(struct tty_struct *tty,
1498 unsigned int set, unsigned int clear)
1499 {
1500 struct usb_serial_port *port = tty->driver_data;
1501 struct moschip_port *mos7840_port;
1502 unsigned int mcr;
1503 int status;
1504
1505 mos7840_port = mos7840_get_port_private(port);
1506
1507 if (mos7840_port == NULL)
1508 return -ENODEV;
1509
1510
1511 mcr = mos7840_port->shadowMCR;
1512 if (clear & TIOCM_RTS)
1513 mcr &= ~MCR_RTS;
1514 if (clear & TIOCM_DTR)
1515 mcr &= ~MCR_DTR;
1516 if (clear & TIOCM_LOOP)
1517 mcr &= ~MCR_LOOPBACK;
1518
1519 if (set & TIOCM_RTS)
1520 mcr |= MCR_RTS;
1521 if (set & TIOCM_DTR)
1522 mcr |= MCR_DTR;
1523 if (set & TIOCM_LOOP)
1524 mcr |= MCR_LOOPBACK;
1525
1526 mos7840_port->shadowMCR = mcr;
1527
1528 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1529 if (status < 0) {
1530 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1531 return status;
1532 }
1533
1534 return 0;
1535 }
1536
1537
1538
1539
1540
1541
1542 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1543 int baudRate, int *divisor,
1544 __u16 *clk_sel_val)
1545 {
1546 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1547
1548 if (baudRate <= 115200) {
1549 *divisor = 115200 / baudRate;
1550 *clk_sel_val = 0x0;
1551 }
1552 if ((baudRate > 115200) && (baudRate <= 230400)) {
1553 *divisor = 230400 / baudRate;
1554 *clk_sel_val = 0x10;
1555 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1556 *divisor = 403200 / baudRate;
1557 *clk_sel_val = 0x20;
1558 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1559 *divisor = 460800 / baudRate;
1560 *clk_sel_val = 0x30;
1561 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1562 *divisor = 806400 / baudRate;
1563 *clk_sel_val = 0x40;
1564 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1565 *divisor = 921600 / baudRate;
1566 *clk_sel_val = 0x50;
1567 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1568 *divisor = 1572864 / baudRate;
1569 *clk_sel_val = 0x60;
1570 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1571 *divisor = 3145728 / baudRate;
1572 *clk_sel_val = 0x70;
1573 }
1574 return 0;
1575 }
1576
1577
1578
1579
1580
1581
1582
1583 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1584 int baudRate)
1585 {
1586 int divisor = 0;
1587 int status;
1588 __u16 Data;
1589 __u16 clk_sel_val;
1590 struct usb_serial_port *port;
1591
1592 if (mos7840_port == NULL)
1593 return -1;
1594
1595 port = mos7840_port->port;
1596 if (mos7840_port_paranoia_check(port, __func__))
1597 return -1;
1598
1599 if (mos7840_serial_paranoia_check(port->serial, __func__))
1600 return -1;
1601
1602 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1603
1604 if (baudRate > 115200) {
1605 #ifdef HW_flow_control
1606
1607
1608 Data = 0x2b;
1609 mos7840_port->shadowMCR = Data;
1610 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1611 Data);
1612 if (status < 0) {
1613 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1614 return -1;
1615 }
1616 #endif
1617
1618 } else {
1619 #ifdef HW_flow_control
1620
1621 Data = 0xb;
1622 mos7840_port->shadowMCR = Data;
1623 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1624 Data);
1625 if (status < 0) {
1626 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1627 return -1;
1628 }
1629 #endif
1630
1631 }
1632
1633 if (1) {
1634 clk_sel_val = 0x0;
1635 Data = 0x0;
1636 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1637 &clk_sel_val);
1638 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1639 &Data);
1640 if (status < 0) {
1641 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1642 return -1;
1643 }
1644 Data = (Data & 0x8f) | clk_sel_val;
1645 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1646 Data);
1647 if (status < 0) {
1648 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1649 return -1;
1650 }
1651
1652
1653 if (status) {
1654 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1655 return status;
1656 }
1657
1658 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1659 mos7840_port->shadowLCR = Data;
1660 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1661
1662
1663 Data = (unsigned char)(divisor & 0xff);
1664 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1665 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1666
1667 Data = (unsigned char)((divisor & 0xff00) >> 8);
1668 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1669 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1670
1671
1672 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1673 mos7840_port->shadowLCR = Data;
1674 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1675
1676 }
1677 return status;
1678 }
1679
1680
1681
1682
1683
1684
1685
1686 static void mos7840_change_port_settings(struct tty_struct *tty,
1687 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1688 {
1689 int baud;
1690 unsigned cflag;
1691 __u8 lData;
1692 __u8 lParity;
1693 __u8 lStop;
1694 int status;
1695 __u16 Data;
1696 struct usb_serial_port *port;
1697
1698 if (mos7840_port == NULL)
1699 return;
1700
1701 port = mos7840_port->port;
1702
1703 if (mos7840_port_paranoia_check(port, __func__))
1704 return;
1705
1706 if (mos7840_serial_paranoia_check(port->serial, __func__))
1707 return;
1708
1709 if (!mos7840_port->open) {
1710 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1711 return;
1712 }
1713
1714 lData = LCR_BITS_8;
1715 lStop = LCR_STOP_1;
1716 lParity = LCR_PAR_NONE;
1717
1718 cflag = tty->termios.c_cflag;
1719
1720
1721 switch (cflag & CSIZE) {
1722 case CS5:
1723 lData = LCR_BITS_5;
1724 break;
1725
1726 case CS6:
1727 lData = LCR_BITS_6;
1728 break;
1729
1730 case CS7:
1731 lData = LCR_BITS_7;
1732 break;
1733
1734 default:
1735 case CS8:
1736 lData = LCR_BITS_8;
1737 break;
1738 }
1739
1740
1741 if (cflag & PARENB) {
1742 if (cflag & PARODD) {
1743 lParity = LCR_PAR_ODD;
1744 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1745 } else {
1746 lParity = LCR_PAR_EVEN;
1747 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1748 }
1749
1750 } else {
1751 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1752 }
1753
1754 if (cflag & CMSPAR)
1755 lParity = lParity | 0x20;
1756
1757
1758 if (cflag & CSTOPB) {
1759 lStop = LCR_STOP_2;
1760 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1761 } else {
1762 lStop = LCR_STOP_1;
1763 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1764 }
1765
1766
1767 mos7840_port->shadowLCR &=
1768 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1769 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1770
1771 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1772 mos7840_port->shadowLCR);
1773
1774 Data = 0x00;
1775 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1776
1777 Data = 0x00;
1778 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1779
1780 Data = 0xcf;
1781 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1782
1783
1784 Data = mos7840_port->shadowLCR;
1785
1786 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1787
1788 Data = 0x00b;
1789 mos7840_port->shadowMCR = Data;
1790 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1791 Data = 0x00b;
1792 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1793
1794
1795
1796 mos7840_port->shadowMCR = MCR_MASTER_IE;
1797 if (cflag & CBAUD)
1798 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1799
1800 if (cflag & CRTSCTS)
1801 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1802 else
1803 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1804
1805 Data = mos7840_port->shadowMCR;
1806 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1807
1808
1809 baud = tty_get_baud_rate(tty);
1810
1811 if (!baud) {
1812
1813 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1814 baud = 9600;
1815 }
1816
1817 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1818 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1819
1820
1821 Data = 0x0c;
1822 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1823
1824 if (!mos7840_port->read_urb_busy) {
1825 mos7840_port->read_urb_busy = true;
1826 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1827 if (status) {
1828 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1829 status);
1830 mos7840_port->read_urb_busy = false;
1831 }
1832 }
1833 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1834 mos7840_port->shadowLCR);
1835 }
1836
1837
1838
1839
1840
1841
1842
1843 static void mos7840_set_termios(struct tty_struct *tty,
1844 struct usb_serial_port *port,
1845 struct ktermios *old_termios)
1846 {
1847 int status;
1848 struct usb_serial *serial;
1849 struct moschip_port *mos7840_port;
1850
1851 if (mos7840_port_paranoia_check(port, __func__))
1852 return;
1853
1854 serial = port->serial;
1855
1856 if (mos7840_serial_paranoia_check(serial, __func__))
1857 return;
1858
1859 mos7840_port = mos7840_get_port_private(port);
1860
1861 if (mos7840_port == NULL)
1862 return;
1863
1864 if (!mos7840_port->open) {
1865 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1866 return;
1867 }
1868
1869
1870
1871 mos7840_change_port_settings(tty, mos7840_port, old_termios);
1872
1873 if (!mos7840_port->read_urb) {
1874 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
1875 return;
1876 }
1877
1878 if (!mos7840_port->read_urb_busy) {
1879 mos7840_port->read_urb_busy = true;
1880 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1881 if (status) {
1882 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1883 status);
1884 mos7840_port->read_urb_busy = false;
1885 }
1886 }
1887 }
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900 static int mos7840_get_lsr_info(struct tty_struct *tty,
1901 unsigned int __user *value)
1902 {
1903 int count;
1904 unsigned int result = 0;
1905
1906 count = mos7840_chars_in_buffer(tty);
1907 if (count == 0)
1908 result = TIOCSER_TEMT;
1909
1910 if (copy_to_user(value, &result, sizeof(int)))
1911 return -EFAULT;
1912 return 0;
1913 }
1914
1915
1916
1917
1918
1919
1920 static int mos7840_get_serial_info(struct tty_struct *tty,
1921 struct serial_struct *ss)
1922 {
1923 struct usb_serial_port *port = tty->driver_data;
1924 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1925
1926 ss->type = PORT_16550A;
1927 ss->line = mos7840_port->port->minor;
1928 ss->port = mos7840_port->port->port_number;
1929 ss->irq = 0;
1930 ss->xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1931 ss->baud_base = 9600;
1932 ss->close_delay = 5 * HZ;
1933 ss->closing_wait = 30 * HZ;
1934 return 0;
1935 }
1936
1937
1938
1939
1940
1941
1942 static int mos7840_ioctl(struct tty_struct *tty,
1943 unsigned int cmd, unsigned long arg)
1944 {
1945 struct usb_serial_port *port = tty->driver_data;
1946 void __user *argp = (void __user *)arg;
1947 struct moschip_port *mos7840_port;
1948
1949 if (mos7840_port_paranoia_check(port, __func__))
1950 return -1;
1951
1952 mos7840_port = mos7840_get_port_private(port);
1953
1954 if (mos7840_port == NULL)
1955 return -1;
1956
1957 switch (cmd) {
1958
1959
1960 case TIOCSERGETLSR:
1961 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1962 return mos7840_get_lsr_info(tty, argp);
1963
1964 default:
1965 break;
1966 }
1967 return -ENOIOCTLCMD;
1968 }
1969
1970 static int mos7810_check(struct usb_serial *serial)
1971 {
1972 int i, pass_count = 0;
1973 u8 *buf;
1974 __u16 data = 0, mcr_data = 0;
1975 __u16 test_pattern = 0x55AA;
1976 int res;
1977
1978 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
1979 if (!buf)
1980 return 0;
1981
1982
1983 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1984 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
1985 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1986 if (res == VENDOR_READ_LENGTH)
1987 mcr_data = *buf;
1988
1989 for (i = 0; i < 16; i++) {
1990
1991 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1992 MCS_WRREQ, MCS_WR_RTYPE,
1993 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
1994 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
1995
1996
1997 res = usb_control_msg(serial->dev,
1998 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
1999 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2000 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2001 if (res == VENDOR_READ_LENGTH)
2002 data = *buf;
2003
2004
2005 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2006 break;
2007
2008 pass_count++;
2009 }
2010
2011
2012 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2013 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2014 0, MOS_WDR_TIMEOUT);
2015
2016 kfree(buf);
2017
2018 if (pass_count == 16)
2019 return 1;
2020
2021 return 0;
2022 }
2023
2024 static int mos7840_probe(struct usb_serial *serial,
2025 const struct usb_device_id *id)
2026 {
2027 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2028 u16 vid = le16_to_cpu(serial->dev->descriptor.idVendor);
2029 u8 *buf;
2030 int device_type;
2031
2032 if (product == MOSCHIP_DEVICE_ID_7810 ||
2033 product == MOSCHIP_DEVICE_ID_7820 ||
2034 product == MOSCHIP_DEVICE_ID_7843) {
2035 device_type = product;
2036 goto out;
2037 }
2038
2039 if (vid == USB_VENDOR_ID_MOXA && product == MOXA_DEVICE_ID_2210) {
2040 device_type = MOSCHIP_DEVICE_ID_7820;
2041 goto out;
2042 }
2043
2044 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2045 if (!buf)
2046 return -ENOMEM;
2047
2048 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2049 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2050 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2051
2052
2053 if (buf[0] & 0x01)
2054 device_type = MOSCHIP_DEVICE_ID_7840;
2055 else if (mos7810_check(serial))
2056 device_type = MOSCHIP_DEVICE_ID_7810;
2057 else
2058 device_type = MOSCHIP_DEVICE_ID_7820;
2059
2060 kfree(buf);
2061 out:
2062 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
2063
2064 return 0;
2065 }
2066
2067 static int mos7840_calc_num_ports(struct usb_serial *serial,
2068 struct usb_serial_endpoints *epds)
2069 {
2070 int device_type = (unsigned long)usb_get_serial_data(serial);
2071 int num_ports;
2072
2073 if (device_type == MOSCHIP_DEVICE_ID_7843)
2074 num_ports = 3;
2075 else
2076 num_ports = (device_type >> 4) & 0x000F;
2077
2078
2079
2080
2081
2082 if (num_ports == 0)
2083 return -ENODEV;
2084
2085 if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
2086 dev_err(&serial->interface->dev, "missing endpoints\n");
2087 return -ENODEV;
2088 }
2089
2090 return num_ports;
2091 }
2092
2093 static int mos7840_port_probe(struct usb_serial_port *port)
2094 {
2095 struct usb_serial *serial = port->serial;
2096 int device_type = (unsigned long)usb_get_serial_data(serial);
2097 struct moschip_port *mos7840_port;
2098 int status;
2099 int pnum;
2100 __u16 Data;
2101
2102
2103
2104
2105 pnum = port->port_number;
2106
2107 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2108 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2109 if (!mos7840_port)
2110 return -ENOMEM;
2111
2112
2113
2114
2115
2116 mos7840_port->port = port;
2117 mos7840_set_port_private(port, mos7840_port);
2118 spin_lock_init(&mos7840_port->pool_lock);
2119
2120
2121
2122
2123 mos7840_port->port_num = pnum + 1;
2124 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
2125 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2126
2127 if (mos7840_port->port_num == 1) {
2128 mos7840_port->SpRegOffset = 0x0;
2129 mos7840_port->ControlRegOffset = 0x1;
2130 mos7840_port->DcrRegOffset = 0x4;
2131 } else {
2132 u8 phy_num = mos7840_port->port_num;
2133
2134
2135 if (serial->num_ports == 2)
2136 phy_num = 3;
2137
2138 mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
2139 mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
2140 mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
2141 }
2142 mos7840_dump_serial_port(port, mos7840_port);
2143 mos7840_set_port_private(port, mos7840_port);
2144
2145
2146 status = mos7840_get_reg_sync(port,
2147 mos7840_port->ControlRegOffset, &Data);
2148 if (status < 0) {
2149 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2150 goto out;
2151 } else
2152 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2153 Data |= 0x08;
2154 Data |= 0x04;
2155
2156
2157
2158 status = mos7840_set_reg_sync(port,
2159 mos7840_port->ControlRegOffset, Data);
2160 if (status < 0) {
2161 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2162 goto out;
2163 } else
2164 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2165
2166
2167
2168 Data = 0x01;
2169 status = mos7840_set_reg_sync(port,
2170 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2171 if (status < 0) {
2172 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2173 goto out;
2174 } else
2175 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2176
2177 Data = 0x05;
2178 status = mos7840_set_reg_sync(port,
2179 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2180 if (status < 0) {
2181 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2182 goto out;
2183 } else
2184 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2185
2186 Data = 0x24;
2187 status = mos7840_set_reg_sync(port,
2188 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2189 if (status < 0) {
2190 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2191 goto out;
2192 } else
2193 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2194
2195
2196 Data = 0x0;
2197 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2198 if (status < 0) {
2199 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2200 goto out;
2201 } else
2202 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2203
2204 Data = 0x20;
2205 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2206 if (status < 0) {
2207 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2208 goto error;
2209 } else
2210 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2211
2212
2213 Data = 0x00;
2214 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2215 if (status < 0) {
2216 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2217 goto out;
2218 } else
2219 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2220
2221
2222 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2223 Data = 0xff;
2224 status = mos7840_set_reg_sync(port,
2225 (__u16) (ZLP_REG1 +
2226 ((__u16)mos7840_port->port_num)), Data);
2227 dev_dbg(&port->dev, "ZLIP offset %x\n",
2228 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2229 if (status < 0) {
2230 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2231 goto out;
2232 } else
2233 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2234 } else {
2235 Data = 0xff;
2236 status = mos7840_set_reg_sync(port,
2237 (__u16) (ZLP_REG1 +
2238 ((__u16)mos7840_port->port_num) - 0x1), Data);
2239 dev_dbg(&port->dev, "ZLIP offset %x\n",
2240 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2241 if (status < 0) {
2242 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2243 goto out;
2244 } else
2245 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2246
2247 }
2248 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2249 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2250 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2251 GFP_KERNEL);
2252 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2253 !mos7840_port->dr) {
2254 status = -ENOMEM;
2255 goto error;
2256 }
2257
2258 mos7840_port->has_led = false;
2259
2260
2261 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2262 mos7840_port->has_led = true;
2263
2264 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2265 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2266 GFP_KERNEL);
2267 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2268 status = -ENOMEM;
2269 goto error;
2270 }
2271
2272 timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
2273 mos7840_port->led_timer1.expires =
2274 jiffies + msecs_to_jiffies(LED_ON_MS);
2275 timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
2276 0);
2277 mos7840_port->led_timer2.expires =
2278 jiffies + msecs_to_jiffies(LED_OFF_MS);
2279
2280
2281 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2282 }
2283 out:
2284 if (pnum == serial->num_ports - 1) {
2285
2286 Data = 0x0f;
2287 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2288 if (status < 0) {
2289 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2290 goto error;
2291 } else
2292 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2293 }
2294 return 0;
2295 error:
2296 kfree(mos7840_port->led_dr);
2297 usb_free_urb(mos7840_port->led_urb);
2298 kfree(mos7840_port->dr);
2299 kfree(mos7840_port->ctrl_buf);
2300 usb_free_urb(mos7840_port->control_urb);
2301 kfree(mos7840_port);
2302
2303 return status;
2304 }
2305
2306 static int mos7840_port_remove(struct usb_serial_port *port)
2307 {
2308 struct moschip_port *mos7840_port;
2309
2310 mos7840_port = mos7840_get_port_private(port);
2311
2312 if (mos7840_port->has_led) {
2313
2314 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2315
2316 del_timer_sync(&mos7840_port->led_timer1);
2317 del_timer_sync(&mos7840_port->led_timer2);
2318
2319 usb_kill_urb(mos7840_port->led_urb);
2320 usb_free_urb(mos7840_port->led_urb);
2321 kfree(mos7840_port->led_dr);
2322 }
2323 usb_kill_urb(mos7840_port->control_urb);
2324 usb_free_urb(mos7840_port->control_urb);
2325 kfree(mos7840_port->ctrl_buf);
2326 kfree(mos7840_port->dr);
2327 kfree(mos7840_port);
2328
2329 return 0;
2330 }
2331
2332 static struct usb_serial_driver moschip7840_4port_device = {
2333 .driver = {
2334 .owner = THIS_MODULE,
2335 .name = "mos7840",
2336 },
2337 .description = DRIVER_DESC,
2338 .id_table = id_table,
2339 .num_interrupt_in = 1,
2340 .open = mos7840_open,
2341 .close = mos7840_close,
2342 .write = mos7840_write,
2343 .write_room = mos7840_write_room,
2344 .chars_in_buffer = mos7840_chars_in_buffer,
2345 .throttle = mos7840_throttle,
2346 .unthrottle = mos7840_unthrottle,
2347 .calc_num_ports = mos7840_calc_num_ports,
2348 .probe = mos7840_probe,
2349 .ioctl = mos7840_ioctl,
2350 .get_serial = mos7840_get_serial_info,
2351 .set_termios = mos7840_set_termios,
2352 .break_ctl = mos7840_break,
2353 .tiocmget = mos7840_tiocmget,
2354 .tiocmset = mos7840_tiocmset,
2355 .tiocmiwait = usb_serial_generic_tiocmiwait,
2356 .get_icount = usb_serial_generic_get_icount,
2357 .port_probe = mos7840_port_probe,
2358 .port_remove = mos7840_port_remove,
2359 .read_bulk_callback = mos7840_bulk_in_callback,
2360 .read_int_callback = mos7840_interrupt_callback,
2361 };
2362
2363 static struct usb_serial_driver * const serial_drivers[] = {
2364 &moschip7840_4port_device, NULL
2365 };
2366
2367 module_usb_serial_driver(serial_drivers, id_table);
2368
2369 MODULE_DESCRIPTION(DRIVER_DESC);
2370 MODULE_LICENSE("GPL");