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