1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
/*
Keyspan USB to Serial Converter driver
(C) Copyright (C) 2000
Hugh Blemings <hugh@linuxcare.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
See http://www.linuxcare.com.au/hugh/keyspan.html for more
information on this driver.
Code in this driver inspired by and in a number of places taken
from Brian Warner's original Keyspan-PDA driver.
This driver has been put together with the support of Innosys, Inc.
and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
Thanks Guys :)
Tip 'o the hat to Linuxcare for supporting staff in their work on
open source projects.
Wed Jul 19 14:00:42 EST 2000 gkh
Added module_init and module_exit functions to handle the fact that this
driver is a loadable module now.
Tue Jul 18 16:14:52 EST 2000 Hugh
Basic character input/output for USA-19 now mostly works,
fixed at 9600 baud for the moment.
*/
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/malloc.h>
#include <linux/fcntl.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#ifdef CONFIG_USB_SERIAL_DEBUG
#define DEBUG
#else
#undef DEBUG
#endif
#include <linux/usb.h>
#include "usb-serial.h"
#include "keyspan.h"
/* Per device and per port private data */
struct keyspan_serial_private {
struct urb *in_urbs[8];
struct urb *out_urbs[8];
char out_buffer[64];
char in_buffer[64];
};
struct keyspan_port_private {
/* Keep track of which output endpoint to use */
int out_flip;
/* Settings for the port */
int baud;
int old_baud;
enum {parity_none, parity_odd, parity_even} parity;
enum {flow_none, flow_cts, flow_xon} flow_control;
int rts_state;
int dtr_state;
};
/* FIXME this will break if multiple physical interfaces used */
static wait_queue_head_t out_wait;
/* Include Keyspan message headers (not both yet, need some tweaks
to get clean build) */
/*#include "keyspan_usa26msg.h"*/
#include "keyspan_usa28msg.h"
/* If you don't get debugging output, uncomment the following
two lines to enable cheat. */
#undef dbg
#define dbg printk
/* Functions - mostly stubs for now */
static void keyspan_rx_throttle (struct usb_serial_port *port)
{
dbg("keyspan_rx_throttle port %d", port->number);
}
static void keyspan_rx_unthrottle (struct usb_serial_port *port)
{
dbg("keyspan_rx_unthrottle port %d", port->number);
}
static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
{
dbg("keyspan_break_ctl");
}
static void keyspan_set_termios (struct usb_serial_port *port,
struct termios *old_termios)
{
dbg("keyspan_set_termios");
}
static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
unsigned int cmd, unsigned long arg)
{
unsigned int value;
dbg("keyspan_ioctl_info");
switch (cmd) {
case TIOCMGET:
value = TIOCM_DTR | TIOCM_RNG;
if (copy_to_user((unsigned int *)arg, &value, sizeof(int))) {
return -EFAULT;
}
else {
return 0;
}
default:
return -ENOIOCTLCMD;
}
return -ENOIOCTLCMD;
}
static int keyspan_write(struct usb_serial_port *port, int from_user,
const unsigned char *buf, int count)
{
struct usb_serial *serial = port->serial;
struct keyspan_serial_private *s_priv;
struct keyspan_port_private *p_priv;
int current_urb;
int i;
s_priv = (struct keyspan_serial_private *)(serial->private);
p_priv = (struct keyspan_port_private *)(port->private);
if (p_priv->out_flip == 0) {
current_urb = 0;
p_priv->out_flip = 1;
}
else {
current_urb = 1;
p_priv->out_flip = 0;
}
dbg("keyspan_write called for port %d (%d) chars {", port->number, count);
for (i = 0; i < count ; i++) {
dbg("%02x ", buf[i]);
}
dbg("}\n");
if (count == 0) {
dbg("write request of 0 bytes");
return (0);
}
/* only send data if we have a bulk out endpoint */
if (s_priv->out_urbs[current_urb]) {
while (s_priv->out_urbs[current_urb]->status == -EINPROGRESS) {
dbg (__FUNCTION__ " INPROGRES\n");
interruptible_sleep_on(&out_wait);
if (signal_pending(current)) {
dbg (__FUNCTION__ " signal\n");
return (-ERESTARTSYS);
}
}
/*if (s_priv->out_urbs[current_urb]->status == -EINPROGRESS) {
dbg ("already writing");
return (-EAGAIN);
}*/
/* First byte in buffer is "last flag" - unused so
for now so set to zero */
memset(s_priv->out_urbs[current_urb]->transfer_buffer, 0, 1);
if (from_user) {
copy_from_user(s_priv->out_urbs[current_urb]->transfer_buffer + 1, buf, count);
}
else {
memcpy (s_priv->out_urbs[current_urb]->transfer_buffer + 1, buf, count);
}
/* send the data out the bulk port */
s_priv->out_urbs[current_urb]->transfer_buffer_length = count + 1;
if (usb_submit_urb(s_priv->out_urbs[current_urb])) {
dbg("usb_submit_urb(write bulk) failed");
}
return (count);
}
/* no bulk out, so return 0 bytes written */
return (0);
}
static void keyspan_write_bulk_callback (struct urb *urb)
{
int endpoint;
endpoint = usb_pipeendpoint(urb->pipe);
dbg("keyspan_write_bulk_callback for endpoint %d\n", endpoint);
/* Only do wakeup if this callback is from one of the data
endpoints. */
if (endpoint == 2 || endpoint == 3) {
wake_up_interruptible(&out_wait);
}
}
static void keyspan_read_bulk_callback (struct urb *urb)
{
int i;
int endpoint;
struct usb_serial *serial;
struct usb_serial_port *port;
struct tty_struct *tty;
unsigned char *data = urb->transfer_buffer;
endpoint = usb_pipeendpoint(urb->pipe);
if (urb->status) {
dbg(__FUNCTION__ "nonzero status: %x on endpoint %d.\n",
urb->status, endpoint);
return;
}
switch (endpoint) {
/* If this is one of the data endpoints, stuff it's
contents into the tty flip_buffer. */
case 1:
case 2: serial = (struct usb_serial *) urb->context;
port = &serial->port[0];
tty = port->tty;
if (urb->actual_length) {
for (i = 0; i < urb->actual_length ; ++i) {
tty_insert_flip_char(tty, data[i], 0);
}
tty_flip_buffer_push(tty);
}
break;
/* INACK endpoint */
case 3: dbg(__FUNCTION__ " callback for INACK endpoint\n");
break;
/* INSTAT endpoint */
case 4: dbg(__FUNCTION__ " callback for INSTAT endpoint\n");
break;
default:
dbg(__FUNCTION__ " callback for unknown endpoint!\n");
break;
}
/* Resubmit urb so we continue receiving */
if (usb_submit_urb(urb)) {
dbg(__FUNCTION__ "resubmit read urb failed.\n");
}
return;
}
static int keyspan_write_room (struct usb_serial_port *port)
{
// dbg("keyspan_write_room called\n");
return (32);
}
static int keyspan_chars_in_buffer (struct usb_serial_port *port)
{
return (0);
}
static int keyspan_open (struct usb_serial_port *port, struct file *filp)
{
struct keyspan_port_private *p_priv;
struct keyspan_serial_private *s_priv;
struct usb_serial *serial = port->serial;
int i;
s_priv = (struct keyspan_serial_private *)(serial->private);
p_priv = (struct keyspan_port_private *)(port->private);
dbg("keyspan_open called.\n");
if (port->active) {
dbg(__FUNCTION__ "port->active already true!\n");
return (-EINVAL);
}
p_priv = (struct keyspan_port_private *)(port->private);
p_priv->out_flip = 0;
port->active = 1;
/* Start reading from port */
for (i = 0; i < 4; i++) {
if (s_priv->in_urbs[i]) {
if (usb_submit_urb(s_priv->in_urbs[i])) {
dbg(__FUNCTION__ " submit in urb %d failed", i);
}
}
}
keyspan_usa19_send_setup(serial, port);
return (0);
}
static void keyspan_close(struct usb_serial_port *port, struct file *filp)
{
int i;
struct usb_serial *serial = port->serial; /* FIXME should so sanity check */
struct keyspan_serial_private *s_priv;
s_priv = (struct keyspan_serial_private *)(serial->private);
/* Stop reading/writing urbs */
for (i = 0; i < 4; i++) {
if (s_priv->in_urbs[i]) {
usb_unlink_urb(s_priv->in_urbs[i]);
}
}
for (i = 0; i < 3; i++) {
if (s_priv->out_urbs[i]) {
usb_unlink_urb(s_priv->out_urbs[i]);
}
}
port->active = 0;
dbg("keyspan_close called\n");
}
/* download the firmware to a pre-renumeration device */
static int keyspan_fake_startup (struct usb_serial *serial)
{
int response;
const struct ezusb_hex_record *record;
char *fw_name;
dbg("Keyspan startup version %04x product %04x\n", serial->dev->descriptor.bcdDevice,
serial->dev->descriptor.idProduct);
if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
dbg("Firmware already loaded. Quitting.\n");
return(1);
}
/* Select firmware image on the basis of idProduct */
switch (serial->dev->descriptor.idProduct) {
case 0x0101: record = &keyspan_usa28_firmware[0];
fw_name = "USA28";
break;
case 0x0102: record = &keyspan_usa28x_firmware[0];
fw_name = "USA28X";
break;
case 0x0103: record = &keyspan_usa19_firmware[0];
fw_name = "USA19";
break;
case 0x0105: record = &keyspan_usa18x_firmware[0];
fw_name = "USA18X";
break;
case 0x0106: record = &keyspan_usa19w_firmware[0];
fw_name = "USA19W";
break;
default: record = NULL;
fw_name = "Unknown";
break;
}
if (record == NULL) {
err("Required keyspan firmware image (%s) unavailable.\n", fw_name);
return(1);
}
dbg("Uploading Keyspan %s firmware.\n", fw_name);
/* download the firmware image */
response = ezusb_set_reset(serial, 1);
while(record->address != 0xffff) {
response = ezusb_writememory(serial, record->address,
(unsigned char *)record->data,
record->data_size, 0xa0);
if (response < 0) {
err("ezusb_writememory failed for Keyspan"
"firmware (%d %04X %p %d)",
response,
record->address, record->data, record->data_size);
break;
}
record++;
}
/* bring device out of reset. Renumeration will occur in a
moment and the new device will bind to the real driver */
response = ezusb_set_reset(serial, 0);
/* we don't want this device to have a driver assigned to it. */
return (1);
}
/* USA-19 uses three output endpoints and four input
endpoints. First two output endpoints are for
data (used in an alternating fashion), the third is
output control. First two input endpoints are for
data (again alternating), the third is the ACK
endpoint, the fourth is input status. */
static void keyspan_usa19_setup_urbs(struct usb_serial *serial)
{
struct keyspan_serial_private *s_priv;
int i;
s_priv = (struct keyspan_serial_private *)(serial->private);
/* Output urbs first */
dbg(__FUNCTION__ "Allocating output urbs.\n");
for (i = 0; i < 3; i++) {
s_priv->out_urbs[i] = usb_alloc_urb (0); /* No ISO */
if (!s_priv->out_urbs[i]) {
dbg (__FUNCTION__ "Alloc for %d out urb failed.\n", i);
return;
}
FILL_BULK_URB(s_priv->out_urbs[i], serial->dev,
usb_sndbulkpipe(serial->dev, i + 1),
&s_priv->out_buffer[i], sizeof(s_priv->out_buffer[i]),
keyspan_write_bulk_callback,
serial);
}
/* Now input urbs */
dbg(__FUNCTION__ "Allocating input urbs.\n");
for (i = 0; i < 4; i++) {
s_priv->in_urbs[i] = usb_alloc_urb (0); /* No ISO */
if (!s_priv->in_urbs[i]) {
dbg (__FUNCTION__ "Alloc for %d in urb failed.\n", i);
return;
}
FILL_BULK_URB(s_priv->in_urbs[i], serial->dev,
usb_rcvbulkpipe(serial->dev, i + 0x81),
&s_priv->in_buffer[i], sizeof(s_priv->in_buffer[i]),
keyspan_read_bulk_callback,
serial);
}
}
static int keyspan_usa19_calc_baud(u32 baud_rate, u8 *rate_hi, u8 *rate_low)
{
u32 b16, /* baud rate times 16 (actual rate used internally) */
div, /* divisor */
cnt; /* inverse of divisor (programmed into 8051) */
/* prevent divide by zero... */
if( (b16 = (baud_rate * 16L)) == 0) {
return (KEYSPAN_INVALID_BAUD_RATE);
}
/* calculate the divisor and the counter (its inverse) */
if( (div = (USA19_BAUDCLK / b16)) == 0) {
return (KEYSPAN_INVALID_BAUD_RATE);
}
else {
cnt = 0 - div;
}
if(div > 0xffff) {
return (KEYSPAN_INVALID_BAUD_RATE);
}
/* return the counter values */
*rate_low = (u8) (cnt & 0xff);
*rate_hi = (u8) ((cnt >> 8) & 0xff);
dbg(__FUNCTION__ " Baud rate of %d is %02x %02x.\n", baud_rate, *rate_hi, *rate_low);
return (KEYSPAN_BAUD_RATE_OK);
}
static int keyspan_usa19_send_setup(struct usb_serial *serial, struct usb_serial_port *port)
{
struct portControlMessage msg;
struct keyspan_serial_private *s_priv;
struct keyspan_port_private *p_priv;
s_priv = (struct keyspan_serial_private *)(serial->private);
p_priv = (struct keyspan_port_private *)(port->private);
//memset(msg, 0, sizeof (struct portControlMessage));
msg.setBaudRate = 1;
if (keyspan_usa19_calc_baud(9600, &msg.baudHi, &msg.baudLo) ==
KEYSPAN_INVALID_BAUD_RATE ) {
dbg(__FUNCTION__ "Invalid baud rate requested %d.\n", 9600);
msg.baudLo = 0xff;
msg.baudHi = 0xb2; /* Values for 9600 baud */
}
/* If parity is enabled, we must calculate it ourselves. */
if (p_priv->parity) {
msg.parity = 1;
}
else {
msg.parity = 0;
}
msg.ctsFlowControl = 0;
msg.xonFlowControl = 0;
msg.rts = 1;
msg.dtr = 0;
msg.forwardingLength = 1;
msg.forwardMs = 10;
msg.breakThreshold = 45;
msg.xonChar = 17;
msg.xoffChar = 19;
msg._txOn = 1;
msg._txOff = 0;
msg.txFlush = 0;
msg.txForceXoff = 0;
msg.txBreak = 0;
msg.rxOn = 1;
msg.rxOff = 0;
msg.rxFlush = 0;
msg.rxForward = 0;
msg.returnStatus = 1;
msg.resetDataToggle = 1;
/* only do something if we have a bulk out endpoint */
if (s_priv->out_urbs[2]) {
if (s_priv->out_urbs[2]->status == -EINPROGRESS) {
dbg (__FUNCTION__ " already writing");
return(-1);
}
memcpy (s_priv->out_urbs[2]->transfer_buffer, &msg, sizeof(msg));
/* send the data out the device on control endpoint */
s_priv->out_urbs[2]->transfer_buffer_length = sizeof(msg);
if (usb_submit_urb(s_priv->out_urbs[2])) {
dbg(__FUNCTION__ " usb_submit_urb(setup) failed\n");
}
else {
dbg(__FUNCTION__ " usb_submit_urb(setup) OK %d bytes\n", s_priv->out_urbs[2]->transfer_buffer_length);
}
}
return (0);
}
/* Gets called by the "real" driver (ie once firmware is loaded
and renumeration has taken place. */
static int keyspan_startup (struct usb_serial *serial)
{
int i;
struct usb_serial_port *port;
dbg("keyspan_startup called.\n");
/* Setup private data for serial driver */
serial->private = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
if (!serial->private) {
dbg(__FUNCTION__ "kmalloc for keyspan_serial_private failed!.\n");
return (1);
}
memset(serial->private, 0, sizeof(struct keyspan_serial_private));
init_waitqueue_head(&out_wait);
/* Now setup per port private data */
for (i = 0; i < serial->num_ports; i++) {
port = &serial->port[i];
port->private = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
if (!port->private) {
dbg(__FUNCTION__ "kmalloc for keyspan_port_private (%d) failed!.\n", i);
return (1);
}
memset(port->private, 0, sizeof(struct keyspan_port_private));
}
switch (serial->dev->descriptor.idProduct) {
case 0x0107: keyspan_usa19_setup_urbs(serial);
//keyspan_send_usa19_setup(serial);
break;
default: break;
}
return (0);
}
static void keyspan_shutdown (struct usb_serial *serial)
{
int i;
struct usb_serial_port *port;
struct keyspan_serial_private *s_priv;
dbg("keyspan_shutdown called freeing ");
s_priv = (struct keyspan_serial_private *)(serial->private);
/* Stop reading/writing urbs */
for (i = 0; i < 4; i++) {
if (s_priv->in_urbs[i]) {
usb_unlink_urb(s_priv->in_urbs[i]);
}
}
for (i = 0; i < 3; i++) {
if (s_priv->out_urbs[i]) {
usb_unlink_urb(s_priv->out_urbs[i]);
}
}
/* Now free them */
for (i = 0; i < 7; i ++) {
if (s_priv->in_urbs[i] != NULL) {
dbg("in%d ", i);
usb_free_urb(s_priv->in_urbs[i]);
}
if (s_priv->out_urbs[i] != NULL) {
dbg("out%d ", i);
usb_free_urb(s_priv->out_urbs[i]);
}
}
dbg("urbs.\n");
dbg("Freeing serial->private.\n");
kfree(serial->private);
dbg("Freeing port->private.\n");
/* Now free per port private data */
for (i = 0; i < serial->num_ports; i++) {
port = &serial->port[i];
kfree(port->private);
}
}
int keyspan_init (void)
{
usb_serial_register (&keyspan_usa18x_pre_device);
usb_serial_register (&keyspan_usa19_pre_device);
usb_serial_register (&keyspan_usa19w_pre_device);
usb_serial_register (&keyspan_usa28_pre_device);
usb_serial_register (&keyspan_usa28x_pre_device);
usb_serial_register (&keyspan_usa18x_device);
usb_serial_register (&keyspan_usa19_device);
usb_serial_register (&keyspan_usa19w_device);
usb_serial_register (&keyspan_usa28_device);
usb_serial_register (&keyspan_usa28x_device);
return 0;
}
void keyspan_exit (void)
{
usb_serial_deregister (&keyspan_usa18x_pre_device);
usb_serial_deregister (&keyspan_usa19_pre_device);
usb_serial_deregister (&keyspan_usa19w_pre_device);
usb_serial_deregister (&keyspan_usa28_pre_device);
usb_serial_deregister (&keyspan_usa28x_pre_device);
usb_serial_deregister (&keyspan_usa18x_device);
usb_serial_deregister (&keyspan_usa19_device);
usb_serial_deregister (&keyspan_usa19w_device);
usb_serial_deregister (&keyspan_usa28_device);
usb_serial_deregister (&keyspan_usa28x_device);
}
module_init(keyspan_init);
module_exit(keyspan_exit);
|