Free Electrons

Embedded Linux Experts

   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
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/*
    comedi/drivers/vmk80xx.c
    Velleman USB Board Low-Level Driver

    Copyright (C) 2009 Manuel Gebele <forensixs@gmx.de>, Germany

    COMEDI - Linux Control and Measurement Device Interface
    Copyright (C) 2000 David A. Schleef <ds@schleef.org>

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
/*
Driver: vmk80xx
Description: Velleman USB Board Low-Level Driver
Devices: K8055/K8061 aka VM110/VM140
Author: Manuel Gebele <forensixs@gmx.de>
Updated: Sun, 10 May 2009 11:14:59 +0200
Status: works

Supports:
 - analog input
 - analog output
 - digital input
 - digital output
 - counter
 - pwm
*/
/*
Changelog:

0.8.81	-3-  code completely rewritten (adjust driver logic)
0.8.81  -2-  full support for K8061
0.8.81  -1-  fix some mistaken among others the number of
	     supported boards and I/O handling

0.7.76  -4-  renamed to vmk80xx
0.7.76  -3-  detect K8061 (only theoretically supported)
0.7.76  -2-  code completely rewritten (adjust driver logic)
0.7.76  -1-  support for digital and counter subdevice
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/errno.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/usb.h>
#include <linux/uaccess.h>

#include "../comedidev.h"

#define BOARDNAME "vmk80xx"

MODULE_AUTHOR("Manuel Gebele <forensixs@gmx.de>");
MODULE_DESCRIPTION("Velleman USB Board Low-Level Driver");
MODULE_SUPPORTED_DEVICE("K8055/K8061 aka VM110/VM140");
MODULE_VERSION("0.8.01");
MODULE_LICENSE("GPL");

enum {
	DEVICE_VMK8055,
	DEVICE_VMK8061
};

static const struct usb_device_id vmk80xx_id_table[] = {
	{USB_DEVICE(0x10cf, 0x5500), .driver_info = DEVICE_VMK8055},
	{USB_DEVICE(0x10cf, 0x5501), .driver_info = DEVICE_VMK8055},
	{USB_DEVICE(0x10cf, 0x5502), .driver_info = DEVICE_VMK8055},
	{USB_DEVICE(0x10cf, 0x5503), .driver_info = DEVICE_VMK8055},
	{USB_DEVICE(0x10cf, 0x8061), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8062), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8063), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8064), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8065), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8066), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8067), .driver_info = DEVICE_VMK8061},
	{USB_DEVICE(0x10cf, 0x8068), .driver_info = DEVICE_VMK8061},
	{}			/* terminating entry */
};

MODULE_DEVICE_TABLE(usb, vmk80xx_id_table);

#define VMK8055_DI_REG          0x00
#define VMK8055_DO_REG          0x01
#define VMK8055_AO1_REG         0x02
#define VMK8055_AO2_REG         0x03
#define VMK8055_AI1_REG         0x02
#define VMK8055_AI2_REG         0x03
#define VMK8055_CNT1_REG        0x04
#define VMK8055_CNT2_REG        0x06

#define VMK8061_CH_REG          0x01
#define VMK8061_DI_REG          0x01
#define VMK8061_DO_REG          0x01
#define VMK8061_PWM_REG1        0x01
#define VMK8061_PWM_REG2        0x02
#define VMK8061_CNT_REG         0x02
#define VMK8061_AO_REG          0x02
#define VMK8061_AI_REG1         0x02
#define VMK8061_AI_REG2         0x03

#define VMK8055_CMD_RST         0x00
#define VMK8055_CMD_DEB1_TIME   0x01
#define VMK8055_CMD_DEB2_TIME   0x02
#define VMK8055_CMD_RST_CNT1    0x03
#define VMK8055_CMD_RST_CNT2    0x04
#define VMK8055_CMD_WRT_AD      0x05

#define VMK8061_CMD_RD_AI       0x00
#define VMK8061_CMR_RD_ALL_AI   0x01	/* !non-active! */
#define VMK8061_CMD_SET_AO      0x02
#define VMK8061_CMD_SET_ALL_AO  0x03	/* !non-active! */
#define VMK8061_CMD_OUT_PWM     0x04
#define VMK8061_CMD_RD_DI       0x05
#define VMK8061_CMD_DO          0x06	/* !non-active! */
#define VMK8061_CMD_CLR_DO      0x07
#define VMK8061_CMD_SET_DO      0x08
#define VMK8061_CMD_RD_CNT      0x09	/* TODO: completely pointless? */
#define VMK8061_CMD_RST_CNT     0x0a	/* TODO: completely pointless? */
#define VMK8061_CMD_RD_VERSION  0x0b	/* internal usage */
#define VMK8061_CMD_RD_JMP_STAT 0x0c	/* TODO: not implemented yet */
#define VMK8061_CMD_RD_PWR_STAT 0x0d	/* internal usage */
#define VMK8061_CMD_RD_DO       0x0e
#define VMK8061_CMD_RD_AO       0x0f
#define VMK8061_CMD_RD_PWM      0x10

#define VMK80XX_MAX_BOARDS      COMEDI_NUM_BOARD_MINORS

#define TRANS_OUT_BUSY          1
#define TRANS_IN_BUSY           2
#define TRANS_IN_RUNNING        3

#define IC3_VERSION             (1 << 0)
#define IC6_VERSION             (1 << 1)

