blob: da60c300b846ddc1230de250a236d1be19c87d5e [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
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#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
36#include <asm/uaccess.h>
37
38
39/*
40 * Version Information
41 */
42#define DRIVER_VERSION "1.0.0.4F"
43#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44#define DRIVER_DESC "Moschip USB Serial Driver"
45
46/* default urb timeout */
47#define MOS_WDR_TIMEOUT (HZ * 5)
48
49#define MOS_PORT1 0x0200
50#define MOS_PORT2 0x0300
51#define MOS_VENREG 0x0000
52#define MOS_MAX_PORT 0x02
53#define MOS_WRITE 0x0E
54#define MOS_READ 0x0D
55
56/* Interrupt Rotinue Defines */
57#define SERIAL_IIR_RLS 0x06
58#define SERIAL_IIR_RDA 0x04
59#define SERIAL_IIR_CTI 0x0c
60#define SERIAL_IIR_THR 0x02
61#define SERIAL_IIR_MS 0x00
62
63#define NUM_URBS 16 /* URB Count */
64#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
65
66/* This structure holds all of the local port information */
67struct moschip_port
68{
69 __u8 shadowLCR; /* last LCR value received */
70 __u8 shadowMCR; /* last MCR value received */
71 __u8 shadowMSR; /* last MSR value received */
72 char open;
73 struct async_icount icount;
74 struct usb_serial_port *port; /* loop back to the owner */
75 struct urb *write_urb_pool[NUM_URBS];
76};
77
78/* This structure holds all of the individual serial device information */
79struct moschip_serial
80{
81 int interrupt_started;
82};
83
84static int debug;
85
86#define USB_VENDOR_ID_MOSCHIP 0x9710
87#define MOSCHIP_DEVICE_ID_7720 0x7720
88#define MOSCHIP_DEVICE_ID_7715 0x7715
89
90static struct usb_device_id moschip_port_id_table [] = {
91 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
92 { } /* terminating entry */
93};
94MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
95
96
97/*
98 * mos7720_interrupt_callback
99 * this is the callback function for when we have received data on the
100 * interrupt endpoint.
101 */
102static void mos7720_interrupt_callback(struct urb *urb)
103{
104 int result;
105 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700106 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100107 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700108 __u8 sp1;
109 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700110
111 dbg("%s"," : Entering\n");
112
113 if (!urb) {
114 dbg("%s","Invalid Pointer !!!!:\n");
115 return;
116 }
117
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700118 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700119 case 0:
120 /* success */
121 break;
122 case -ECONNRESET:
123 case -ENOENT:
124 case -ESHUTDOWN:
125 /* this urb is terminated, clean up */
126 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700127 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700128 return;
129 default:
130 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700131 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700132 goto exit;
133 }
134
135 length = urb->actual_length;
136 data = urb->transfer_buffer;
137
138 /* Moschip get 4 bytes
139 * Byte 1 IIR Port 1 (port.number is 0)
140 * Byte 2 IIR Port 2 (port.number is 1)
141 * Byte 3 --------------
142 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100143
144 /* the above description is inverted
145 * oneukum 2007-03-14 */
146
147 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700148 dbg("Wrong data !!!");
149 return;
150 }
151
Oliver Neukum325b70c2007-03-19 13:58:29 +0100152 sp1 = data[3];
153 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700154
Oliver Neukum325b70c2007-03-19 13:58:29 +0100155 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700156 /* No Interrupt Pending in both the ports */
157 dbg("No Interrupt !!!");
158 } else {
159 switch (sp1 & 0x0f) {
160 case SERIAL_IIR_RLS:
161 dbg("Serial Port 1: Receiver status error or address "
162 "bit detected in 9-bit mode\n");
163 break;
164 case SERIAL_IIR_CTI:
165 dbg("Serial Port 1: Receiver time out");
166 break;
167 case SERIAL_IIR_MS:
168 dbg("Serial Port 1: Modem status change");
169 break;
170 }
171
172 switch (sp2 & 0x0f) {
173 case SERIAL_IIR_RLS:
174 dbg("Serial Port 2: Receiver status error or address "
175 "bit detected in 9-bit mode");
176 break;
177 case SERIAL_IIR_CTI:
178 dbg("Serial Port 2: Receiver time out");
179 break;
180 case SERIAL_IIR_MS:
181 dbg("Serial Port 2: Modem status change");
182 break;
183 }
184 }
185
186exit:
187 result = usb_submit_urb(urb, GFP_ATOMIC);
188 if (result)
189 dev_err(&urb->dev->dev,
190 "%s - Error %d submitting control urb\n",
191 __FUNCTION__, result);
192 return;
193}
194
195/*
196 * mos7720_bulk_in_callback
197 * this is the callback function for when we have received data on the
198 * bulk in endpoint.
199 */
200static void mos7720_bulk_in_callback(struct urb *urb)
201{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700202 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700203 unsigned char *data ;
204 struct usb_serial_port *port;
205 struct moschip_port *mos7720_port;
206 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700207 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700208
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700209 if (status) {
210 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700211 return;
212 }
213
214 mos7720_port = urb->context;
215 if (!mos7720_port) {
216 dbg("%s","NULL mos7720_port pointer \n");
217 return ;
218 }
219
220 port = mos7720_port->port;
221
222 dbg("Entering...%s", __FUNCTION__);
223
224 data = urb->transfer_buffer;
225
226 tty = port->tty;
227 if (tty && urb->actual_length) {
228 tty_buffer_request_room(tty, urb->actual_length);
229 tty_insert_flip_string(tty, data, urb->actual_length);
230 tty_flip_buffer_push(tty);
231 }
232
233 if (!port->read_urb) {
234 dbg("URB KILLED !!!");
235 return;
236 }
237
238 if (port->read_urb->status != -EINPROGRESS) {
239 port->read_urb->dev = port->serial->dev;
240
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700241 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
242 if (retval)
243 dbg("usb_submit_urb(read bulk) failed, retval = %d",
244 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700245 }
246}
247
248/*
249 * mos7720_bulk_out_data_callback
250 * this is the callback function for when we have finished sending serial
251 * data on the bulk out endpoint.
252 */
253static void mos7720_bulk_out_data_callback(struct urb *urb)
254{
255 struct moschip_port *mos7720_port;
256 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700257 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700258
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700259 if (status) {
260 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700261 return;
262 }
263
264 mos7720_port = urb->context;
265 if (!mos7720_port) {
266 dbg("NULL mos7720_port pointer");
267 return ;
268 }
269
270 dbg("Entering .........");
271
272 tty = mos7720_port->port->tty;
273
Jiri Slabyb963a842007-02-10 01:44:55 -0800274 if (tty && mos7720_port->open)
275 tty_wakeup(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700276}
277
278/*
279 * send_mos_cmd
280 * this function will be used for sending command to device
281 */
282static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
283 __u16 index, void *data)
284{
285 int status;
286 unsigned int pipe;
287 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
288 __u8 requesttype;
289 __u16 size = 0x0000;
290
291 if (value < MOS_MAX_PORT) {
292 if (product == MOSCHIP_DEVICE_ID_7715) {
293 value = value*0x100+0x100;
294 } else {
295 value = value*0x100+0x200;
296 }
297 } else {
298 value = 0x0000;
299 if ((product == MOSCHIP_DEVICE_ID_7715) &&
300 (index != 0x08)) {
301 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
302 //index = 0x01 ;
303 }
304 }
305
306 if (request == MOS_WRITE) {
307 request = (__u8)MOS_WRITE;
308 requesttype = (__u8)0x40;
309 value = value + (__u16)*((unsigned char *)data);
310 data = NULL;
311 pipe = usb_sndctrlpipe(serial->dev, 0);
312 } else {
313 request = (__u8)MOS_READ;
314 requesttype = (__u8)0xC0;
315 size = 0x01;
316 pipe = usb_rcvctrlpipe(serial->dev,0);
317 }
318
319 status = usb_control_msg(serial->dev, pipe, request, requesttype,
320 value, index, data, size, MOS_WDR_TIMEOUT);
321
322 if (status < 0)
323 dbg("Command Write failed Value %x index %x\n",value,index);
324
325 return status;
326}
327
328static int mos7720_open(struct usb_serial_port *port, struct file * filp)
329{
330 struct usb_serial *serial;
331 struct usb_serial_port *port0;
332 struct urb *urb;
333 struct moschip_serial *mos7720_serial;
334 struct moschip_port *mos7720_port;
335 int response;
336 int port_number;
337 char data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100338 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700339 int j;
340
341 serial = port->serial;
342
343 mos7720_port = usb_get_serial_port_data(port);
344 if (mos7720_port == NULL)
345 return -ENODEV;
346
347 port0 = serial->port[0];
348
349 mos7720_serial = usb_get_serial_data(serial);
350
351 if (mos7720_serial == NULL || port0 == NULL)
352 return -ENODEV;
353
354 usb_clear_halt(serial->dev, port->write_urb->pipe);
355 usb_clear_halt(serial->dev, port->read_urb->pipe);
356
357 /* Initialising the write urb pool */
358 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukumc2cf3f62007-03-06 16:21:22 +0100359 urb = usb_alloc_urb(0,GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700360 mos7720_port->write_urb_pool[j] = urb;
361
362 if (urb == NULL) {
363 err("No more urbs???");
364 continue;
365 }
366
367 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
368 GFP_KERNEL);
369 if (!urb->transfer_buffer) {
370 err("%s-out of memory for urb buffers.", __FUNCTION__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100371 usb_free_urb(mos7720_port->write_urb_pool[j]);
372 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700373 continue;
374 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100375 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700376 }
377
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100378 if (!allocated_urbs)
379 return -ENOMEM;
380
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700381 /* Initialize MCS7720 -- Write Init values to corresponding Registers
382 *
383 * Register Index
384 * 1 : IER
385 * 2 : FCR
386 * 3 : LCR
387 * 4 : MCR
388 *
389 * 0x08 : SP1/2 Control Reg
390 */
391 port_number = port->number - port->serial->minor;
392 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
393 dbg("SS::%p LSR:%x\n",mos7720_port, data);
394
395 dbg("Check:Sending Command ..........");
396
397 data = 0x02;
398 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
399 data = 0x02;
400 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
401
402 data = 0x00;
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
404 data = 0x00;
405 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
406
407 data = 0xCF;
408 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
409 data = 0x03;
410 mos7720_port->shadowLCR = data;
411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
412 data = 0x0b;
413 mos7720_port->shadowMCR = data;
414 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
415 data = 0x0b;
416 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
417
418 data = 0x00;
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
420 data = 0x00;
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
422
423/* data = 0x00;
424 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
425 data = 0x03;
426 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
427 data = 0x00;
428 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
429*/
430 data = 0x00;
431 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
432
433 data = data | (port->number - port->serial->minor + 1);
434 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
435
436 data = 0x83;
437 mos7720_port->shadowLCR = data;
438 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
439 data = 0x0c;
440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
441 data = 0x00;
442 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
443 data = 0x03;
444 mos7720_port->shadowLCR = data;
445 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
446 data = 0x0c;
447 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
448 data = 0x0c;
449 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
450
451//Matrix
452
453 /* force low_latency on so that our tty_push actually forces *
454 * the data through,otherwise it is scheduled, and with *
455 * high data rates (like with OHCI) data can get lost. */
456
457 if (port->tty)
458 port->tty->low_latency = 1;
459
460 /* see if we've set up our endpoint info yet *
461 * (can't set it up in mos7720_startup as the *
462 * structures were not set up at that time.) */
463 if (!mos7720_serial->interrupt_started) {
464 dbg("Interrupt buffer NULL !!!");
465
466 /* not set up yet, so do it now */
467 mos7720_serial->interrupt_started = 1;
468
469 dbg("To Submit URB !!!");
470
471 /* set up our interrupt urb */
472 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
473 usb_rcvintpipe(serial->dev,
474 port->interrupt_in_endpointAddress),
475 port0->interrupt_in_buffer,
476 port0->interrupt_in_urb->transfer_buffer_length,
477 mos7720_interrupt_callback, mos7720_port,
478 port0->interrupt_in_urb->interval);
479
480 /* start interrupt read for this mos7720 this interrupt *
481 * will continue as long as the mos7720 is connected */
482 dbg("Submit URB over !!!");
483 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
484 if (response)
485 dev_err(&port->dev,
486 "%s - Error %d submitting control urb",
487 __FUNCTION__, response);
488 }
489
490 /* set up our bulk in urb */
491 usb_fill_bulk_urb(port->read_urb, serial->dev,
492 usb_rcvbulkpipe(serial->dev,
493 port->bulk_in_endpointAddress),
494 port->bulk_in_buffer,
495 port->read_urb->transfer_buffer_length,
496 mos7720_bulk_in_callback, mos7720_port);
497 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
498 if (response)
499 dev_err(&port->dev,
500 "%s - Error %d submitting read urb", __FUNCTION__, response);
501
502 /* initialize our icount structure */
503 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
504
505 /* initialize our port settings */
506 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
507
508 /* send a open port command */
509 mos7720_port->open = 1;
510
511 return 0;
512}
513
514/*
515 * mos7720_chars_in_buffer
516 * this function is called by the tty driver when it wants to know how many
517 * bytes of data we currently have outstanding in the port (data that has
518 * been written, but hasn't made it out the port yet)
519 * If successful, we return the number of bytes left to be written in the
520 * system,
521 * Otherwise we return a negative error number.
522 */
523static int mos7720_chars_in_buffer(struct usb_serial_port *port)
524{
525 int i;
526 int chars = 0;
527 struct moschip_port *mos7720_port;
528
529 dbg("%s:entering ...........", __FUNCTION__);
530
531 mos7720_port = usb_get_serial_port_data(port);
532 if (mos7720_port == NULL) {
533 dbg("%s:leaving ...........", __FUNCTION__);
534 return -ENODEV;
535 }
536
537 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100538 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700539 chars += URB_TRANSFER_BUFFER_SIZE;
540 }
541 dbg("%s - returns %d", __FUNCTION__, chars);
542 return chars;
543}
544
545static void mos7720_close(struct usb_serial_port *port, struct file *filp)
546{
547 struct usb_serial *serial;
548 struct moschip_port *mos7720_port;
549 char data;
550 int j;
551
552 dbg("mos7720_close:entering...");
553
554 serial = port->serial;
555
556 mos7720_port = usb_get_serial_port_data(port);
557 if (mos7720_port == NULL)
558 return;
559
560 for (j = 0; j < NUM_URBS; ++j)
561 usb_kill_urb(mos7720_port->write_urb_pool[j]);
562
563 /* Freeing Write URBs */
564 for (j = 0; j < NUM_URBS; ++j) {
565 if (mos7720_port->write_urb_pool[j]) {
566 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
567 usb_free_urb(mos7720_port->write_urb_pool[j]);
568 }
569 }
570
571 /* While closing port, shutdown all bulk read, write *
572 * and interrupt read if they exists */
573 if (serial->dev) {
574 dbg("Shutdown bulk write");
575 usb_kill_urb(port->write_urb);
576 dbg("Shutdown bulk read");
577 usb_kill_urb(port->read_urb);
578 }
579
580 data = 0x00;
581 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
582 0x04, &data);
583
584 data = 0x00;
585 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
586 0x01, &data);
587
588 mos7720_port->open = 0;
589
590 dbg("Leaving %s", __FUNCTION__);
591}
592
593static void mos7720_break(struct usb_serial_port *port, int break_state)
594{
595 unsigned char data;
596 struct usb_serial *serial;
597 struct moschip_port *mos7720_port;
598
599 dbg("Entering %s", __FUNCTION__);
600
601 serial = port->serial;
602
603 mos7720_port = usb_get_serial_port_data(port);
604 if (mos7720_port == NULL)
605 return;
606
607 if (break_state == -1)
608 data = mos7720_port->shadowLCR | UART_LCR_SBC;
609 else
610 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
611
612 mos7720_port->shadowLCR = data;
613 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
614 0x03, &data);
615
616 return;
617}
618
619/*
620 * mos7720_write_room
621 * this function is called by the tty driver when it wants to know how many
622 * bytes of data we can accept for a specific port.
623 * If successful, we return the amount of room that we have for this port
624 * Otherwise we return a negative error number.
625 */
626static int mos7720_write_room(struct usb_serial_port *port)
627{
628 struct moschip_port *mos7720_port;
629 int room = 0;
630 int i;
631
632 dbg("%s:entering ...........", __FUNCTION__);
633
634 mos7720_port = usb_get_serial_port_data(port);
635 if (mos7720_port == NULL) {
636 dbg("%s:leaving ...........", __FUNCTION__);
637 return -ENODEV;
638 }
639
640 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100641 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700642 room += URB_TRANSFER_BUFFER_SIZE;
643 }
644
645 dbg("%s - returns %d", __FUNCTION__, room);
646 return room;
647}
648
649static int mos7720_write(struct usb_serial_port *port,
650 const unsigned char *data, int count)
651{
652 int status;
653 int i;
654 int bytes_sent = 0;
655 int transfer_size;
656
657 struct moschip_port *mos7720_port;
658 struct usb_serial *serial;
659 struct urb *urb;
660 const unsigned char *current_position = data;
661
662 dbg("%s:entering ...........", __FUNCTION__);
663
664 serial = port->serial;
665
666 mos7720_port = usb_get_serial_port_data(port);
667 if (mos7720_port == NULL) {
668 dbg("mos7720_port is NULL");
669 return -ENODEV;
670 }
671
672 /* try to find a free urb in the list */
673 urb = NULL;
674
675 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100676 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700677 urb = mos7720_port->write_urb_pool[i];
678 dbg("URB:%d",i);
679 break;
680 }
681 }
682
683 if (urb == NULL) {
684 dbg("%s - no more free urbs", __FUNCTION__);
685 goto exit;
686 }
687
688 if (urb->transfer_buffer == NULL) {
689 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
690 GFP_KERNEL);
691 if (urb->transfer_buffer == NULL) {
692 err("%s no more kernel memory...", __FUNCTION__);
693 goto exit;
694 }
695 }
696 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
697
698 memcpy(urb->transfer_buffer, current_position, transfer_size);
699 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
700 urb->transfer_buffer);
701
702 /* fill urb with data and submit */
703 usb_fill_bulk_urb(urb, serial->dev,
704 usb_sndbulkpipe(serial->dev,
705 port->bulk_out_endpointAddress),
706 urb->transfer_buffer, transfer_size,
707 mos7720_bulk_out_data_callback, mos7720_port);
708
709 /* send it down the pipe */
710 status = usb_submit_urb(urb,GFP_ATOMIC);
711 if (status) {
712 err("%s - usb_submit_urb(write bulk) failed with status = %d",
713 __FUNCTION__, status);
714 bytes_sent = status;
715 goto exit;
716 }
717 bytes_sent = transfer_size;
718
719exit:
720 return bytes_sent;
721}
722
723static void mos7720_throttle(struct usb_serial_port *port)
724{
725 struct moschip_port *mos7720_port;
726 struct tty_struct *tty;
727 int status;
728
729 dbg("%s- port %d\n", __FUNCTION__, port->number);
730
731 mos7720_port = usb_get_serial_port_data(port);
732
733 if (mos7720_port == NULL)
734 return;
735
736 if (!mos7720_port->open) {
737 dbg("port not opened");
738 return;
739 }
740
741 dbg("%s: Entering ..........", __FUNCTION__);
742
743 tty = port->tty;
744 if (!tty) {
745 dbg("%s - no tty available", __FUNCTION__);
746 return;
747 }
748
749 /* if we are implementing XON/XOFF, send the stop character */
750 if (I_IXOFF(tty)) {
751 unsigned char stop_char = STOP_CHAR(tty);
752 status = mos7720_write(port, &stop_char, 1);
753 if (status <= 0)
754 return;
755 }
756
757 /* if we are implementing RTS/CTS, toggle that line */
758 if (tty->termios->c_cflag & CRTSCTS) {
759 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
760 status = send_mos_cmd(port->serial, MOS_WRITE,
761 port->number - port->serial->minor,
762 UART_MCR, &mos7720_port->shadowMCR);
763 if (status != 0)
764 return;
765 }
766}
767
768static void mos7720_unthrottle(struct usb_serial_port *port)
769{
770 struct tty_struct *tty;
771 int status;
772 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
773
774 if (mos7720_port == NULL)
775 return;
776
777 if (!mos7720_port->open) {
778 dbg("%s - port not opened", __FUNCTION__);
779 return;
780 }
781
782 dbg("%s: Entering ..........", __FUNCTION__);
783
784 tty = port->tty;
785 if (!tty) {
786 dbg("%s - no tty available", __FUNCTION__);
787 return;
788 }
789
790 /* if we are implementing XON/XOFF, send the start character */
791 if (I_IXOFF(tty)) {
792 unsigned char start_char = START_CHAR(tty);
793 status = mos7720_write(port, &start_char, 1);
794 if (status <= 0)
795 return;
796 }
797
798 /* if we are implementing RTS/CTS, toggle that line */
799 if (tty->termios->c_cflag & CRTSCTS) {
800 mos7720_port->shadowMCR |= UART_MCR_RTS;
801 status = send_mos_cmd(port->serial, MOS_WRITE,
802 port->number - port->serial->minor,
803 UART_MCR, &mos7720_port->shadowMCR);
804 if (status != 0)
805 return;
806 }
807}
808
809static int set_higher_rates(struct moschip_port *mos7720_port,
810 unsigned int baud)
811{
812 unsigned char data;
813 struct usb_serial_port *port;
814 struct usb_serial *serial;
815 int port_number;
816
817 if (mos7720_port == NULL)
818 return -EINVAL;
819
820 port = mos7720_port->port;
821 serial = port->serial;
822
823 /***********************************************
824 * Init Sequence for higher rates
825 ***********************************************/
826 dbg("Sending Setting Commands ..........");
827 port_number = port->number - port->serial->minor;
828
829 data = 0x000;
830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
831 data = 0x000;
832 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
833 data = 0x0CF;
834 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
835 data = 0x00b;
836 mos7720_port->shadowMCR = data;
837 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
838 data = 0x00b;
839 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
840
841 data = 0x000;
842 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
843 data = 0x000;
844 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
845
846
847 /***********************************************
848 * Set for higher rates *
849 ***********************************************/
850
851 data = baud * 0x10;
852 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
853
854 data = 0x003;
855 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
856 data = 0x003;
857 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
858
859 data = 0x02b;
860 mos7720_port->shadowMCR = data;
861 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
862 data = 0x02b;
863 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
864
865 /***********************************************
866 * Set DLL/DLM
867 ***********************************************/
868
869 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
870 mos7720_port->shadowLCR = data;
871 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
872
873 data = 0x001; /* DLL */
874 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
875 data = 0x000; /* DLM */
876 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
877
878 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
879 mos7720_port->shadowLCR = data;
880 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
881
882 return 0;
883}
884
885/* baud rate information */
886struct divisor_table_entry
887{
888 __u32 baudrate;
889 __u16 divisor;
890};
891
892/* Define table of divisors for moschip 7720 hardware *
893 * These assume a 3.6864MHz crystal, the standard /16, and *
894 * MCR.7 = 0. */
895static struct divisor_table_entry divisor_table[] = {
896 { 50, 2304},
897 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
898 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
899 { 150, 768},
900 { 300, 384},
901 { 600, 192},
902 { 1200, 96},
903 { 1800, 64},
904 { 2400, 48},
905 { 4800, 24},
906 { 7200, 16},
907 { 9600, 12},
908 { 19200, 6},
909 { 38400, 3},
910 { 57600, 2},
911 { 115200, 1},
912};
913
914/*****************************************************************************
915 * calc_baud_rate_divisor
916 * this function calculates the proper baud rate divisor for the specified
917 * baud rate.
918 *****************************************************************************/
919static int calc_baud_rate_divisor(int baudrate, int *divisor)
920{
921 int i;
922 __u16 custom;
923 __u16 round1;
924 __u16 round;
925
926
927 dbg("%s - %d", __FUNCTION__, baudrate);
928
929 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
930 if (divisor_table[i].baudrate == baudrate) {
931 *divisor = divisor_table[i].divisor;
932 return 0;
933 }
934 }
935
936 /* After trying for all the standard baud rates *
937 * Try calculating the divisor for this baud rate */
938 if (baudrate > 75 && baudrate < 230400) {
939 /* get the divisor */
940 custom = (__u16)(230400L / baudrate);
941
942 /* Check for round off */
943 round1 = (__u16)(2304000L / baudrate);
944 round = (__u16)(round1 - (custom * 10));
945 if (round > 4)
946 custom++;
947 *divisor = custom;
948
949 dbg("Baud %d = %d",baudrate, custom);
950 return 0;
951 }
952
953 dbg("Baud calculation Failed...");
954 return -EINVAL;
955}
956
957/*
958 * send_cmd_write_baud_rate
959 * this function sends the proper command to change the baud rate of the
960 * specified port.
961 */
962static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
963 int baudrate)
964{
965 struct usb_serial_port *port;
966 struct usb_serial *serial;
967 int divisor;
968 int status;
969 unsigned char data;
970 unsigned char number;
971
972 if (mos7720_port == NULL)
973 return -1;
974
975 port = mos7720_port->port;
976 serial = port->serial;
977
978 dbg("%s: Entering ..........", __FUNCTION__);
979
980 number = port->number - port->serial->minor;
981 dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
982
983 /* Calculate the Divisor */
984 status = calc_baud_rate_divisor(baudrate, &divisor);
985 if (status) {
986 err("%s - bad baud rate", __FUNCTION__);
987 return status;
988 }
989
990 /* Enable access to divisor latch */
991 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
992 mos7720_port->shadowLCR = data;
993 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
994
995 /* Write the divisor */
996 data = ((unsigned char)(divisor & 0xff));
997 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
998
999 data = ((unsigned char)((divisor & 0xff00) >> 8));
1000 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1001
1002 /* Disable access to divisor latch */
1003 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1004 mos7720_port->shadowLCR = data;
1005 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1006
1007 return status;
1008}
1009
1010/*
1011 * change_port_settings
1012 * This routine is called to set the UART on the device to match
1013 * the specified new settings.
1014 */
1015static void change_port_settings(struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001016 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001017{
1018 struct usb_serial_port *port;
1019 struct usb_serial *serial;
1020 struct tty_struct *tty;
1021 int baud;
1022 unsigned cflag;
1023 unsigned iflag;
1024 __u8 mask = 0xff;
1025 __u8 lData;
1026 __u8 lParity;
1027 __u8 lStop;
1028 int status;
1029 int port_number;
1030 char data;
1031
1032 if (mos7720_port == NULL)
1033 return ;
1034
1035 port = mos7720_port->port;
1036 serial = port->serial;
1037 port_number = port->number - port->serial->minor;
1038
1039 dbg("%s - port %d", __FUNCTION__, port->number);
1040
1041 if (!mos7720_port->open) {
1042 dbg("%s - port not opened", __FUNCTION__);
1043 return;
1044 }
1045
1046 tty = mos7720_port->port->tty;
1047
1048 if ((!tty) || (!tty->termios)) {
1049 dbg("%s - no tty structures", __FUNCTION__);
1050 return;
1051 }
1052
1053 dbg("%s: Entering ..........", __FUNCTION__);
1054
1055 lData = UART_LCR_WLEN8;
1056 lStop = 0x00; /* 1 stop bit */
1057 lParity = 0x00; /* No parity */
1058
1059 cflag = tty->termios->c_cflag;
1060 iflag = tty->termios->c_iflag;
1061
1062 /* Change the number of bits */
1063 switch (cflag & CSIZE) {
1064 case CS5:
1065 lData = UART_LCR_WLEN5;
1066 mask = 0x1f;
1067 break;
1068
1069 case CS6:
1070 lData = UART_LCR_WLEN6;
1071 mask = 0x3f;
1072 break;
1073
1074 case CS7:
1075 lData = UART_LCR_WLEN7;
1076 mask = 0x7f;
1077 break;
1078 default:
1079 case CS8:
1080 lData = UART_LCR_WLEN8;
1081 break;
1082 }
1083
1084 /* Change the Parity bit */
1085 if (cflag & PARENB) {
1086 if (cflag & PARODD) {
1087 lParity = UART_LCR_PARITY;
1088 dbg("%s - parity = odd", __FUNCTION__);
1089 } else {
1090 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1091 dbg("%s - parity = even", __FUNCTION__);
1092 }
1093
1094 } else {
1095 dbg("%s - parity = none", __FUNCTION__);
1096 }
1097
1098 if (cflag & CMSPAR)
1099 lParity = lParity | 0x20;
1100
1101 /* Change the Stop bit */
1102 if (cflag & CSTOPB) {
1103 lStop = UART_LCR_STOP;
1104 dbg("%s - stop bits = 2", __FUNCTION__);
1105 } else {
1106 lStop = 0x00;
1107 dbg("%s - stop bits = 1", __FUNCTION__);
1108 }
1109
1110#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1111#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1112#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1113
1114 /* Update the LCR with the correct value */
1115 mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1116 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1117
1118
1119 /* Disable Interrupts */
1120 data = 0x00;
1121 send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1122
1123 data = 0x00;
1124 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1125
1126 data = 0xcf;
1127 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1128
1129 /* Send the updated LCR value to the mos7720 */
1130 data = mos7720_port->shadowLCR;
1131 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1132
1133 data = 0x00b;
1134 mos7720_port->shadowMCR = data;
1135 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1136 data = 0x00b;
1137 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1138
1139 /* set up the MCR register and send it to the mos7720 */
1140 mos7720_port->shadowMCR = UART_MCR_OUT2;
1141 if (cflag & CBAUD)
1142 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1143
1144 if (cflag & CRTSCTS) {
1145 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1146
1147 /* To set hardware flow control to the specified *
1148 * serial port, in SP1/2_CONTROL_REG */
1149 if (port->number) {
1150 data = 0x001;
1151 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1152 0x08, &data);
1153 } else {
1154 data = 0x002;
1155 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1156 0x08, &data);
1157 }
1158 } else {
1159 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1160 }
1161
1162 data = mos7720_port->shadowMCR;
1163 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1164
1165 /* Determine divisor based on baud rate */
1166 baud = tty_get_baud_rate(tty);
1167 if (!baud) {
1168 /* pick a default, any default... */
1169 dbg("Picked default baud...");
1170 baud = 9600;
1171 }
1172
1173 if (baud >= 230400) {
1174 set_higher_rates(mos7720_port, baud);
1175 /* Enable Interrupts */
1176 data = 0x0c;
1177 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1178 return;
1179 }
1180
1181 dbg("%s - baud rate = %d", __FUNCTION__, baud);
1182 status = send_cmd_write_baud_rate(mos7720_port, baud);
1183
1184 /* Enable Interrupts */
1185 data = 0x0c;
1186 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1187
1188 if (port->read_urb->status != -EINPROGRESS) {
1189 port->read_urb->dev = serial->dev;
1190
1191 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1192 if (status)
1193 dbg("usb_submit_urb(read bulk) failed, status = %d",
1194 status);
1195 }
1196 return;
1197}
1198
1199/*
1200 * mos7720_set_termios
1201 * this function is called by the tty driver when it wants to change the
1202 * termios structure.
1203 */
1204static void mos7720_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001205 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206{
1207 int status;
1208 unsigned int cflag;
1209 struct usb_serial *serial;
1210 struct moschip_port *mos7720_port;
1211 struct tty_struct *tty;
1212
1213 serial = port->serial;
1214
1215 mos7720_port = usb_get_serial_port_data(port);
1216
1217 if (mos7720_port == NULL)
1218 return;
1219
1220 tty = port->tty;
1221
1222 if (!port->tty || !port->tty->termios) {
1223 dbg("%s - no tty or termios", __FUNCTION__);
1224 return;
1225 }
1226
1227 if (!mos7720_port->open) {
1228 dbg("%s - port not opened", __FUNCTION__);
1229 return;
1230 }
1231
1232 dbg("%s\n","setting termios - ASPIRE");
1233
1234 cflag = tty->termios->c_cflag;
1235
1236 if (!cflag) {
1237 printk("%s %s\n",__FUNCTION__,"cflag is NULL");
1238 return;
1239 }
1240
1241 /* check that they really want us to change something */
1242 if (old_termios) {
1243 if ((cflag == old_termios->c_cflag) &&
1244 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1245 RELEVANT_IFLAG(old_termios->c_iflag))) {
1246 dbg("Nothing to change");
1247 return;
1248 }
1249 }
1250
1251 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1252 tty->termios->c_cflag,
1253 RELEVANT_IFLAG(tty->termios->c_iflag));
1254
1255 if (old_termios)
1256 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1257 old_termios->c_cflag,
1258 RELEVANT_IFLAG(old_termios->c_iflag));
1259
1260 dbg("%s - port %d", __FUNCTION__, port->number);
1261
1262 /* change the port settings to the new ones specified */
1263 change_port_settings(mos7720_port, old_termios);
1264
1265 if(!port->read_urb) {
1266 dbg("%s","URB KILLED !!!!!\n");
1267 return;
1268 }
1269
1270 if(port->read_urb->status != -EINPROGRESS) {
1271 port->read_urb->dev = serial->dev;
1272 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1273 if (status)
1274 dbg("usb_submit_urb(read bulk) failed, status = %d",
1275 status);
1276 }
1277 return;
1278}
1279
1280/*
1281 * get_lsr_info - get line status register info
1282 *
1283 * Purpose: Let user call ioctl() to get info when the UART physically
1284 * is emptied. On bus types like RS485, the transmitter must
1285 * release the bus after transmitting. This must be done when
1286 * the transmit shift register is empty, not be done when the
1287 * transmit holding register is empty. This functionality
1288 * allows an RS485 driver to be written in user space.
1289 */
1290static int get_lsr_info(struct moschip_port *mos7720_port,
1291 unsigned int __user *value)
1292{
1293 int count;
1294 unsigned int result = 0;
1295
1296 count = mos7720_chars_in_buffer(mos7720_port->port);
1297 if (count == 0) {
1298 dbg("%s -- Empty", __FUNCTION__);
1299 result = TIOCSER_TEMT;
1300 }
1301
1302 if (copy_to_user(value, &result, sizeof(int)))
1303 return -EFAULT;
1304 return 0;
1305}
1306
1307/*
1308 * get_number_bytes_avail - get number of bytes available
1309 *
1310 * Purpose: Let user call ioctl to get the count of number of bytes available.
1311 */
1312static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1313 unsigned int __user *value)
1314{
1315 unsigned int result = 0;
1316 struct tty_struct *tty = mos7720_port->port->tty;
1317
1318 if (!tty)
1319 return -ENOIOCTLCMD;
1320
1321 result = tty->read_cnt;
1322
1323 dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
1324 if (copy_to_user(value, &result, sizeof(int)))
1325 return -EFAULT;
1326
1327 return -ENOIOCTLCMD;
1328}
1329
1330static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1331 unsigned int __user *value)
1332{
1333 unsigned int mcr ;
1334 unsigned int arg;
1335 unsigned char data;
1336
1337 struct usb_serial_port *port;
1338
1339 if (mos7720_port == NULL)
1340 return -1;
1341
1342 port = (struct usb_serial_port*)mos7720_port->port;
1343 mcr = mos7720_port->shadowMCR;
1344
1345 if (copy_from_user(&arg, value, sizeof(int)))
1346 return -EFAULT;
1347
1348 switch (cmd) {
1349 case TIOCMBIS:
1350 if (arg & TIOCM_RTS)
1351 mcr |= UART_MCR_RTS;
1352 if (arg & TIOCM_DTR)
1353 mcr |= UART_MCR_RTS;
1354 if (arg & TIOCM_LOOP)
1355 mcr |= UART_MCR_LOOP;
1356 break;
1357
1358 case TIOCMBIC:
1359 if (arg & TIOCM_RTS)
1360 mcr &= ~UART_MCR_RTS;
1361 if (arg & TIOCM_DTR)
1362 mcr &= ~UART_MCR_RTS;
1363 if (arg & TIOCM_LOOP)
1364 mcr &= ~UART_MCR_LOOP;
1365 break;
1366
1367 case TIOCMSET:
1368 /* turn off the RTS and DTR and LOOPBACK
1369 * and then only turn on what was asked to */
1370 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1371 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1372 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1373 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1374 break;
1375 }
1376
1377 mos7720_port->shadowMCR = mcr;
1378
1379 data = mos7720_port->shadowMCR;
1380 send_mos_cmd(port->serial, MOS_WRITE,
1381 port->number - port->serial->minor, UART_MCR, &data);
1382
1383 return 0;
1384}
1385
1386static int get_modem_info(struct moschip_port *mos7720_port,
1387 unsigned int __user *value)
1388{
1389 unsigned int result = 0;
1390 unsigned int msr = mos7720_port->shadowMSR;
1391 unsigned int mcr = mos7720_port->shadowMCR;
1392
1393 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1394 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1395 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1396 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1397 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1398 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1399
1400
1401 dbg("%s -- %x", __FUNCTION__, result);
1402
1403 if (copy_to_user(value, &result, sizeof(int)))
1404 return -EFAULT;
1405 return 0;
1406}
1407
1408static int get_serial_info(struct moschip_port *mos7720_port,
1409 struct serial_struct __user *retinfo)
1410{
1411 struct serial_struct tmp;
1412
1413 if (!retinfo)
1414 return -EFAULT;
1415
1416 memset(&tmp, 0, sizeof(tmp));
1417
1418 tmp.type = PORT_16550A;
1419 tmp.line = mos7720_port->port->serial->minor;
1420 tmp.port = mos7720_port->port->number;
1421 tmp.irq = 0;
1422 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1423 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1424 tmp.baud_base = 9600;
1425 tmp.close_delay = 5*HZ;
1426 tmp.closing_wait = 30*HZ;
1427
1428 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1429 return -EFAULT;
1430 return 0;
1431}
1432
1433static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1434 unsigned int cmd, unsigned long arg)
1435{
1436 struct moschip_port *mos7720_port;
1437 struct async_icount cnow;
1438 struct async_icount cprev;
1439 struct serial_icounter_struct icount;
1440
1441 mos7720_port = usb_get_serial_port_data(port);
1442 if (mos7720_port == NULL)
1443 return -ENODEV;
1444
1445 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1446
1447 switch (cmd) {
1448 case TIOCINQ:
1449 /* return number of bytes available */
1450 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1451 return get_number_bytes_avail(mos7720_port,
1452 (unsigned int __user *)arg);
1453 break;
1454
1455 case TIOCSERGETLSR:
1456 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1457 return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1458 return 0;
1459
1460 case TIOCMBIS:
1461 case TIOCMBIC:
1462 case TIOCMSET:
1463 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
1464 port->number);
1465 return set_modem_info(mos7720_port, cmd,
1466 (unsigned int __user *)arg);
1467
1468 case TIOCMGET:
1469 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
1470 return get_modem_info(mos7720_port,
1471 (unsigned int __user *)arg);
1472
1473 case TIOCGSERIAL:
1474 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1475 return get_serial_info(mos7720_port,
1476 (struct serial_struct __user *)arg);
1477
1478 case TIOCSSERIAL:
1479 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1480 break;
1481
1482 case TIOCMIWAIT:
1483 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1484 cprev = mos7720_port->icount;
1485 while (1) {
1486 if (signal_pending(current))
1487 return -ERESTARTSYS;
1488 cnow = mos7720_port->icount;
1489 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1490 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1491 return -EIO; /* no change => error */
1492 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1493 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1494 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1495 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1496 return 0;
1497 }
1498 cprev = cnow;
1499 }
1500 /* NOTREACHED */
1501 break;
1502
1503 case TIOCGICOUNT:
1504 cnow = mos7720_port->icount;
1505 icount.cts = cnow.cts;
1506 icount.dsr = cnow.dsr;
1507 icount.rng = cnow.rng;
1508 icount.dcd = cnow.dcd;
1509 icount.rx = cnow.rx;
1510 icount.tx = cnow.tx;
1511 icount.frame = cnow.frame;
1512 icount.overrun = cnow.overrun;
1513 icount.parity = cnow.parity;
1514 icount.brk = cnow.brk;
1515 icount.buf_overrun = cnow.buf_overrun;
1516
1517 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
1518 port->number, icount.rx, icount.tx );
1519 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1520 return -EFAULT;
1521 return 0;
1522 }
1523
1524 return -ENOIOCTLCMD;
1525}
1526
1527static int mos7720_startup(struct usb_serial *serial)
1528{
1529 struct moschip_serial *mos7720_serial;
1530 struct moschip_port *mos7720_port;
1531 struct usb_device *dev;
1532 int i;
1533 char data;
1534
1535 dbg("%s: Entering ..........", __FUNCTION__);
1536
1537 if (!serial) {
1538 dbg("Invalid Handler");
1539 return -ENODEV;
1540 }
1541
1542 dev = serial->dev;
1543
1544 /* create our private serial structure */
1545 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1546 if (mos7720_serial == NULL) {
1547 err("%s - Out of memory", __FUNCTION__);
1548 return -ENOMEM;
1549 }
1550
1551 usb_set_serial_data(serial, mos7720_serial);
1552
1553 /* we set up the pointers to the endpoints in the mos7720_open *
1554 * function, as the structures aren't created yet. */
1555
1556 /* set up port private structures */
1557 for (i = 0; i < serial->num_ports; ++i) {
1558 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1559 if (mos7720_port == NULL) {
1560 err("%s - Out of memory", __FUNCTION__);
1561 usb_set_serial_data(serial, NULL);
1562 kfree(mos7720_serial);
1563 return -ENOMEM;
1564 }
1565
1566 /* Initialize all port interrupt end point to port 0 int
1567 * endpoint. Our device has only one interrupt endpoint
1568 * comman to all ports */
1569 serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1570
1571 mos7720_port->port = serial->port[i];
1572 usb_set_serial_port_data(serial->port[i], mos7720_port);
1573
1574 dbg("port number is %d", serial->port[i]->number);
1575 dbg("serial number is %d", serial->minor);
1576 }
1577
1578
1579 /* setting configuration feature to one */
1580 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1581 (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1582
1583 send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
1584 dbg("LSR:%x",data);
1585
1586 send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
1587 dbg("LSR:%x",data);
1588
1589 return 0;
1590}
1591
1592static void mos7720_shutdown(struct usb_serial *serial)
1593{
1594 int i;
1595
1596 /* free private structure allocated for serial port */
1597 for (i=0; i < serial->num_ports; ++i) {
1598 kfree(usb_get_serial_port_data(serial->port[i]));
1599 usb_set_serial_port_data(serial->port[i], NULL);
1600 }
1601
1602 /* free private structure allocated for serial device */
1603 kfree(usb_get_serial_data(serial));
1604 usb_set_serial_data(serial, NULL);
1605}
1606
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001607static struct usb_driver usb_driver = {
1608 .name = "moschip7720",
1609 .probe = usb_serial_probe,
1610 .disconnect = usb_serial_disconnect,
1611 .id_table = moschip_port_id_table,
1612 .no_dynamic_id = 1,
1613};
1614
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001615static struct usb_serial_driver moschip7720_2port_driver = {
1616 .driver = {
1617 .owner = THIS_MODULE,
1618 .name = "moschip7720",
1619 },
1620 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001621 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001622 .id_table = moschip_port_id_table,
1623 .num_interrupt_in = 1,
1624 .num_bulk_in = 2,
1625 .num_bulk_out = 2,
1626 .num_ports = 2,
1627 .open = mos7720_open,
1628 .close = mos7720_close,
1629 .throttle = mos7720_throttle,
1630 .unthrottle = mos7720_unthrottle,
1631 .attach = mos7720_startup,
1632 .shutdown = mos7720_shutdown,
1633 .ioctl = mos7720_ioctl,
1634 .set_termios = mos7720_set_termios,
1635 .write = mos7720_write,
1636 .write_room = mos7720_write_room,
1637 .chars_in_buffer = mos7720_chars_in_buffer,
1638 .break_ctl = mos7720_break,
1639 .read_bulk_callback = mos7720_bulk_in_callback,
Oliver Neukume8e30c72007-03-14 11:11:08 +01001640 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001641};
1642
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001643static int __init moschip7720_init(void)
1644{
1645 int retval;
1646
1647 dbg("%s: Entering ..........", __FUNCTION__);
1648
1649 /* Register with the usb serial */
1650 retval = usb_serial_register(&moschip7720_2port_driver);
1651 if (retval)
1652 goto failed_port_device_register;
1653
1654 info(DRIVER_DESC " " DRIVER_VERSION);
1655
1656 /* Register with the usb */
1657 retval = usb_register(&usb_driver);
1658 if (retval)
1659 goto failed_usb_register;
1660
1661 return 0;
1662
1663failed_usb_register:
1664 usb_serial_deregister(&moschip7720_2port_driver);
1665
1666failed_port_device_register:
1667 return retval;
1668}
1669
1670static void __exit moschip7720_exit(void)
1671{
1672 usb_deregister(&usb_driver);
1673 usb_serial_deregister(&moschip7720_2port_driver);
1674}
1675
1676module_init(moschip7720_init);
1677module_exit(moschip7720_exit);
1678
1679/* Module information */
1680MODULE_AUTHOR( DRIVER_AUTHOR );
1681MODULE_DESCRIPTION( DRIVER_DESC );
1682MODULE_LICENSE("GPL");
1683
1684module_param(debug, bool, S_IRUGO | S_IWUSR);
1685MODULE_PARM_DESC(debug, "Debug enabled or not");