staging: comedi: dt9812: pass the comedi_device * to dt9812_digital_in()
[deliverable/linux.git] / drivers / staging / comedi / drivers / dt9812.c
CommitLineData
63274cd7
AB
1/*
2 * comedi/drivers/dt9812.c
3 * COMEDI driver for DataTranslation DT9812 USB module
4 *
5 * Copyright (C) 2005 Anders Blomdell <anders.blomdell@control.lth.se>
6 *
7 * COMEDI - Linux Control and Measurement Device Interface
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
63274cd7
AB
18 */
19
20/*
21Driver: dt9812
22Description: Data Translation DT9812 USB module
23Author: anders.blomdell@control.lth.se (Anders Blomdell)
24Status: in development
25Devices: [Data Translation] DT9812 (dt9812)
26Updated: Sun Nov 20 20:18:34 EST 2005
27
28This driver works, but bulk transfers not implemented. Might be a starting point
29for someone else. I found out too late that USB has too high latencies (>1 ms)
30for my needs.
31*/
32
33/*
34 * Nota Bene:
35 * 1. All writes to command pipe has to be 32 bytes (ISP1181B SHRTP=0 ?)
36 * 2. The DDK source (as of sep 2005) is in error regarding the
37 * input MUX bits (example code says P4, but firmware schematics
38 * says P1).
39 */
40
509f3f78
YT
41#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
63274cd7
AB
43#include <linux/kernel.h>
44#include <linux/errno.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/module.h>
48#include <linux/kref.h>
f52a8af7 49#include <linux/uaccess.h>
63274cd7 50#include <linux/usb.h>
f52a8af7 51
63274cd7 52#include "../comedidev.h"
c73190fa
GKH
53
54#define DT9812_DIAGS_BOARD_INFO_ADDR 0xFBFF
55#define DT9812_MAX_WRITE_CMD_PIPE_SIZE 32
56#define DT9812_MAX_READ_CMD_PIPE_SIZE 32
57
58/*
59 * See Silican Laboratories C8051F020/1/2/3 manual
60 */
61#define F020_SFR_P4 0x84
62#define F020_SFR_P1 0x90
63#define F020_SFR_P2 0xa0
64#define F020_SFR_P3 0xb0
65#define F020_SFR_AMX0CF 0xba
66#define F020_SFR_AMX0SL 0xbb
67#define F020_SFR_ADC0CF 0xbc
68#define F020_SFR_ADC0L 0xbe
69#define F020_SFR_ADC0H 0xbf
70#define F020_SFR_DAC0L 0xd2
71#define F020_SFR_DAC0H 0xd3
72#define F020_SFR_DAC0CN 0xd4
73#define F020_SFR_DAC1L 0xd5
74#define F020_SFR_DAC1H 0xd6
75#define F020_SFR_DAC1CN 0xd7
76#define F020_SFR_ADC0CN 0xe8
77
78#define F020_MASK_ADC0CF_AMP0GN0 0x01
79#define F020_MASK_ADC0CF_AMP0GN1 0x02
80#define F020_MASK_ADC0CF_AMP0GN2 0x04
81
82#define F020_MASK_ADC0CN_AD0EN 0x80
83#define F020_MASK_ADC0CN_AD0INT 0x20
84#define F020_MASK_ADC0CN_AD0BUSY 0x10
85
86#define F020_MASK_DACxCN_DACxEN 0x80
87
98f3c672 88enum {
0a85b6f0 89 /* A/D D/A DI DO CT */
c73190fa 90 DT9812_DEVID_DT9812_10, /* 8 2 8 8 1 +/- 10V */
0a85b6f0 91 DT9812_DEVID_DT9812_2PT5, /* 8 2 8 8 1 0-2.44V */
c73190fa
GKH
92#if 0
93 DT9812_DEVID_DT9813, /* 16 2 4 4 1 +/- 10V */
94 DT9812_DEVID_DT9814 /* 24 2 0 0 1 +/- 10V */
95#endif
98f3c672 96};
c73190fa 97
98f3c672 98enum dt9812_gain {
c73190fa
GKH
99 DT9812_GAIN_0PT25 = 1,
100 DT9812_GAIN_0PT5 = 2,
101 DT9812_GAIN_1 = 4,
102 DT9812_GAIN_2 = 8,
103 DT9812_GAIN_4 = 16,
104 DT9812_GAIN_8 = 32,
105 DT9812_GAIN_16 = 64,
98f3c672 106};
c73190fa 107
98f3c672 108enum {
c73190fa
GKH
109 DT9812_LEAST_USB_FIRMWARE_CMD_CODE = 0,
110 /* Write Flash memory */
111 DT9812_W_FLASH_DATA = 0,
112 /* Read Flash memory misc config info */
113 DT9812_R_FLASH_DATA = 1,
114
115 /*
116 * Register read/write commands for processor
117 */
118
119 /* Read a single byte of USB memory */
120 DT9812_R_SINGLE_BYTE_REG = 2,
121 /* Write a single byte of USB memory */
122 DT9812_W_SINGLE_BYTE_REG = 3,
123 /* Multiple Reads of USB memory */
124 DT9812_R_MULTI_BYTE_REG = 4,
125 /* Multiple Writes of USB memory */
126 DT9812_W_MULTI_BYTE_REG = 5,
127 /* Read, (AND) with mask, OR value, then write (single) */
128 DT9812_RMW_SINGLE_BYTE_REG = 6,
129 /* Read, (AND) with mask, OR value, then write (multiple) */
130 DT9812_RMW_MULTI_BYTE_REG = 7,
131
132 /*
133 * Register read/write commands for SMBus
134 */
135
136 /* Read a single byte of SMBus */
137 DT9812_R_SINGLE_BYTE_SMBUS = 8,
138 /* Write a single byte of SMBus */
139 DT9812_W_SINGLE_BYTE_SMBUS = 9,
140 /* Multiple Reads of SMBus */
141 DT9812_R_MULTI_BYTE_SMBUS = 10,
142 /* Multiple Writes of SMBus */
143 DT9812_W_MULTI_BYTE_SMBUS = 11,
144
145 /*
146 * Register read/write commands for a device
147 */
148
149 /* Read a single byte of a device */
150 DT9812_R_SINGLE_BYTE_DEV = 12,
151 /* Write a single byte of a device */
152 DT9812_W_SINGLE_BYTE_DEV = 13,
153 /* Multiple Reads of a device */
154 DT9812_R_MULTI_BYTE_DEV = 14,
155 /* Multiple Writes of a device */
156 DT9812_W_MULTI_BYTE_DEV = 15,
157
158 /* Not sure if we'll need this */
159 DT9812_W_DAC_THRESHOLD = 16,
160
161 /* Set interrupt on change mask */
162 DT9812_W_INT_ON_CHANGE_MASK = 17,
163
164 /* Write (or Clear) the CGL for the ADC */
165 DT9812_W_CGL = 18,
166 /* Multiple Reads of USB memory */
167 DT9812_R_MULTI_BYTE_USBMEM = 19,
168 /* Multiple Writes to USB memory */
169 DT9812_W_MULTI_BYTE_USBMEM = 20,
170
171 /* Issue a start command to a given subsystem */
172 DT9812_START_SUBSYSTEM = 21,
173 /* Issue a stop command to a given subsystem */
174 DT9812_STOP_SUBSYSTEM = 22,
175
176 /* calibrate the board using CAL_POT_CMD */
177 DT9812_CALIBRATE_POT = 23,
178 /* set the DAC FIFO size */
179 DT9812_W_DAC_FIFO_SIZE = 24,
180 /* Write or Clear the CGL for the DAC */
181 DT9812_W_CGL_DAC = 25,
182 /* Read a single value from a subsystem */
183 DT9812_R_SINGLE_VALUE_CMD = 26,
184 /* Write a single value to a subsystem */
185 DT9812_W_SINGLE_VALUE_CMD = 27,
186 /* Valid DT9812_USB_FIRMWARE_CMD_CODE's will be less than this number */
187 DT9812_MAX_USB_FIRMWARE_CMD_CODE,
98f3c672 188};
c73190fa 189
98f3c672 190struct dt9812_flash_data {
c73190fa
GKH
191 u16 numbytes;
192 u16 address;
98f3c672 193};
c73190fa
GKH
194
195#define DT9812_MAX_NUM_MULTI_BYTE_RDS \
65c5cfa6 196 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
c73190fa 197
98f3c672 198struct dt9812_read_multi {
c73190fa
GKH
199 u8 count;
200 u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
98f3c672 201};
c73190fa 202
98f3c672 203struct dt9812_write_byte {
c73190fa
GKH
204 u8 address;
205 u8 value;
98f3c672 206};
c73190fa
GKH
207
208#define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
65c5cfa6
RKM
209 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
210 sizeof(struct dt9812_write_byte))
c73190fa 211
98f3c672 212struct dt9812_write_multi {
c73190fa 213 u8 count;
98f3c672
GKH
214 struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
215};
c73190fa 216
98f3c672 217struct dt9812_rmw_byte {
c73190fa
GKH
218 u8 address;
219 u8 and_mask;
220 u8 or_value;
98f3c672 221};
c73190fa
GKH
222
223#define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
65c5cfa6
RKM
224 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
225 sizeof(struct dt9812_rmw_byte))
c73190fa 226
98f3c672 227struct dt9812_rmw_multi {
c73190fa 228 u8 count;
98f3c672
GKH
229 struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
230};
c73190fa 231
98f3c672 232struct dt9812_usb_cmd {
c73190fa
GKH
233 u32 cmd;
234 union {
98f3c672
GKH
235 struct dt9812_flash_data flash_data_info;
236 struct dt9812_read_multi read_multi_info;
237 struct dt9812_write_multi write_multi_info;
238 struct dt9812_rmw_multi rmw_multi_info;
c73190fa
GKH
239 } u;
240#if 0
241 WRITE_BYTE_INFO WriteByteInfo;
242 READ_BYTE_INFO ReadByteInfo;
243 WRITE_MULTI_INFO WriteMultiInfo;
244 READ_MULTI_INFO ReadMultiInfo;
245 RMW_BYTE_INFO RMWByteInfo;
246 RMW_MULTI_INFO RMWMultiInfo;
247 DAC_THRESHOLD_INFO DacThresholdInfo;
248 INT_ON_CHANGE_MASK_INFO IntOnChangeMaskInfo;
249 CGL_INFO CglInfo;
250 SUBSYSTEM_INFO SubsystemInfo;
251 CAL_POT_CMD CalPotCmd;
252 WRITE_DEV_BYTE_INFO WriteDevByteInfo;
253 READ_DEV_BYTE_INFO ReadDevByteInfo;
254 WRITE_DEV_MULTI_INFO WriteDevMultiInfo;
255 READ_DEV_MULTI_INFO ReadDevMultiInfo;
256 READ_SINGLE_VALUE_INFO ReadSingleValueInfo;
257 WRITE_SINGLE_VALUE_INFO WriteSingleValueInfo;
258#endif
98f3c672 259};
63274cd7 260
f52a8af7 261#define DT9812_NUM_SLOTS 16
63274cd7 262
45f4d024 263static DEFINE_SEMAPHORE(dt9812_mutex);
63274cd7 264
98f3c672 265struct usb_dt9812 {
63274cd7
AB
266 struct slot_dt9812 *slot;
267 struct usb_device *udev;
268 struct usb_interface *interface;
269 u16 vendor;
270 u16 product;
271 u16 device;
272 u32 serial;
273 struct {
274 __u8 addr;
275 size_t size;
276 } message_pipe, command_write, command_read, write_stream, read_stream;
277 struct kref kref;
278 u16 analog_out_shadow[2];
279 u8 digital_out_shadow;
98f3c672 280};
63274cd7 281
415227c1 282struct dt9812_private {
63274cd7 283 struct slot_dt9812 *slot;
98f3c672 284};
63274cd7 285
98f3c672 286struct slot_dt9812 {
63274cd7 287 struct semaphore mutex;
98f3c672 288 struct usb_dt9812 *usb;
98c83cfa 289 struct dt9812_private *devpriv;
98f3c672 290};
63274cd7 291
98f3c672 292static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
63274cd7 293
98f3c672 294static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
63274cd7 295{
98f3c672 296 return container_of(d, struct usb_dt9812, kref);
63274cd7
AB
297}
298
299static void dt9812_delete(struct kref *kref)
300{
98f3c672 301 struct usb_dt9812 *dev = to_dt9812_dev(kref);
63274cd7
AB
302
303 usb_put_dev(dev->udev);
304 kfree(dev);
305}
306
98f3c672 307static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
f52a8af7 308 size_t buf_size)
63274cd7 309{
0ec77d34 310 struct usb_device *usb = dev->udev;
98f3c672 311 struct dt9812_usb_cmd cmd;
0ec77d34 312 int count, ret;
63274cd7
AB
313
314 cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA);
315 cmd.u.flash_data_info.address =
0a85b6f0 316 cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset);
63274cd7
AB
317 cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
318
f52a8af7 319 /* DT9812 only responds to 32 byte writes!! */
0ec77d34
HS
320 ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, dev->command_write.addr),
321 &cmd, 32, &count, HZ * 1);
322 if (ret)
323 return ret;
324
325 return usb_bulk_msg(usb, usb_rcvbulkpipe(usb, dev->command_read.addr),
326 buf, buf_size, &count, HZ * 1);
63274cd7
AB
327}
328
98f3c672 329static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
a7a55d4a 330 u8 *address, u8 *value)
63274cd7 331{
0ec77d34 332 struct usb_device *usb = dev->udev;
98f3c672 333 struct dt9812_usb_cmd cmd;
0ec77d34 334 int i, count, ret;
63274cd7
AB
335
336 cmd.cmd = cpu_to_le32(DT9812_R_MULTI_BYTE_REG);
337 cmd.u.read_multi_info.count = reg_count;
f52a8af7 338 for (i = 0; i < reg_count; i++)
63274cd7 339 cmd.u.read_multi_info.address[i] = address[i];
f52a8af7
GKH
340
341 /* DT9812 only responds to 32 byte writes!! */
0ec77d34
HS
342 ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, dev->command_write.addr),
343 &cmd, 32, &count, HZ * 1);
344 if (ret)
345 return ret;
346
347 return usb_bulk_msg(usb, usb_rcvbulkpipe(usb, dev->command_read.addr),
348 value, reg_count, &count, HZ * 1);
63274cd7
AB
349}
350
98f3c672 351static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
a7a55d4a
RKM
352 int reg_count, u8 *address,
353 u8 *value)
63274cd7 354{
0ec77d34 355 struct usb_device *usb = dev->udev;
98f3c672 356 struct dt9812_usb_cmd cmd;
0ec77d34 357 int i, count;
63274cd7
AB
358
359 cmd.cmd = cpu_to_le32(DT9812_W_MULTI_BYTE_REG);
360 cmd.u.read_multi_info.count = reg_count;
361 for (i = 0; i < reg_count; i++) {
362 cmd.u.write_multi_info.write[i].address = address[i];
363 cmd.u.write_multi_info.write[i].value = value[i];
364 }
0ec77d34 365
f52a8af7 366 /* DT9812 only responds to 32 byte writes!! */
0ec77d34
HS
367 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, dev->command_write.addr),
368 &cmd, 32, &count, HZ * 1);
63274cd7
AB
369}
370
98f3c672
GKH
371static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
372 struct dt9812_rmw_byte *rmw)
63274cd7 373{
0ec77d34 374 struct usb_device *usb = dev->udev;
98f3c672 375 struct dt9812_usb_cmd cmd;
0ec77d34 376 int i, count;
63274cd7
AB
377
378 cmd.cmd = cpu_to_le32(DT9812_RMW_MULTI_BYTE_REG);
379 cmd.u.rmw_multi_info.count = reg_count;
f52a8af7 380 for (i = 0; i < reg_count; i++)
63274cd7 381 cmd.u.rmw_multi_info.rmw[i] = rmw[i];
f52a8af7
GKH
382
383 /* DT9812 only responds to 32 byte writes!! */
0ec77d34
HS
384 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, dev->command_write.addr),
385 &cmd, 32, &count, HZ * 1);
63274cd7
AB
386}
387
4bc839f2 388static int dt9812_digital_in(struct comedi_device *dev, u8 *bits)
63274cd7 389{
4bc839f2
HS
390 struct dt9812_private *devpriv = dev->private;
391 struct slot_dt9812 *slot = devpriv->slot;
392 int ret = -ENODEV;
63274cd7
AB
393
394 down(&slot->mutex);
395 if (slot->usb) {
396 u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
397 u8 value[2];
398
4bc839f2
HS
399 ret = dt9812_read_multiple_registers(slot->usb, 2, reg, value);
400 if (ret == 0) {
f52a8af7
GKH
401 /*
402 * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
403 * input port bit 3 in F020_SFR_P1 is bit 7 in the
404 * digital input port
405 */
63274cd7 406 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
63274cd7
AB
407 }
408 }
409 up(&slot->mutex);
410
4bc839f2 411 return ret;
63274cd7
AB
412}
413
98f3c672 414static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
63274cd7
AB
415{
416 int result = -ENODEV;
417
418 down(&slot->mutex);
419 if (slot->usb) {
420 u8 reg[1];
421 u8 value[1];
422
423 reg[0] = F020_SFR_P2;
424 value[0] = bits;
425 result = dt9812_write_multiple_registers(slot->usb, 1, reg,
f52a8af7 426 value);
63274cd7
AB
427 slot->usb->digital_out_shadow = bits;
428 }
429 up(&slot->mutex);
430 return result;
431}
432
a7a55d4a 433static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 *bits)
63274cd7
AB
434{
435 int result = -ENODEV;
436
437 down(&slot->mutex);
438 if (slot->usb) {
439 *bits = slot->usb->digital_out_shadow;
440 result = 0;
441 }
442 up(&slot->mutex);
443 return result;
444}
445
98f3c672
GKH
446static void dt9812_configure_mux(struct usb_dt9812 *dev,
447 struct dt9812_rmw_byte *rmw, int channel)
63274cd7
AB
448{
449 if (dev->device == DT9812_DEVID_DT9812_10) {
f52a8af7 450 /* In the DT9812/10V MUX is selected by P1.5-7 */
63274cd7
AB
451 rmw->address = F020_SFR_P1;
452 rmw->and_mask = 0xe0;
453 rmw->or_value = channel << 5;
454 } else {
f52a8af7 455 /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
63274cd7
AB
456 rmw->address = F020_SFR_AMX0SL;
457 rmw->and_mask = 0xff;
458 rmw->or_value = channel & 0x07;
459 }
460}
461
98f3c672
GKH
462static void dt9812_configure_gain(struct usb_dt9812 *dev,
463 struct dt9812_rmw_byte *rmw,
464 enum dt9812_gain gain)
63274cd7
AB
465{
466 if (dev->device == DT9812_DEVID_DT9812_10) {
f52a8af7 467 /* In the DT9812/10V, there is an external gain of 0.5 */
63274cd7
AB
468 gain <<= 1;
469 }
470
471 rmw->address = F020_SFR_ADC0CF;
f52a8af7 472 rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
0a85b6f0 473 F020_MASK_ADC0CF_AMP0GN1 | F020_MASK_ADC0CF_AMP0GN0;
63274cd7 474 switch (gain) {
f52a8af7
GKH
475 /*
476 * 000 -> Gain = 1
477 * 001 -> Gain = 2
478 * 010 -> Gain = 4
479 * 011 -> Gain = 8
480 * 10x -> Gain = 16
481 * 11x -> Gain = 0.5
482 */
483 case DT9812_GAIN_0PT5:
cba2c993 484 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 |
0a85b6f0 485 F020_MASK_ADC0CF_AMP0GN1;
63274cd7 486 break;
f52a8af7
GKH
487 case DT9812_GAIN_1:
488 rmw->or_value = 0x00;
63274cd7 489 break;
f52a8af7
GKH
490 case DT9812_GAIN_2:
491 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
63274cd7 492 break;
f52a8af7
GKH
493 case DT9812_GAIN_4:
494 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
63274cd7 495 break;
f52a8af7 496 case DT9812_GAIN_8:
cba2c993 497 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 |
0a85b6f0 498 F020_MASK_ADC0CF_AMP0GN0;
63274cd7 499 break;
f52a8af7
GKH
500 case DT9812_GAIN_16:
501 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
63274cd7 502 break;
f52a8af7 503 default:
923faa6a 504 dev_err(&dev->interface->dev, "Illegal gain %d\n", gain);
f52a8af7 505
63274cd7
AB
506 }
507}
508
a7a55d4a 509static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 *value,
98f3c672 510 enum dt9812_gain gain)
63274cd7 511{
98f3c672 512 struct dt9812_rmw_byte rmw[3];
f52a8af7
GKH
513 u8 reg[3] = {
514 F020_SFR_ADC0CN,
515 F020_SFR_ADC0H,
516 F020_SFR_ADC0L
517 };
518 u8 val[3];
63274cd7
AB
519 int result = -ENODEV;
520
521 down(&slot->mutex);
f52a8af7
GKH
522 if (!slot->usb)
523 goto exit;
524
525 /* 1 select the gain */
526 dt9812_configure_gain(slot->usb, &rmw[0], gain);
527
528 /* 2 set the MUX to select the channel */
529 dt9812_configure_mux(slot->usb, &rmw[1], channel);
530
531 /* 3 start conversion */
532 rmw[2].address = F020_SFR_ADC0CN;
533 rmw[2].and_mask = 0xff;
534 rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
535
536 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
537 if (result)
538 goto exit;
539
540 /* read the status and ADC */
541 result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
542 if (result)
543 goto exit;
544 /*
545 * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
546 * Therefore, between the instant that AD0BUSY was set via
547 * dt9812_rmw_multiple_registers and the read of AD0BUSY via
548 * dt9812_read_multiple_registers, the conversion should be complete
549 * since these two operations require two USB transactions each taking
550 * at least a millisecond to complete. However, lets make sure that
551 * conversion is finished.
552 */
553 if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
554 F020_MASK_ADC0CN_AD0INT) {
555 switch (slot->usb->device) {
556 case DT9812_DEVID_DT9812_10:
557 /*
558 * For DT9812-10V the personality module set the
559 * encoding to 2's complement. Hence, convert it before
560 * returning it
561 */
562 *value = ((val[1] << 8) | val[2]) + 0x800;
563 break;
564 case DT9812_DEVID_DT9812_2PT5:
565 *value = (val[1] << 8) | val[2];
566 break;
63274cd7
AB
567 }
568 }
f52a8af7
GKH
569
570exit:
63274cd7
AB
571 up(&slot->mutex);
572 return result;
573}
574
98f3c672 575static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
a7a55d4a 576 u16 *value)
63274cd7
AB
577{
578 int result = -ENODEV;
579
580 down(&slot->mutex);
581 if (slot->usb) {
582 *value = slot->usb->analog_out_shadow[channel];
583 result = 0;
584 }
585 up(&slot->mutex);
586
587 return result;
588}
589
98f3c672 590static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
63274cd7
AB
591{
592 int result = -ENODEV;
593
594 down(&slot->mutex);
595 if (slot->usb) {
98f3c672 596 struct dt9812_rmw_byte rmw[3];
63274cd7
AB
597
598 switch (channel) {
f52a8af7
GKH
599 case 0:
600 /* 1. Set DAC mode */
601 rmw[0].address = F020_SFR_DAC0CN;
602 rmw[0].and_mask = 0xff;
603 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
604
605 /* 2 load low byte of DAC value first */
606 rmw[1].address = F020_SFR_DAC0L;
607 rmw[1].and_mask = 0xff;
608 rmw[1].or_value = value & 0xff;
609
610 /* 3 load high byte of DAC value next to latch the
611 12-bit value */
612 rmw[2].address = F020_SFR_DAC0H;
613 rmw[2].and_mask = 0xff;
614 rmw[2].or_value = (value >> 8) & 0xf;
63274cd7 615 break;
f52a8af7
GKH
616
617 case 1:
618 /* 1. Set DAC mode */
619 rmw[0].address = F020_SFR_DAC1CN;
620 rmw[0].and_mask = 0xff;
621 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
622
623 /* 2 load low byte of DAC value first */
624 rmw[1].address = F020_SFR_DAC1L;
625 rmw[1].and_mask = 0xff;
626 rmw[1].or_value = value & 0xff;
627
628 /* 3 load high byte of DAC value next to latch the
629 12-bit value */
630 rmw[2].address = F020_SFR_DAC1H;
631 rmw[2].and_mask = 0xff;
632 rmw[2].or_value = (value >> 8) & 0xf;
63274cd7
AB
633 break;
634 }
635 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
636 slot->usb->analog_out_shadow[channel] = value;
637 }
638 up(&slot->mutex);
639
640 return result;
641}
642
0a85b6f0
MT
643static int dt9812_di_rinsn(struct comedi_device *dev,
644 struct comedi_subdevice *s, struct comedi_insn *insn,
645 unsigned int *data)
63274cd7 646{
564c526a 647 unsigned int channel = CR_CHAN(insn->chanspec);
63274cd7
AB
648 int n;
649 u8 bits = 0;
650
4bc839f2 651 dt9812_digital_in(dev, &bits);
f52a8af7 652 for (n = 0; n < insn->n; n++)
564c526a 653 data[n] = ((1 << channel) & bits) != 0;
63274cd7
AB
654 return n;
655}
656
0a85b6f0
MT
657static int dt9812_do_winsn(struct comedi_device *dev,
658 struct comedi_subdevice *s, struct comedi_insn *insn,
659 unsigned int *data)
63274cd7 660{
415227c1 661 struct dt9812_private *devpriv = dev->private;
564c526a 662 unsigned int channel = CR_CHAN(insn->chanspec);
63274cd7
AB
663 int n;
664 u8 bits = 0;
665
666 dt9812_digital_out_shadow(devpriv->slot, &bits);
667 for (n = 0; n < insn->n; n++) {
564c526a 668 u8 mask = 1 << channel;
63274cd7
AB
669
670 bits &= ~mask;
f52a8af7 671 if (data[n])
63274cd7 672 bits |= mask;
63274cd7
AB
673 }
674 dt9812_digital_out(devpriv->slot, bits);
675 return n;
676}
677
0a85b6f0
MT
678static int dt9812_ai_rinsn(struct comedi_device *dev,
679 struct comedi_subdevice *s, struct comedi_insn *insn,
680 unsigned int *data)
63274cd7 681{
415227c1 682 struct dt9812_private *devpriv = dev->private;
564c526a 683 unsigned int channel = CR_CHAN(insn->chanspec);
63274cd7
AB
684 int n;
685
686 for (n = 0; n < insn->n; n++) {
687 u16 value = 0;
688
564c526a 689 dt9812_analog_in(devpriv->slot, channel, &value, DT9812_GAIN_1);
63274cd7
AB
690 data[n] = value;
691 }
692 return n;
693}
694
0a85b6f0
MT
695static int dt9812_ao_rinsn(struct comedi_device *dev,
696 struct comedi_subdevice *s, struct comedi_insn *insn,
697 unsigned int *data)
63274cd7 698{
415227c1 699 struct dt9812_private *devpriv = dev->private;
564c526a 700 unsigned int channel = CR_CHAN(insn->chanspec);
63274cd7 701 int n;
f52a8af7 702 u16 value;
63274cd7
AB
703
704 for (n = 0; n < insn->n; n++) {
f52a8af7 705 value = 0;
564c526a 706 dt9812_analog_out_shadow(devpriv->slot, channel, &value);
63274cd7
AB
707 data[n] = value;
708 }
709 return n;
710}
711
0a85b6f0
MT
712static int dt9812_ao_winsn(struct comedi_device *dev,
713 struct comedi_subdevice *s, struct comedi_insn *insn,
714 unsigned int *data)
63274cd7 715{
415227c1 716 struct dt9812_private *devpriv = dev->private;
564c526a 717 unsigned int channel = CR_CHAN(insn->chanspec);
63274cd7
AB
718 int n;
719
f52a8af7 720 for (n = 0; n < insn->n; n++)
564c526a 721 dt9812_analog_out(devpriv->slot, channel, data[n]);
63274cd7
AB
722 return n;
723}
724
0707bb04 725static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
63274cd7 726{
1415003f 727 struct slot_dt9812 *slot = NULL;
415227c1 728 struct dt9812_private *devpriv;
63274cd7 729 int i;
34c43922 730 struct comedi_subdevice *s;
1415003f 731 bool range_2_5;
8b6c5694 732 int ret;
63274cd7 733
c34fa261
HS
734 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
735 if (!devpriv)
736 return -ENOMEM;
737 dev->private = devpriv;
f52a8af7 738
1415003f
HS
739 down(&dt9812_mutex);
740
f52a8af7 741 /*
1415003f
HS
742 * Find the first unused slot for the comedi device
743 * that has a usb device connected.
f52a8af7 744 */
1415003f
HS
745 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
746 if (dt9812[i].usb && !dt9812[i].devpriv) {
747 slot = &dt9812[i];
748 break;
749 }
750 }
751 if (!slot) {
752 up(&dt9812_mutex);
753 return -ENODEV;
754 }
63274cd7 755
1415003f
HS
756 down(&slot->mutex);
757 slot->devpriv = devpriv;
758 devpriv->slot = slot;
759 range_2_5 = (slot->usb->device == DT9812_DEVID_DT9812_2PT5);
760 up(&slot->mutex);
761
762 up(&dt9812_mutex);
63274cd7 763
8b6c5694
HS
764 ret = comedi_alloc_subdevices(dev, 4);
765 if (ret)
766 return ret;
63274cd7
AB
767
768 /* digital input subdevice */
3a207891 769 s = &dev->subdevices[0];
63274cd7
AB
770 s->type = COMEDI_SUBD_DI;
771 s->subdev_flags = SDF_READABLE;
1415003f 772 s->n_chan = 8;
63274cd7
AB
773 s->maxdata = 1;
774 s->range_table = &range_digital;
775 s->insn_read = &dt9812_di_rinsn;
776
777 /* digital output subdevice */
3a207891 778 s = &dev->subdevices[1];
63274cd7
AB
779 s->type = COMEDI_SUBD_DO;
780 s->subdev_flags = SDF_WRITEABLE;
1415003f 781 s->n_chan = 8;
63274cd7
AB
782 s->maxdata = 1;
783 s->range_table = &range_digital;
784 s->insn_write = &dt9812_do_winsn;
785
786 /* analog input subdevice */
3a207891 787 s = &dev->subdevices[2];
63274cd7
AB
788 s->type = COMEDI_SUBD_AI;
789 s->subdev_flags = SDF_READABLE | SDF_GROUND;
1415003f
HS
790 s->n_chan = 8;
791 s->maxdata = 4095;
792 s->range_table = range_2_5 ? &range_unipolar2_5 : &range_bipolar10;
63274cd7
AB
793 s->insn_read = &dt9812_ai_rinsn;
794
795 /* analog output subdevice */
3a207891 796 s = &dev->subdevices[3];
63274cd7
AB
797 s->type = COMEDI_SUBD_AO;
798 s->subdev_flags = SDF_WRITEABLE;
799 s->n_chan = 0;
1415003f
HS
800 s->maxdata = 4095;
801 s->range_table = range_2_5 ? &range_unipolar2_5 : &range_bipolar10;
63274cd7
AB
802 s->insn_write = &dt9812_ao_winsn;
803 s->insn_read = &dt9812_ao_rinsn;
804
509f3f78 805 dev_info(dev->class_dev, "successfully attached to dt9812.\n");
63274cd7 806
63274cd7
AB
807 return 0;
808}
809
484ecc95 810static void dt9812_detach(struct comedi_device *dev)
63274cd7 811{
1415003f
HS
812 struct dt9812_private *devpriv = dev->private;
813
814 if (devpriv && devpriv->slot)
815 devpriv->slot = NULL;
63274cd7
AB
816}
817
139dfbdf 818static struct comedi_driver dt9812_comedi_driver = {
63274cd7
AB
819 .module = THIS_MODULE,
820 .driver_name = "dt9812",
821 .attach = dt9812_attach,
822 .detach = dt9812_detach,
823};
824
df785c7a
HS
825static int dt9812_probe(struct usb_interface *interface,
826 const struct usb_device_id *id)
827{
1415003f 828 struct slot_dt9812 *slot = NULL;
df785c7a
HS
829 struct usb_dt9812 *dev = NULL;
830 struct usb_host_interface *iface_desc;
831 struct usb_endpoint_descriptor *endpoint;
1415003f 832 int retval = -ENOMEM;
df785c7a
HS
833 int i;
834 u8 fw;
835
836 /* allocate memory for our device state and initialize it */
837 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
838 if (dev == NULL)
839 goto error;
840
841 kref_init(&dev->kref);
842
1415003f
HS
843 down(&dt9812_mutex);
844
845 /* Find an empty slot for the usb device */
846 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
847 if (!dt9812[i].usb) {
848 slot = &dt9812[i];
849 break;
850 }
851 }
852 if (!slot) {
853 up(&dt9812_mutex);
854 retval = -ENODEV;
855 goto error;
856 }
857
858 down(&slot->mutex);
859 slot->usb = dev;
860 dev->slot = slot;
861 up(&slot->mutex);
862
863 up(&dt9812_mutex);
864
df785c7a
HS
865 dev->udev = usb_get_dev(interface_to_usbdev(interface));
866 dev->interface = interface;
867
868 /* Check endpoints */
869 iface_desc = interface->cur_altsetting;
870
871 if (iface_desc->desc.bNumEndpoints != 5) {
872 dev_err(&interface->dev, "Wrong number of endpoints.\n");
873 retval = -ENODEV;
874 goto error;
875 }
876
877 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
878 int direction = -1;
879 endpoint = &iface_desc->endpoint[i].desc;
880 switch (i) {
881 case 0:
882 direction = USB_DIR_IN;
883 dev->message_pipe.addr = endpoint->bEndpointAddress;
884 dev->message_pipe.size =
885 le16_to_cpu(endpoint->wMaxPacketSize);
886
887 break;
888 case 1:
889 direction = USB_DIR_OUT;
890 dev->command_write.addr = endpoint->bEndpointAddress;
891 dev->command_write.size =
892 le16_to_cpu(endpoint->wMaxPacketSize);
893 break;
894 case 2:
895 direction = USB_DIR_IN;
896 dev->command_read.addr = endpoint->bEndpointAddress;
897 dev->command_read.size =
898 le16_to_cpu(endpoint->wMaxPacketSize);
899 break;
900 case 3:
901 direction = USB_DIR_OUT;
902 dev->write_stream.addr = endpoint->bEndpointAddress;
903 dev->write_stream.size =
904 le16_to_cpu(endpoint->wMaxPacketSize);
905 break;
906 case 4:
907 direction = USB_DIR_IN;
908 dev->read_stream.addr = endpoint->bEndpointAddress;
909 dev->read_stream.size =
910 le16_to_cpu(endpoint->wMaxPacketSize);
911 break;
912 }
913 if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
914 dev_err(&interface->dev,
915 "Endpoint has wrong direction.\n");
916 retval = -ENODEV;
917 goto error;
918 }
919 }
920 if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
921 /*
922 * Seems like a configuration reset is necessary if driver is
923 * reloaded while device is attached
924 */
925 usb_reset_configuration(dev->udev);
926 for (i = 0; i < 10; i++) {
927 retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
928 if (retval == 0) {
929 dev_info(&interface->dev,
930 "usb_reset_configuration succeeded "
931 "after %d iterations\n", i);
932 break;
933 }
934 }
935 }
936
937 if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
938 dev_err(&interface->dev, "Failed to read vendor.\n");
939 retval = -ENODEV;
940 goto error;
941 }
942 if (dt9812_read_info(dev, 3, &dev->product, sizeof(dev->product)) != 0) {
943 dev_err(&interface->dev, "Failed to read product.\n");
944 retval = -ENODEV;
945 goto error;
946 }
947 if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
948 dev_err(&interface->dev, "Failed to read device.\n");
949 retval = -ENODEV;
950 goto error;
951 }
952 if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
953 dev_err(&interface->dev, "Failed to read serial.\n");
954 retval = -ENODEV;
955 goto error;
956 }
957
958 dev->vendor = le16_to_cpu(dev->vendor);
959 dev->product = le16_to_cpu(dev->product);
960 dev->device = le16_to_cpu(dev->device);
961 dev->serial = le32_to_cpu(dev->serial);
962 switch (dev->device) {
963 case DT9812_DEVID_DT9812_10:
964 dev->analog_out_shadow[0] = 0x0800;
965 dev->analog_out_shadow[1] = 0x800;
966 break;
967 case DT9812_DEVID_DT9812_2PT5:
968 dev->analog_out_shadow[0] = 0x0000;
969 dev->analog_out_shadow[1] = 0x0000;
970 break;
971 }
972 dev->digital_out_shadow = 0;
973
974 /* save our data pointer in this interface device */
975 usb_set_intfdata(interface, dev);
976
977 /* let the user know what node this device is now attached to */
978 dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
979 dev->vendor, dev->product, dev->device, dev->serial);
980
df785c7a
HS
981 return 0;
982
983error:
984 if (dev)
985 kref_put(&dev->kref, dt9812_delete);
986 return retval;
987}
988
989static void dt9812_disconnect(struct usb_interface *interface)
990{
991 struct usb_dt9812 *dev;
992 int minor = interface->minor;
993
994 down(&dt9812_mutex);
995 dev = usb_get_intfdata(interface);
996 if (dev->slot) {
997 down(&dev->slot->mutex);
998 dev->slot->usb = NULL;
999 up(&dev->slot->mutex);
1000 dev->slot = NULL;
1001 }
1002 usb_set_intfdata(interface, NULL);
1003 up(&dt9812_mutex);
1004
1005 /* queue final destruction */
1006 kref_put(&dev->kref, dt9812_delete);
1007
1008 dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
1009}
1010
1011static const struct usb_device_id dt9812_table[] = {
1012 { USB_DEVICE(0x0867, 0x9812) },
1013 { }
1014};
1015MODULE_DEVICE_TABLE(usb, dt9812_table);
1016
1017static struct usb_driver dt9812_usb_driver = {
1018 .name = "dt9812",
1019 .id_table = dt9812_table,
1020 .probe = dt9812_probe,
1021 .disconnect = dt9812_disconnect,
1022};
1023
63274cd7
AB
1024static int __init usb_dt9812_init(void)
1025{
ae59ecfd 1026 int i;
63274cd7 1027
f52a8af7 1028 /* Initialize all driver slots */
63274cd7 1029 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
0f0800f1 1030 sema_init(&dt9812[i].mutex, 1);
63274cd7 1031 dt9812[i].usb = NULL;
98c83cfa 1032 dt9812[i].devpriv = NULL;
63274cd7 1033 }
63274cd7 1034
ae59ecfd
PH
1035 return comedi_usb_driver_register(&dt9812_comedi_driver,
1036 &dt9812_usb_driver);
63274cd7
AB
1037}
1038
1039static void __exit usb_dt9812_exit(void)
1040{
ae59ecfd 1041 comedi_usb_driver_unregister(&dt9812_comedi_driver, &dt9812_usb_driver);
63274cd7
AB
1042}
1043
1044module_init(usb_dt9812_init);
1045module_exit(usb_dt9812_exit);
1046
1047MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
1048MODULE_DESCRIPTION("Comedi DT9812 driver");
1049MODULE_LICENSE("GPL");
This page took 0.503038 seconds and 5 git commands to generate.