#define URB_RCV_FLAG            (1 << 0)
#define URB_SND_FLAG            (1 << 1)

#define CONFIG_VMK80XX_DEBUG
#undef CONFIG_VMK80XX_DEBUG

#ifdef CONFIG_VMK80XX_DEBUG
static int dbgvm = 1;
#else
static int dbgvm;
#endif

#ifdef CONFIG_COMEDI_DEBUG
static int dbgcm = 1;
#else
static int dbgcm;
#endif

#define dbgvm(fmt, arg...)                     \
do {                                           \
	if (dbgvm)                             \
		printk(KERN_DEBUG fmt, ##arg); \
} while (0)

#define dbgcm(fmt, arg...)                     \
do {                                           \
	if (dbgcm)                             \
		printk(KERN_DEBUG fmt, ##arg); \
} while (0)

enum vmk80xx_model {
	VMK8055_MODEL,
	VMK8061_MODEL
};

struct firmware_version {
	unsigned char ic3_vers[32];	/* USB-Controller */
	unsigned char ic6_vers[32];	/* CPU */
};

static const struct comedi_lrange vmk8055_range = {
	1, {UNI_RANGE(5)}
};

static const struct comedi_lrange vmk8061_range = {
	2, {UNI_RANGE(5), UNI_RANGE(10)}
};

struct vmk80xx_board {
	const char *name;
	enum vmk80xx_model model;
	const struct comedi_lrange *range;
	__u8 ai_chans;
	__le16 ai_bits;
	__u8 ao_chans;
	__le16 ao_bits;
	__u8 di_chans;
	__le16 di_bits;
	__u8 do_chans;
	__le16 do_bits;
	__u8 cnt_chans;
	__le16 cnt_bits;
	__u8 pwm_chans;
	__le16 pwm_bits;
};

enum {
	VMK80XX_SUBD_AI,
	VMK80XX_SUBD_AO,
	VMK80XX_SUBD_DI,
	VMK80XX_SUBD_DO,
	VMK80XX_SUBD_CNT,
	VMK80XX_SUBD_PWM,
};

struct vmk80xx_usb {
	struct usb_device *udev;
	struct usb_interface *intf;
	struct usb_endpoint_descriptor *ep_rx;
	struct usb_endpoint_descriptor *ep_tx;
	struct usb_anchor rx_anchor;
	struct usb_anchor tx_anchor;
	struct vmk80xx_board board;
	struct firmware_version fw;
	struct semaphore limit_sem;
	wait_queue_head_t read_wait;
	wait_queue_head_t write_wait;
	unsigned char *usb_rx_buf;
	unsigned char *usb_tx_buf;
	unsigned long flags;
	int probed;
	int attached;
	int count;
};

static struct vmk80xx_usb vmb[VMK80XX_MAX_BOARDS];

static DEFINE_MUTEX(glb_mutex);

static void vmk80xx_tx_callback(struct urb *urb)
{
	struct vmk80xx_usb *dev = urb->context;
	int stat = urb->status;

	dbgvm("vmk80xx: %s\n", __func__);

	if (stat && !(stat == -ENOENT
		      || stat == -ECONNRESET || stat == -ESHUTDOWN))
		dbgcm("comedi#: vmk80xx: %s - nonzero urb status (%d)\n",
		      __func__, stat);

	if (!test_bit(TRANS_OUT_BUSY, &dev->flags))
		return;

	clear_bit(TRANS_OUT_BUSY, &dev->flags);

	wake_up_interruptible(&dev->write_wait);
}

static void vmk80xx_rx_callback(struct urb *urb)
{
	struct vmk80xx_usb *dev = urb->context;
	int stat = urb->status;

	dbgvm("vmk80xx: %s\n", __func__);

	switch (stat) {
	case 0:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
		break;
	default:
		dbgcm("comedi#: vmk80xx: %s - nonzero urb status (%d)\n",
		      __func__, stat);
		goto resubmit;
	}

	goto exit;
resubmit:
	if (test_bit(TRANS_IN_RUNNING, &dev->flags) && dev->intf) {
		usb_anchor_urb(urb, &dev->rx_anchor);

		if (!usb_submit_urb(urb, GFP_KERNEL))
			goto exit;

		err("comedi#: vmk80xx: %s - submit urb failed\n", __func__);

		usb_unanchor_urb(urb);
	}
exit:
	clear_bit(TRANS_IN_BUSY, &dev->flags);

	wake_up_interruptible(&dev->read_wait);
}

static int vmk80xx_check_data_link(struct vmk80xx_usb *dev)
{
	unsigned int tx_pipe;
	unsigned int rx_pipe;
	unsigned char tx[1];
	unsigned char rx[2];

	dbgvm("vmk80xx: %s\n", __func__);

	tx_pipe = usb_sndbulkpipe(dev->udev, 0x01);
	rx_pipe = usb_rcvbulkpipe(dev->udev, 0x81);

	tx[0] = VMK8061_CMD_RD_PWR_STAT;

	/*
	 * Check that IC6 (PIC16F871) is powered and
	 * running and the data link between IC3 and
	 * IC6 is working properly
	 */
	usb_bulk_msg(dev->udev, tx_pipe, tx, 1, NULL, dev->ep_tx->bInterval);
	usb_bulk_msg(dev->udev, rx_pipe, rx, 2, NULL, HZ * 10);

	return (int)rx[1];
}

static void vmk80xx_read_eeprom(struct vmk80xx_usb *dev, int flag)
{
	unsigned int tx_pipe;
	unsigned int rx_pipe;
	unsigned char tx[1];
	unsigned char rx[64];
	int cnt;

	dbgvm("vmk80xx: %s\n", __func__);

	tx_pipe = usb_sndbulkpipe(dev->udev, 0x01);
	rx_pipe = usb_rcvbulkpipe(dev->udev, 0x81);

	tx[0] = VMK8061_CMD_RD_VERSION;

	/*
	 * Read the firmware version info of IC3 and
	 * IC6 from the internal EEPROM of the IC
	 */
	usb_bulk_msg(dev->udev, tx_pipe, tx, 1, NULL, dev->ep_tx->bInterval);
	usb_bulk_msg(dev->udev, rx_pipe, rx, 64, &cnt, HZ * 10);

	rx[cnt] = '\0';

	if (flag & IC3_VERSION)
		strncpy(dev->fw.ic3_vers, rx + 1, 24);
	else			/* IC6_VERSION */
		strncpy(dev->fw.ic6_vers, rx + 25, 24);
}

static int vmk80xx_reset_device(struct vmk80xx_usb *dev)
{
	struct urb *urb;
	unsigned int tx_pipe;
	int ival;
	size_t size;

	dbgvm("vmk80xx: %s\n", __func__);

	urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!urb)
		return -ENOMEM;

	tx_pipe = usb_sndintpipe(dev->udev, 0x01);

	ival = dev->ep_tx->bInterval;
	size = le16_to_cpu(dev->ep_tx->wMaxPacketSize);

	dev->usb_tx_buf[0] = VMK8055_CMD_RST;
	dev->usb_tx_buf[1] = 0x00;
	dev->usb_tx_buf[2] = 0x00;
	dev->usb_tx_buf[3] = 0x00;
	dev->usb_tx_buf[4] = 0x00;
	dev->usb_tx_buf[5] = 0x00;
	dev->usb_tx_buf[6] = 0x00;
	dev->usb_tx_buf[7] = 0x00;

	usb_fill_int_urb(urb, dev->udev, tx_pipe, dev->usb_tx_buf,
			 size, vmk80xx_tx_callback, dev, ival);

	usb_anchor_urb(urb, &dev->tx_anchor);

	return usb_submit_urb(urb, GFP_KERNEL);
}

static void vmk80xx_build_int_urb(struct urb *urb, int flag)
{
	struct vmk80xx_usb *dev = urb->context;
	__u8 rx_addr;
	__u8 tx_addr;
	unsigned int pipe;
	unsigned char *buf;
	size_t size;
	void (*callback) (struct urb *);
	int ival;

	dbgvm("vmk80xx: %s\n", __func__);

	if (flag & URB_RCV_FLAG) {
		rx_addr = dev->ep_rx->bEndpointAddress;
		pipe = usb_rcvintpipe(dev->udev, rx_addr);
		buf = dev->usb_rx_buf;
		size = le16_to_cpu(dev->ep_rx->wMaxPacketSize);
		callback = vmk80xx_rx_callback;
		ival = dev->ep_rx->bInterval;
	} else {		/* URB_SND_FLAG */
		tx_addr = dev->ep_tx->bEndpointAddress;
		pipe = usb_sndintpipe(dev->udev, tx_addr);
		buf = dev->usb_tx_buf;
		size = le16_to_cpu(dev->ep_tx->wMaxPacketSize);
		callback = vmk80xx_tx_callback;
		ival = dev->ep_tx->bInterval;
	}

	usb_fill_int_urb(urb, dev->udev, pipe, buf, size, callback, dev, ival);
}

static void vmk80xx_do_bulk_msg(struct vmk80xx_usb *dev)
{
	__u8 tx_addr;
	__u8 rx_addr;
	unsigned int tx_pipe;
	unsigned int rx_pipe;
	size_t size;

	dbgvm("vmk80xx: %s\n", __func__);

	set_bit(TRANS_IN_BUSY, &dev->flags);
	set_bit(TRANS_OUT_BUSY, &dev->flags);

	tx_addr = dev->ep_tx->bEndpointAddress;
	rx_addr = dev->ep_rx->bEndpointAddress;
	tx_pipe = usb_sndbulkpipe(dev->udev, tx_addr);
	rx_pipe = usb_rcvbulkpipe(dev->udev, rx_addr);

	/*
	 * The max packet size attributes of the K8061
	 * input/output endpoints are identical
	 */
	size = le16_to_cpu(dev->ep_tx->wMaxPacketSize);

	usb_bulk_msg(dev->udev, tx_pipe, dev->usb_tx_buf,
		     size, NULL, dev->ep_tx->bInterval);
	usb_bulk_msg(dev->udev, rx_pipe, dev->usb_rx_buf, size, NULL, HZ * 10);

	clear_bit(TRANS_OUT_BUSY, &dev->flags);
	clear_bit(TRANS_IN_BUSY, &dev->flags);
}

static int vmk80xx_read_packet(struct vmk80xx_usb *dev)
{
	struct urb *urb;
	int retval;

	dbgvm("vmk80xx: %s\n", __func__);

	if (!dev->intf)
		return -ENODEV;

	/* Only useful for interrupt transfers */
	if (test_bit(TRANS_IN_BUSY, &dev->flags))
		if (wait_event_interruptible(dev->read_wait,
					     !test_bit(TRANS_IN_BUSY,
						       &dev->flags)))
			return -ERESTART;

	if (dev->board.model == VMK8061_MODEL) {
		vmk80xx_do_bulk_msg(dev);

		return 0;
	}

	urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!urb)
		return -ENOMEM;

	urb->context = dev;
	vmk80xx_build_int_urb(urb, URB_RCV_FLAG);

	set_bit(TRANS_IN_RUNNING, &dev->flags);
	set_bit(TRANS_IN_BUSY, &dev->flags);

	usb_anchor_urb(urb, &dev->rx_anchor);

	retval = usb_submit_urb(urb, GFP_KERNEL);
	if (!retval)
		goto exit;

	clear_bit(TRANS_IN_RUNNING, &dev->flags);
	usb_unanchor_urb(urb);

exit:
	usb_free_urb(urb);

	return retval;
}

