Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 2 | * dscore.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/mod_devicetable.h> |
| 25 | #include <linux/usb.h> |
| 26 | |
| 27 | #include "dscore.h" |
| 28 | |
| 29 | static struct usb_device_id ds_id_table [] = { |
| 30 | { USB_DEVICE(0x04fa, 0x2490) }, |
| 31 | { }, |
| 32 | }; |
| 33 | MODULE_DEVICE_TABLE(usb, ds_id_table); |
| 34 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 35 | static int ds_probe(struct usb_interface *, const struct usb_device_id *); |
| 36 | static void ds_disconnect(struct usb_interface *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | int ds_touch_bit(struct ds_device *, u8, u8 *); |
| 39 | int ds_read_byte(struct ds_device *, u8 *); |
| 40 | int ds_read_bit(struct ds_device *, u8 *); |
| 41 | int ds_write_byte(struct ds_device *, u8); |
| 42 | int ds_write_bit(struct ds_device *, u8); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 43 | static int ds_start_pulse(struct ds_device *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | int ds_reset(struct ds_device *, struct ds_status *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct ds_device * ds_get_device(void); |
| 46 | void ds_put_device(struct ds_device *); |
| 47 | |
| 48 | static inline void ds_dump_status(unsigned char *, unsigned char *, int); |
| 49 | static int ds_send_control(struct ds_device *, u16, u16); |
| 50 | static int ds_send_control_mode(struct ds_device *, u16, u16); |
| 51 | static int ds_send_control_cmd(struct ds_device *, u16, u16); |
| 52 | |
| 53 | |
| 54 | static struct usb_driver ds_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | .name = "DS9490R", |
| 56 | .probe = ds_probe, |
| 57 | .disconnect = ds_disconnect, |
| 58 | .id_table = ds_id_table, |
| 59 | }; |
| 60 | |
| 61 | static struct ds_device *ds_dev; |
| 62 | |
| 63 | struct ds_device * ds_get_device(void) |
| 64 | { |
| 65 | if (ds_dev) |
| 66 | atomic_inc(&ds_dev->refcnt); |
| 67 | return ds_dev; |
| 68 | } |
| 69 | |
| 70 | void ds_put_device(struct ds_device *dev) |
| 71 | { |
| 72 | atomic_dec(&dev->refcnt); |
| 73 | } |
| 74 | |
| 75 | static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index) |
| 76 | { |
| 77 | int err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 78 | |
| 79 | err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | CONTROL_CMD, 0x40, value, index, NULL, 0, 1000); |
| 81 | if (err < 0) { |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 82 | printk(KERN_ERR "Failed to send command control message %x.%x: err=%d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | value, index, err); |
| 84 | return err; |
| 85 | } |
| 86 | |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index) |
| 91 | { |
| 92 | int err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 93 | |
| 94 | err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | MODE_CMD, 0x40, value, index, NULL, 0, 1000); |
| 96 | if (err < 0) { |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 97 | printk(KERN_ERR "Failed to send mode control message %x.%x: err=%d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | value, index, err); |
| 99 | return err; |
| 100 | } |
| 101 | |
| 102 | return err; |
| 103 | } |
| 104 | |
| 105 | static int ds_send_control(struct ds_device *dev, u16 value, u16 index) |
| 106 | { |
| 107 | int err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 108 | |
| 109 | err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | COMM_CMD, 0x40, value, index, NULL, 0, 1000); |
| 111 | if (err < 0) { |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 112 | printk(KERN_ERR "Failed to send control message %x.%x: err=%d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | value, index, err); |
| 114 | return err; |
| 115 | } |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | static inline void ds_dump_status(unsigned char *buf, unsigned char *str, int off) |
| 121 | { |
| 122 | printk("%45s: %8x\n", str, buf[off]); |
| 123 | } |
| 124 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 125 | static int ds_recv_status_nodump(struct ds_device *dev, struct ds_status *st, |
| 126 | unsigned char *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
| 128 | int count, err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | memset(st, 0, sizeof(st)); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | count = 0; |
| 133 | err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_STATUS]), buf, size, &count, 100); |
| 134 | if (err < 0) { |
| 135 | printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err); |
| 136 | return err; |
| 137 | } |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (count >= sizeof(*st)) |
| 140 | memcpy(st, buf, sizeof(*st)); |
| 141 | |
| 142 | return count; |
| 143 | } |
| 144 | |
| 145 | static int ds_recv_status(struct ds_device *dev, struct ds_status *st) |
| 146 | { |
| 147 | unsigned char buf[64]; |
| 148 | int count, err = 0, i; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | memcpy(st, buf, sizeof(*st)); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | count = ds_recv_status_nodump(dev, st, buf, sizeof(buf)); |
| 153 | if (count < 0) |
| 154 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count); |
| 157 | for (i=0; i<count; ++i) |
| 158 | printk("%02x ", buf[i]); |
| 159 | printk("\n"); |
| 160 | |
| 161 | if (count >= 16) { |
| 162 | ds_dump_status(buf, "enable flag", 0); |
| 163 | ds_dump_status(buf, "1-wire speed", 1); |
| 164 | ds_dump_status(buf, "strong pullup duration", 2); |
| 165 | ds_dump_status(buf, "programming pulse duration", 3); |
| 166 | ds_dump_status(buf, "pulldown slew rate control", 4); |
| 167 | ds_dump_status(buf, "write-1 low time", 5); |
| 168 | ds_dump_status(buf, "data sample offset/write-0 recovery time", 6); |
| 169 | ds_dump_status(buf, "reserved (test register)", 7); |
| 170 | ds_dump_status(buf, "device status flags", 8); |
| 171 | ds_dump_status(buf, "communication command byte 1", 9); |
| 172 | ds_dump_status(buf, "communication command byte 2", 10); |
| 173 | ds_dump_status(buf, "communication command buffer status", 11); |
| 174 | ds_dump_status(buf, "1-wire data output buffer status", 12); |
| 175 | ds_dump_status(buf, "1-wire data input buffer status", 13); |
| 176 | ds_dump_status(buf, "reserved", 14); |
| 177 | ds_dump_status(buf, "reserved", 15); |
| 178 | } |
| 179 | |
| 180 | memcpy(st, buf, sizeof(*st)); |
| 181 | |
| 182 | if (st->status & ST_EPOF) { |
| 183 | printk(KERN_INFO "Resetting device after ST_EPOF.\n"); |
| 184 | err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0); |
| 185 | if (err) |
| 186 | return err; |
| 187 | count = ds_recv_status_nodump(dev, st, buf, sizeof(buf)); |
| 188 | if (count < 0) |
| 189 | return err; |
| 190 | } |
| 191 | #if 0 |
| 192 | if (st->status & ST_IDLE) { |
| 193 | printk(KERN_INFO "Resetting pulse after ST_IDLE.\n"); |
| 194 | err = ds_start_pulse(dev, PULLUP_PULSE_DURATION); |
| 195 | if (err) |
| 196 | return err; |
| 197 | } |
| 198 | #endif |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return err; |
| 201 | } |
| 202 | |
| 203 | static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size) |
| 204 | { |
| 205 | int count, err; |
| 206 | struct ds_status st; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | count = 0; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 209 | err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | buf, size, &count, 1000); |
| 211 | if (err < 0) { |
| 212 | printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]); |
| 213 | usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN])); |
| 214 | ds_recv_status(dev, &st); |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | #if 0 |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | printk("%s: count=%d: ", __func__, count); |
| 223 | for (i=0; i<count; ++i) |
| 224 | printk("%02x ", buf[i]); |
| 225 | printk("\n"); |
| 226 | } |
| 227 | #endif |
| 228 | return count; |
| 229 | } |
| 230 | |
| 231 | static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len) |
| 232 | { |
| 233 | int count, err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | count = 0; |
| 236 | err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000); |
| 237 | if (err < 0) { |
| 238 | printk(KERN_ERR "Failed to read 1-wire data from 0x02: err=%d.\n", err); |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | return err; |
| 243 | } |
| 244 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 245 | #if 0 |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | int ds_stop_pulse(struct ds_device *dev, int limit) |
| 248 | { |
| 249 | struct ds_status st; |
| 250 | int count = 0, err = 0; |
| 251 | u8 buf[0x20]; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | do { |
| 254 | err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0); |
| 255 | if (err) |
| 256 | break; |
| 257 | err = ds_send_control(dev, CTL_RESUME_EXE, 0); |
| 258 | if (err) |
| 259 | break; |
| 260 | err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf)); |
| 261 | if (err) |
| 262 | break; |
| 263 | |
| 264 | if ((st.status & ST_SPUA) == 0) { |
| 265 | err = ds_send_control_mode(dev, MOD_PULSE_EN, 0); |
| 266 | if (err) |
| 267 | break; |
| 268 | } |
| 269 | } while(++count < limit); |
| 270 | |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | int ds_detect(struct ds_device *dev, struct ds_status *st) |
| 275 | { |
| 276 | int err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0); |
| 279 | if (err) |
| 280 | return err; |
| 281 | |
| 282 | err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0); |
| 283 | if (err) |
| 284 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40); |
| 287 | if (err) |
| 288 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG); |
| 291 | if (err) |
| 292 | return err; |
| 293 | |
| 294 | err = ds_recv_status(dev, st); |
| 295 | |
| 296 | return err; |
| 297 | } |
| 298 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 299 | #endif /* 0 */ |
| 300 | |
| 301 | static int ds_wait_status(struct ds_device *dev, struct ds_status *st) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | u8 buf[0x20]; |
| 304 | int err, count = 0; |
| 305 | |
| 306 | do { |
| 307 | err = ds_recv_status_nodump(dev, st, buf, sizeof(buf)); |
| 308 | #if 0 |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 309 | if (err >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | int i; |
| 311 | printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err); |
| 312 | for (i=0; i<err; ++i) |
| 313 | printk("%02x ", buf[i]); |
| 314 | printk("\n"); |
| 315 | } |
| 316 | #endif |
| 317 | } while(!(buf[0x08] & 0x20) && !(err < 0) && ++count < 100); |
| 318 | |
| 319 | |
| 320 | if (((err > 16) && (buf[0x10] & 0x01)) || count >= 100 || err < 0) { |
| 321 | ds_recv_status(dev, st); |
| 322 | return -1; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 323 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int ds_reset(struct ds_device *dev, struct ds_status *st) |
| 328 | { |
| 329 | int err; |
| 330 | |
| 331 | //err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE, SPEED_FLEXIBLE); |
| 332 | err = ds_send_control(dev, 0x43, SPEED_NORMAL); |
| 333 | if (err) |
| 334 | return err; |
| 335 | |
| 336 | ds_wait_status(dev, st); |
| 337 | #if 0 |
| 338 | if (st->command_buffer_status) { |
| 339 | printk(KERN_INFO "Short circuit.\n"); |
| 340 | return -EIO; |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 347 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | int ds_set_speed(struct ds_device *dev, int speed) |
| 349 | { |
| 350 | int err; |
| 351 | |
| 352 | if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | if (speed != SPEED_OVERDRIVE) |
| 356 | speed = SPEED_FLEXIBLE; |
| 357 | |
| 358 | speed &= 0xff; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed); |
| 361 | if (err) |
| 362 | return err; |
| 363 | |
| 364 | return err; |
| 365 | } |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 366 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 368 | static int ds_start_pulse(struct ds_device *dev, int delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
| 370 | int err; |
| 371 | u8 del = 1 + (u8)(delay >> 4); |
| 372 | struct ds_status st; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | #if 0 |
| 375 | err = ds_stop_pulse(dev, 10); |
| 376 | if (err) |
| 377 | return err; |
| 378 | |
| 379 | err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE); |
| 380 | if (err) |
| 381 | return err; |
| 382 | #endif |
| 383 | err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del); |
| 384 | if (err) |
| 385 | return err; |
| 386 | |
| 387 | err = ds_send_control(dev, COMM_PULSE | COMM_IM | COMM_F, 0); |
| 388 | if (err) |
| 389 | return err; |
| 390 | |
| 391 | mdelay(delay); |
| 392 | |
| 393 | ds_wait_status(dev, &st); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return err; |
| 396 | } |
| 397 | |
| 398 | int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit) |
| 399 | { |
| 400 | int err, count; |
| 401 | struct ds_status st; |
| 402 | u16 value = (COMM_BIT_IO | COMM_IM) | ((bit) ? COMM_D : 0); |
| 403 | u16 cmd; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | err = ds_send_control(dev, value, 0); |
| 406 | if (err) |
| 407 | return err; |
| 408 | |
| 409 | count = 0; |
| 410 | do { |
| 411 | err = ds_wait_status(dev, &st); |
| 412 | if (err) |
| 413 | return err; |
| 414 | |
| 415 | cmd = st.command0 | (st.command1 << 8); |
| 416 | } while (cmd != value && ++count < 10); |
| 417 | |
| 418 | if (err < 0 || count >= 10) { |
| 419 | printk(KERN_ERR "Failed to obtain status.\n"); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | err = ds_recv_data(dev, tbit, sizeof(*tbit)); |
| 424 | if (err < 0) |
| 425 | return err; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int ds_write_bit(struct ds_device *dev, u8 bit) |
| 431 | { |
| 432 | int err; |
| 433 | struct ds_status st; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit) ? COMM_D : 0, 0); |
| 436 | if (err) |
| 437 | return err; |
| 438 | |
| 439 | ds_wait_status(dev, &st); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int ds_write_byte(struct ds_device *dev, u8 byte) |
| 445 | { |
| 446 | int err; |
| 447 | struct ds_status st; |
| 448 | u8 rbyte; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | COMM_SPU, byte); |
| 451 | if (err) |
| 452 | return err; |
| 453 | |
| 454 | err = ds_wait_status(dev, &st); |
| 455 | if (err) |
| 456 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | err = ds_recv_data(dev, &rbyte, sizeof(rbyte)); |
| 459 | if (err < 0) |
| 460 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | ds_start_pulse(dev, PULLUP_PULSE_DURATION); |
| 463 | |
| 464 | return !(byte == rbyte); |
| 465 | } |
| 466 | |
| 467 | int ds_read_bit(struct ds_device *dev, u8 *bit) |
| 468 | { |
| 469 | int err; |
| 470 | |
| 471 | err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE); |
| 472 | if (err) |
| 473 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0); |
| 476 | if (err) |
| 477 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | err = ds_recv_data(dev, bit, sizeof(*bit)); |
| 480 | if (err < 0) |
| 481 | return err; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | int ds_read_byte(struct ds_device *dev, u8 *byte) |
| 487 | { |
| 488 | int err; |
| 489 | struct ds_status st; |
| 490 | |
| 491 | err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff); |
| 492 | if (err) |
| 493 | return err; |
| 494 | |
| 495 | ds_wait_status(dev, &st); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | err = ds_recv_data(dev, byte, sizeof(*byte)); |
| 498 | if (err < 0) |
| 499 | return err; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | int ds_read_block(struct ds_device *dev, u8 *buf, int len) |
| 505 | { |
| 506 | struct ds_status st; |
| 507 | int err; |
| 508 | |
| 509 | if (len > 64*1024) |
| 510 | return -E2BIG; |
| 511 | |
| 512 | memset(buf, 0xFF, len); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | err = ds_send_data(dev, buf, len); |
| 515 | if (err < 0) |
| 516 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len); |
| 519 | if (err) |
| 520 | return err; |
| 521 | |
| 522 | ds_wait_status(dev, &st); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | memset(buf, 0x00, len); |
| 525 | err = ds_recv_data(dev, buf, len); |
| 526 | |
| 527 | return err; |
| 528 | } |
| 529 | |
| 530 | int ds_write_block(struct ds_device *dev, u8 *buf, int len) |
| 531 | { |
| 532 | int err; |
| 533 | struct ds_status st; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | err = ds_send_data(dev, buf, len); |
| 536 | if (err < 0) |
| 537 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | ds_wait_status(dev, &st); |
| 540 | |
| 541 | err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | COMM_SPU, len); |
| 542 | if (err) |
| 543 | return err; |
| 544 | |
| 545 | ds_wait_status(dev, &st); |
| 546 | |
| 547 | err = ds_recv_data(dev, buf, len); |
| 548 | if (err < 0) |
| 549 | return err; |
| 550 | |
| 551 | ds_start_pulse(dev, PULLUP_PULSE_DURATION); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return !(err == len); |
| 554 | } |
| 555 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 556 | #if 0 |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search) |
| 559 | { |
| 560 | int err; |
| 561 | u16 value, index; |
| 562 | struct ds_status st; |
| 563 | |
| 564 | memset(buf, 0, sizeof(buf)); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | err = ds_send_data(ds_dev, (unsigned char *)&init, 8); |
| 567 | if (err) |
| 568 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | ds_wait_status(ds_dev, &st); |
| 571 | |
| 572 | value = COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS; |
| 573 | index = (conditional_search ? 0xEC : 0xF0) | (id_number << 8); |
| 574 | err = ds_send_control(ds_dev, value, index); |
| 575 | if (err) |
| 576 | return err; |
| 577 | |
| 578 | ds_wait_status(ds_dev, &st); |
| 579 | |
| 580 | err = ds_recv_data(ds_dev, (unsigned char *)buf, 8*id_number); |
| 581 | if (err < 0) |
| 582 | return err; |
| 583 | |
| 584 | return err/8; |
| 585 | } |
| 586 | |
| 587 | int ds_match_access(struct ds_device *dev, u64 init) |
| 588 | { |
| 589 | int err; |
| 590 | struct ds_status st; |
| 591 | |
| 592 | err = ds_send_data(dev, (unsigned char *)&init, sizeof(init)); |
| 593 | if (err) |
| 594 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | ds_wait_status(dev, &st); |
| 597 | |
| 598 | err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055); |
| 599 | if (err) |
| 600 | return err; |
| 601 | |
| 602 | ds_wait_status(dev, &st); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | int ds_set_path(struct ds_device *dev, u64 init) |
| 608 | { |
| 609 | int err; |
| 610 | struct ds_status st; |
| 611 | u8 buf[9]; |
| 612 | |
| 613 | memcpy(buf, &init, 8); |
| 614 | buf[8] = BRANCH_MAIN; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | err = ds_send_data(dev, buf, sizeof(buf)); |
| 617 | if (err) |
| 618 | return err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | ds_wait_status(dev, &st); |
| 621 | |
| 622 | err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0); |
| 623 | if (err) |
| 624 | return err; |
| 625 | |
| 626 | ds_wait_status(dev, &st); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 631 | #endif /* 0 */ |
| 632 | |
| 633 | static int ds_probe(struct usb_interface *intf, |
| 634 | const struct usb_device_id *udev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
| 636 | struct usb_device *udev = interface_to_usbdev(intf); |
| 637 | struct usb_endpoint_descriptor *endpoint; |
| 638 | struct usb_host_interface *iface_desc; |
| 639 | int i, err; |
| 640 | |
| 641 | ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL); |
| 642 | if (!ds_dev) { |
| 643 | printk(KERN_INFO "Failed to allocate new DS9490R structure.\n"); |
| 644 | return -ENOMEM; |
| 645 | } |
| 646 | |
| 647 | ds_dev->udev = usb_get_dev(udev); |
| 648 | usb_set_intfdata(intf, ds_dev); |
| 649 | |
| 650 | err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3); |
| 651 | if (err) { |
| 652 | printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n", |
| 653 | intf->altsetting[0].desc.bInterfaceNumber, err); |
| 654 | return err; |
| 655 | } |
| 656 | |
| 657 | err = usb_reset_configuration(ds_dev->udev); |
| 658 | if (err) { |
| 659 | printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err); |
| 660 | return err; |
| 661 | } |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | iface_desc = &intf->altsetting[0]; |
| 664 | if (iface_desc->desc.bNumEndpoints != NUM_EP-1) { |
| 665 | printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints); |
| 666 | return -ENODEV; |
| 667 | } |
| 668 | |
| 669 | atomic_set(&ds_dev->refcnt, 0); |
| 670 | memset(ds_dev->ep, 0, sizeof(ds_dev->ep)); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 673 | * This loop doesn'd show control 0 endpoint, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * so we will fill only 1-3 endpoints entry. |
| 675 | */ |
| 676 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 677 | endpoint = &iface_desc->endpoint[i].desc; |
| 678 | |
| 679 | ds_dev->ep[i+1] = endpoint->bEndpointAddress; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | printk("%d: addr=%x, size=%d, dir=%s, type=%x\n", |
| 682 | i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize), |
| 683 | (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT", |
| 684 | endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); |
| 685 | } |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | #if 0 |
| 688 | { |
| 689 | int err, i; |
| 690 | u64 buf[3]; |
| 691 | u64 init=0xb30000002078ee81ull; |
| 692 | struct ds_status st; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | ds_reset(ds_dev, &st); |
| 695 | err = ds_search(ds_dev, init, buf, 3, 0); |
| 696 | if (err < 0) |
| 697 | return err; |
| 698 | for (i=0; i<err; ++i) |
| 699 | printk("%d: %llx\n", i, buf[i]); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 700 | |
| 701 | printk("Resetting...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | ds_reset(ds_dev, &st); |
| 703 | printk("Setting path for %llx.\n", init); |
| 704 | err = ds_set_path(ds_dev, init); |
| 705 | if (err) |
| 706 | return err; |
| 707 | printk("Calling MATCH_ACCESS.\n"); |
| 708 | err = ds_match_access(ds_dev, init); |
| 709 | if (err) |
| 710 | return err; |
| 711 | |
| 712 | printk("Searching the bus...\n"); |
| 713 | err = ds_search(ds_dev, init, buf, 3, 0); |
| 714 | |
| 715 | printk("ds_search() returned %d\n", err); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if (err < 0) |
| 718 | return err; |
| 719 | for (i=0; i<err; ++i) |
| 720 | printk("%d: %llx\n", i, buf[i]); |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | #endif |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 729 | static void ds_disconnect(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
| 731 | struct ds_device *dev; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | dev = usb_get_intfdata(intf); |
| 734 | usb_set_intfdata(intf, NULL); |
| 735 | |
| 736 | while (atomic_read(&dev->refcnt)) { |
| 737 | printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n", |
| 738 | atomic_read(&dev->refcnt)); |
| 739 | |
| 740 | if (msleep_interruptible(1000)) |
| 741 | flush_signals(current); |
| 742 | } |
| 743 | |
| 744 | usb_put_dev(dev->udev); |
| 745 | kfree(dev); |
| 746 | ds_dev = NULL; |
| 747 | } |
| 748 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 749 | static int ds_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
| 751 | int err; |
| 752 | |
| 753 | err = usb_register(&ds_driver); |
| 754 | if (err) { |
| 755 | printk(KERN_INFO "Failed to register DS9490R USB device: err=%d.\n", err); |
| 756 | return err; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 762 | static void ds_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
| 764 | usb_deregister(&ds_driver); |
| 765 | } |
| 766 | |
| 767 | module_init(ds_init); |
| 768 | module_exit(ds_fini); |
| 769 | |
| 770 | MODULE_LICENSE("GPL"); |
| 771 | MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>"); |
| 772 | |
| 773 | EXPORT_SYMBOL(ds_touch_bit); |
| 774 | EXPORT_SYMBOL(ds_read_byte); |
| 775 | EXPORT_SYMBOL(ds_read_bit); |
| 776 | EXPORT_SYMBOL(ds_read_block); |
| 777 | EXPORT_SYMBOL(ds_write_byte); |
| 778 | EXPORT_SYMBOL(ds_write_bit); |
| 779 | EXPORT_SYMBOL(ds_write_block); |
| 780 | EXPORT_SYMBOL(ds_reset); |
| 781 | EXPORT_SYMBOL(ds_get_device); |
| 782 | EXPORT_SYMBOL(ds_put_device); |
| 783 | |
| 784 | /* |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 785 | * This functions can be used for EEPROM programming, |
| 786 | * when driver will be included into mainline this will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | * require uncommenting. |
| 788 | */ |
| 789 | #if 0 |
| 790 | EXPORT_SYMBOL(ds_start_pulse); |
| 791 | EXPORT_SYMBOL(ds_set_speed); |
| 792 | EXPORT_SYMBOL(ds_detect); |
| 793 | EXPORT_SYMBOL(ds_stop_pulse); |
| 794 | EXPORT_SYMBOL(ds_search); |
| 795 | #endif |