static int vmk80xx_write_packet(struct vmk80xx_usb *dev, int cmd)
{
	struct urb *urb;
	int retval;

	dbgvm("vmk80xx: %s\n", __func__);

	if (!dev->intf)
		return -ENODEV;

	if (test_bit(TRANS_OUT_BUSY, &dev->flags))
		if (wait_event_interruptible(dev->write_wait,
					     !test_bit(TRANS_OUT_BUSY,
						       &dev->flags)))
			return -ERESTART;

	if (dev->board.model == VMK8061_MODEL) {
		dev->usb_tx_buf[0] = cmd;
		vmk80xx_do_bulk_msg(dev);

		return 0;
	}

	urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!urb)
		return -ENOMEM;

	urb->context = dev;
	vmk80xx_build_int_urb(urb, URB_SND_FLAG);

	set_bit(TRANS_OUT_BUSY, &dev->flags);

	usb_anchor_urb(urb, &dev->tx_anchor);

	dev->usb_tx_buf[0] = cmd;

	retval = usb_submit_urb(urb, GFP_KERNEL);
	if (!retval)
		goto exit;

	clear_bit(TRANS_OUT_BUSY, &dev->flags);
	usb_unanchor_urb(urb);

exit:
	usb_free_urb(urb);

	return retval;
}

#define DIR_IN  1
#define DIR_OUT 2

static int rudimentary_check(struct vmk80xx_usb *dev, int dir)
{
	if (!dev)
		return -EFAULT;
	if (!dev->probed)
		return -ENODEV;
	if (!dev->attached)
		return -ENODEV;
	if (dir & DIR_IN) {
		if (test_bit(TRANS_IN_BUSY, &dev->flags))
			return -EBUSY;
	}
	if (dir & DIR_OUT) {
		if (test_bit(TRANS_OUT_BUSY, &dev->flags))
			return -EBUSY;
	}

	return 0;
}

static int vmk80xx_ai_rinsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	int reg[2];
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	switch (dev->board.model) {
	case VMK8055_MODEL:
		if (!chan)
			reg[0] = VMK8055_AI1_REG;
		else
			reg[0] = VMK8055_AI2_REG;
		break;
	case VMK8061_MODEL:
		reg[0] = VMK8061_AI_REG1;
		reg[1] = VMK8061_AI_REG2;
		dev->usb_tx_buf[0] = VMK8061_CMD_RD_AI;
		dev->usb_tx_buf[VMK8061_CH_REG] = chan;
		break;
	}

	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		if (dev->board.model == VMK8055_MODEL) {
			data[n] = dev->usb_rx_buf[reg[0]];
			continue;
		}

		/* VMK8061_MODEL */
		data[n] = dev->usb_rx_buf[reg[0]] + 256 *
		    dev->usb_rx_buf[reg[1]];
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_ao_winsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	int cmd;
	int reg;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_OUT);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	switch (dev->board.model) {
	case VMK8055_MODEL:
		cmd = VMK8055_CMD_WRT_AD;
		if (!chan)
			reg = VMK8055_AO1_REG;
		else
			reg = VMK8055_AO2_REG;
		break;
	default:		/* NOTE: avoid compiler warnings */
		cmd = VMK8061_CMD_SET_AO;
		reg = VMK8061_AO_REG;
		dev->usb_tx_buf[VMK8061_CH_REG] = chan;
		break;
	}

	for (n = 0; n < insn->n; n++) {
		dev->usb_tx_buf[reg] = data[n];

		if (vmk80xx_write_packet(dev, cmd))
			break;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_ao_rinsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	int reg;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	reg = VMK8061_AO_REG - 1;

	dev->usb_tx_buf[0] = VMK8061_CMD_RD_AO;

	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		data[n] = dev->usb_rx_buf[reg + chan];
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_di_bits(struct comedi_device *cdev,
			   struct comedi_subdevice *s,
			   struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	unsigned char *rx_buf;
	int reg;
	int retval;

	dbgvm("vmk80xx: %s\n", __func__);

	retval = rudimentary_check(dev, DIR_IN);
	if (retval)
		return retval;

	down(&dev->limit_sem);

	rx_buf = dev->usb_rx_buf;

	if (dev->board.model == VMK8061_MODEL) {
		reg = VMK8061_DI_REG;
		dev->usb_tx_buf[0] = VMK8061_CMD_RD_DI;
	} else {
		reg = VMK8055_DI_REG;
	}

	retval = vmk80xx_read_packet(dev);

	if (!retval) {
		if (dev->board.model == VMK8055_MODEL)
			data[1] = (((rx_buf[reg] >> 4) & 0x03) |
				  ((rx_buf[reg] << 2) & 0x04) |
				  ((rx_buf[reg] >> 3) & 0x18));
		else
			data[1] = rx_buf[reg];

		retval = 2;
	}

	up(&dev->limit_sem);

	return retval;
}

static int vmk80xx_di_rinsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	unsigned char *rx_buf;
	int reg;
	int inp;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	rx_buf = dev->usb_rx_buf;

	if (dev->board.model == VMK8061_MODEL) {
		reg = VMK8061_DI_REG;
		dev->usb_tx_buf[0] = VMK8061_CMD_RD_DI;
	} else {
		reg = VMK8055_DI_REG;
	}
	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		if (dev->board.model == VMK8055_MODEL)
			inp = (((rx_buf[reg] >> 4) & 0x03) |
			       ((rx_buf[reg] << 2) & 0x04) |
			       ((rx_buf[reg] >> 3) & 0x18));
		else
			inp = rx_buf[reg];

		data[n] = (inp >> chan) & 1;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_do_winsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	unsigned char *tx_buf;
	int reg;
	int cmd;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_OUT);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	tx_buf = dev->usb_tx_buf;

	for (n = 0; n < insn->n; n++) {
		if (dev->board.model == VMK8055_MODEL) {
			reg = VMK8055_DO_REG;
			cmd = VMK8055_CMD_WRT_AD;
			if (data[n] == 1)
				tx_buf[reg] |= (1 << chan);
			else
				tx_buf[reg] ^= (1 << chan);
		} else { /* VMK8061_MODEL */
			reg = VMK8061_DO_REG;
			if (data[n] == 1) {
				cmd = VMK8061_CMD_SET_DO;
				tx_buf[reg] = 1 << chan;
			} else {
				cmd = VMK8061_CMD_CLR_DO;
				tx_buf[reg] = 0xff - (1 << chan);
			}
		}

		if (vmk80xx_write_packet(dev, cmd))
			break;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_do_rinsn(struct comedi_device *cdev,
			    struct comedi_subdevice *s,
			    struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	int reg;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	reg = VMK8061_DO_REG;

	dev->usb_tx_buf[0] = VMK8061_CMD_RD_DO;

	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		data[n] = (dev->usb_rx_buf[reg] >> chan) & 1;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_do_bits(struct comedi_device *cdev,
			   struct comedi_subdevice *s,
			   struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	unsigned char *rx_buf, *tx_buf;
	int dir, reg, cmd;
	int retval;

	dbgvm("vmk80xx: %s\n", __func__);

	dir = 0;

	if (data[0])
		dir |= DIR_OUT;

	if (dev->board.model == VMK8061_MODEL)
		dir |= DIR_IN;

	retval = rudimentary_check(dev, dir);
	if (retval)
		return retval;

	down(&dev->limit_sem);

	rx_buf = dev->usb_rx_buf;
	tx_buf = dev->usb_tx_buf;

	if (data[0]) {
		if (dev->board.model == VMK8055_MODEL) {
			reg = VMK8055_DO_REG;
			cmd = VMK8055_CMD_WRT_AD;
		} else { /* VMK8061_MODEL */
			reg = VMK8061_DO_REG;
			cmd = VMK8061_CMD_DO;
		}

		tx_buf[reg] &= ~data[0];
		tx_buf[reg] |= (data[0] & data[1]);

		retval = vmk80xx_write_packet(dev, cmd);

		if (retval)
			goto out;
	}

	if (dev->board.model == VMK8061_MODEL) {
		reg = VMK8061_DO_REG;
		tx_buf[0] = VMK8061_CMD_RD_DO;

		retval = vmk80xx_read_packet(dev);

		if (!retval) {
			data[1] = rx_buf[reg];
			retval = 2;
		}
	} else {
		data[1] = tx_buf[reg];
		retval = 2;
	}

out:
	up(&dev->limit_sem);

	return retval;
}

static int vmk80xx_cnt_rinsn(struct comedi_device *cdev,
			     struct comedi_subdevice *s,
			     struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int chan;
	int reg[2];
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	switch (dev->board.model) {
	case VMK8055_MODEL:
		if (!chan)
			reg[0] = VMK8055_CNT1_REG;
		else
			reg[0] = VMK8055_CNT2_REG;
		break;
	case VMK8061_MODEL:
		reg[0] = VMK8061_CNT_REG;
		reg[1] = VMK8061_CNT_REG;
		dev->usb_tx_buf[0] = VMK8061_CMD_RD_CNT;
		break;
	}

	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		if (dev->board.model == VMK8055_MODEL)
			data[n] = dev->usb_rx_buf[reg[0]];
		else /* VMK8061_MODEL */
			data[n] = dev->usb_rx_buf[reg[0] * (chan + 1) + 1]
			    + 256 * dev->usb_rx_buf[reg[1] * 2 + 2];
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_cnt_cinsn(struct comedi_device *cdev,
			     struct comedi_subdevice *s,
			     struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	unsigned int insn_cmd;
	int chan;
	int cmd;
	int reg;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_OUT);
	if (n)
		return n;

	down(&dev->limit_sem);

	insn_cmd = data[0];
	if (insn_cmd != INSN_CONFIG_RESET && insn_cmd != GPCT_RESET)
		return -EINVAL;

	chan = CR_CHAN(insn->chanspec);

	if (dev->board.model == VMK8055_MODEL) {
		if (!chan) {
			cmd = VMK8055_CMD_RST_CNT1;
			reg = VMK8055_CNT1_REG;
		} else {
			cmd = VMK8055_CMD_RST_CNT2;
			reg = VMK8055_CNT2_REG;
		}

		dev->usb_tx_buf[reg] = 0x00;
	} else {
		cmd = VMK8061_CMD_RST_CNT;
	}

	for (n = 0; n < insn->n; n++)
		if (vmk80xx_write_packet(dev, cmd))
			break;

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_cnt_winsn(struct comedi_device *cdev,
			     struct comedi_subdevice *s,
			     struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	unsigned long debtime;
	unsigned long val;
	int chan;
	int cmd;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_OUT);
	if (n)
		return n;

	down(&dev->limit_sem);
	chan = CR_CHAN(insn->chanspec);

	if (!chan)
		cmd = VMK8055_CMD_DEB1_TIME;
	else
		cmd = VMK8055_CMD_DEB2_TIME;

	for (n = 0; n < insn->n; n++) {
		debtime = data[n];
		if (debtime == 0)
			debtime = 1;

		/* TODO: Prevent overflows */
		if (debtime > 7450)
			debtime = 7450;

		val = int_sqrt(debtime * 1000 / 115);
		if (((val + 1) * val) < debtime * 1000 / 115)
			val += 1;

		dev->usb_tx_buf[6 + chan] = val;

		if (vmk80xx_write_packet(dev, cmd))
			break;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_pwm_rinsn(struct comedi_device *cdev,
			     struct comedi_subdevice *s,
			     struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	int reg[2];
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_IN);
	if (n)
		return n;

	down(&dev->limit_sem);

	reg[0] = VMK8061_PWM_REG1;
	reg[1] = VMK8061_PWM_REG2;

	dev->usb_tx_buf[0] = VMK8061_CMD_RD_PWM;

	for (n = 0; n < insn->n; n++) {
		if (vmk80xx_read_packet(dev))
			break;

		data[n] = dev->usb_rx_buf[reg[0]] + 4 * dev->usb_rx_buf[reg[1]];
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_pwm_winsn(struct comedi_device *cdev,
			     struct comedi_subdevice *s,
			     struct comedi_insn *insn, unsigned int *data)
{
	struct vmk80xx_usb *dev = cdev->private;
	unsigned char *tx_buf;
	int reg[2];
	int cmd;
	int n;

	dbgvm("vmk80xx: %s\n", __func__);

	n = rudimentary_check(dev, DIR_OUT);
	if (n)
		return n;

	down(&dev->limit_sem);

	tx_buf = dev->usb_tx_buf;

	reg[0] = VMK8061_PWM_REG1;
	reg[1] = VMK8061_PWM_REG2;

	cmd = VMK8061_CMD_OUT_PWM;

	/*
	 * The followin piece of code was translated from the inline
	 * assembler code in the DLL source code.
	 *
	 * asm
	 *   mov eax, k  ; k is the value (data[n])
	 *   and al, 03h ; al are the lower 8 bits of eax
	 *   mov lo, al  ; lo is the low part (tx_buf[reg[0]])
	 *   mov eax, k
	 *   shr eax, 2  ; right shift eax register by 2
	 *   mov hi, al  ; hi is the high part (tx_buf[reg[1]])
	 * end;
	 */
	for (n = 0; n < insn->n; n++) {
		tx_buf[reg[0]] = (unsigned char)(data[n] & 0x03);
		tx_buf[reg[1]] = (unsigned char)(data[n] >> 2) & 0xff;

		if (vmk80xx_write_packet(dev, cmd))
			break;
	}

	up(&dev->limit_sem);

	return n;
}

static int vmk80xx_attach(struct comedi_device *cdev,
			  struct comedi_devconfig *it)
{
	int i;
	struct vmk80xx_usb *dev;
	int n_subd;
	struct comedi_subdevice *s;
	int minor;

	dbgvm("vmk80xx: %s\n", __func__);

	mutex_lock(&glb_mutex);

	for (i = 0; i < VMK80XX_MAX_BOARDS; i++)
		if (vmb[i].probed && !vmb[i].attached)
			break;

	if (i == VMK80XX_MAX_BOARDS) {
		mutex_unlock(&glb_mutex);
		return -ENODEV;
	}

	dev = &vmb[i];

	down(&dev->limit_sem);

	cdev->board_name = dev->board.name;
	cdev->private = dev;

	if (dev->board.model == VMK8055_MODEL)
		n_subd = 5;
	else
		n_subd = 6;

	if (alloc_subdevices(cdev, n_subd) < 0) {
		up(&dev->limit_sem);
		mutex_unlock(&glb_mutex);
		return -ENOMEM;
	}

	/* Analog input subdevice */
	s = cdev->subdevices + VMK80XX_SUBD_AI;
	s->type = COMEDI_SUBD_AI;
	s->subdev_flags = SDF_READABLE | SDF_GROUND;
	s->n_chan = dev->board.ai_chans;
	s->maxdata = (1 << dev->board.ai_bits) - 1;
	s->range_table = dev->board.range;
	s->insn_read = vmk80xx_ai_rinsn;

	/* Analog output subdevice */
	s = cdev->subdevices + VMK80XX_SUBD_AO;
	s->type = COMEDI_SUBD_AO;
	s->subdev_flags = SDF_WRITEABLE | SDF_GROUND;
	s->n_chan = dev->board.ao_chans;
	s->maxdata = (1 << dev->board.ao_bits) - 1;
	s->range_table = dev->board.range;
	s->insn_write = vmk80xx_ao_winsn;

	if (dev->board.model == VMK8061_MODEL) {
		s->subdev_flags |= SDF_READABLE;
		s->insn_read = vmk80xx_ao_rinsn;
	}

	/* Digital input subdevice */
	s = cdev->subdevices + VMK80XX_SUBD_DI;
	s->type = COMEDI_SUBD_DI;
	s->subdev_flags = SDF_READABLE | SDF_GROUND;
	s->n_chan = dev->board.di_chans;
	s->maxdata = 1;
	s->insn_read = vmk80xx_di_rinsn;
	s->insn_bits = vmk80xx_di_bits;

	/* Digital output subdevice */
	s = cdev->subdevices + VMK80XX_SUBD_DO;
	s->type = COMEDI_SUBD_DO;
	s->subdev_flags = SDF_WRITEABLE | SDF_GROUND;
	s->n_chan = dev->board.do_chans;
	s->maxdata = 1;
	s->insn_write = vmk80xx_do_winsn;
	s->insn_bits = vmk80xx_do_bits;

	if (dev->board.model == VMK8061_MODEL) {
		s->subdev_flags |= SDF_READABLE;
		s->insn_read = vmk80xx_do_rinsn;
	}

	/* Counter subdevice */
	s = cdev->subdevices + VMK80XX_SUBD_CNT;
	s->type = COMEDI_SUBD_COUNTER;
	s->subdev_flags = SDF_READABLE;
	s->n_chan = dev->board.cnt_chans;
	s->insn_read = vmk80xx_cnt_rinsn;
	s->insn_config = vmk80xx_cnt_cinsn;

	if (dev->board.model == VMK8055_MODEL) {
		s->subdev_flags |= SDF_WRITEABLE;
		s->maxdata = (1 << dev->board.cnt_bits) - 1;
		s->insn_write = vmk80xx_cnt_winsn;
	}

	/* PWM subdevice */
	if (dev->board.model == VMK8061_MODEL) {
		s = cdev->subdevices + VMK80XX_SUBD_PWM;
		s->type = COMEDI_SUBD_PWM;
		s->subdev_flags = SDF_READABLE | SDF_WRITEABLE;
		s->n_chan = dev->board.pwm_chans;
		s->maxdata = (1 << dev->board.pwm_bits) - 1;
		s->insn_read = vmk80xx_pwm_rinsn;
		s->insn_write = vmk80xx_pwm_winsn;
	}

	dev->attached = 1;

	minor = cdev->minor;

	printk(KERN_INFO
	       "comedi%d: vmk80xx: board #%d [%s] attached to comedi\n",
	       minor, dev->count, dev->board.name);

	up(&dev->limit_sem);
	mutex_unlock(&glb_mutex);

	return 0;
}

static int vmk80xx_detach(struct comedi_device *cdev)
{
	struct vmk80xx_usb *dev;
	int minor;

	dbgvm("vmk80xx: %s\n", __func__);

	if (!cdev)
		return -EFAULT;

	dev = cdev->private;
	if (!dev)
		return -EFAULT;

	down(&dev->limit_sem);

	cdev->private = NULL;
	dev->attached = 0;

	minor = cdev->minor;

	printk(KERN_INFO
	       "comedi%d: vmk80xx: board #%d [%s] detached from comedi\n",
	       minor, dev->count, dev->board.name);

	up(&dev->limit_sem);

	return 0;
}

static int vmk80xx_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
	int i;
	struct vmk80xx_usb *dev;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *ep_desc;
	size_t size;

	dbgvm("vmk80xx: %s\n", __func__);

	mutex_lock(&glb_mutex);

	for (i = 0; i < VMK80XX_MAX_BOARDS; i++)
		if (!vmb[i].probed)
			break;

	if (i == VMK80XX_MAX_BOARDS) {
		mutex_unlock(&glb_mutex);
		return -EMFILE;
	}

	dev = &vmb[i];

	memset(dev, 0x00, sizeof(struct vmk80xx_usb));
	dev->count = i;

	iface_desc = intf->cur_altsetting;
	if (iface_desc->desc.bNumEndpoints != 2)
		goto error;

	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
		ep_desc = &iface_desc->endpoint[i].desc;

		if (usb_endpoint_is_int_in(ep_desc)) {
			dev->ep_rx = ep_desc;
			continue;
		}

		if (usb_endpoint_is_int_out(ep_desc)) {
			dev->ep_tx = ep_desc;
			continue;
		}

		if (usb_endpoint_is_bulk_in(ep_desc)) {
			dev->ep_rx = ep_desc;
			continue;
		}

		if (usb_endpoint_is_bulk_out(ep_desc)) {
			dev->ep_tx = ep_desc;
			continue;
		}
	}

	if (!dev->ep_rx || !dev->ep_tx)
		goto error;

	size = le16_to_cpu(dev->ep_rx->wMaxPacketSize);
	dev->usb_rx_buf = kmalloc(size, GFP_KERNEL);
	if (!dev->usb_rx_buf) {
		mutex_unlock(&glb_mutex);
		return -ENOMEM;
	}

	size = le16_to_cpu(dev->ep_tx->wMaxPacketSize);
	dev->usb_tx_buf = kmalloc(size, GFP_KERNEL);
	if (!dev->usb_tx_buf) {
		kfree(dev->usb_rx_buf);
		mutex_unlock(&glb_mutex);
		return -ENOMEM;
	}

	dev->udev = interface_to_usbdev(intf);
	dev->intf = intf;

	sema_init(&dev->limit_sem, 8);
	init_waitqueue_head(&dev->read_wait);
	init_waitqueue_head(&dev->write_wait);

	init_usb_anchor(&dev->rx_anchor);
	init_usb_anchor(&dev->tx_anchor);

	usb_set_intfdata(intf, dev);

	switch (id->driver_info) {
	case DEVICE_VMK8055:
		dev->board.name = "K8055 (VM110)";
		dev->board.model = VMK8055_MODEL;
		dev->board.range = &vmk8055_range;
		dev->board.ai_chans = 2;
		dev->board.ai_bits = 8;
		dev->board.ao_chans = 2;
		dev->board.ao_bits = 8;
		dev->board.di_chans = 5;
		dev->board.di_bits = 1;
		dev->board.do_chans = 8;
		dev->board.do_bits = 1;
		dev->board.cnt_chans = 2;
		dev->board.cnt_bits = 16;
		dev->board.pwm_chans = 0;
		dev->board.pwm_bits = 0;
		break;
	case DEVICE_VMK8061:
		dev->board.name = "K8061 (VM140)";
		dev->board.model = VMK8061_MODEL;
		dev->board.range = &vmk8061_range;
		dev->board.ai_chans = 8;
		dev->board.ai_bits = 10;
		dev->board.ao_chans = 8;
		dev->board.ao_bits = 8;
		dev->board.di_chans = 8;
		dev->board.di_bits = 1;
		dev->board.do_chans = 8;
		dev->board.do_bits = 1;
		dev->board.cnt_chans = 2;
		dev->board.cnt_bits = 0;
		dev->board.pwm_chans = 1;
		dev->board.pwm_bits = 10;
		break;
	}

	if (dev->board.model == VMK8061_MODEL) {
		vmk80xx_read_eeprom(dev, IC3_VERSION);
		printk(KERN_INFO "comedi#: vmk80xx: %s\n", dev->fw.ic3_vers);

		if (vmk80xx_check_data_link(dev)) {
			vmk80xx_read_eeprom(dev, IC6_VERSION);
			printk(KERN_INFO "comedi#: vmk80xx: %s\n",
			       dev->fw.ic6_vers);
		} else {
			dbgcm("comedi#: vmk80xx: no conn. to CPU\n");
		}
	}

	if (dev->board.model == VMK8055_MODEL)
		vmk80xx_reset_device(dev);

	dev->probed = 1;

	printk(KERN_INFO "comedi#: vmk80xx: board #%d [%s] now attached\n",
	       dev->count, dev->board.name);

	mutex_unlock(&glb_mutex);

	comedi_usb_auto_config(dev->udev, BOARDNAME);

	return 0;
error:
	mutex_unlock(&glb_mutex);

	return -ENODEV;
}

static void vmk80xx_disconnect(struct usb_interface *intf)
{
	struct vmk80xx_usb *dev = usb_get_intfdata(intf);

	dbgvm("vmk80xx: %s\n", __func__);

	if (!dev)
		return;

	comedi_usb_auto_unconfig(dev->udev);

	mutex_lock(&glb_mutex);
	down(&dev->limit_sem);

	dev->probed = 0;
	usb_set_intfdata(dev->intf, NULL);

	usb_kill_anchored_urbs(&dev->rx_anchor);
	usb_kill_anchored_urbs(&dev->tx_anchor);

	kfree(dev->usb_rx_buf);
	kfree(dev->usb_tx_buf);

	printk(KERN_INFO "comedi#: vmk80xx: board #%d [%s] now detached\n",
	       dev->count, dev->board.name);

	up(&dev->limit_sem);
	mutex_unlock(&glb_mutex);
}

/* TODO: Add support for suspend, resume, pre_reset,
 * post_reset and flush */
static struct usb_driver vmk80xx_driver = {
	.name = "vmk80xx",
	.probe = vmk80xx_probe,
	.disconnect = vmk80xx_disconnect,
	.id_table = vmk80xx_id_table
};

static struct comedi_driver driver_vmk80xx = {
	.module = THIS_MODULE,
	.driver_name = "vmk80xx",
	.attach = vmk80xx_attach,
	.detach = vmk80xx_detach
};

static int __init vmk80xx_init(void)
{
	int retval;

	printk(KERN_INFO "vmk80xx: version 0.8.01 "
	       "Manuel Gebele <forensixs@gmx.de>\n");

	retval = comedi_driver_register(&driver_vmk80xx);
	if (retval < 0)
		return retval;

	return usb_register(&vmk80xx_driver);
}

static void __exit vmk80xx_exit(void)
{
	comedi_driver_unregister(&driver_vmk80xx);
	usb_deregister(&vmk80xx_driver);
}

module_init(vmk80xx_init);
module_exit(vmk80xx_exit);