1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ds2490.c USB to one wire bridge
4 *
5 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
6 */
7
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/usb.h>
11 #include <linux/slab.h>
12
13 #include <linux/w1.h>
14
15 /* USB Standard */
16 /* USB Control request vendor type */
17 #define VENDOR 0x40
18
19 /* COMMAND TYPE CODES */
20 #define CONTROL_CMD 0x00
21 #define COMM_CMD 0x01
22 #define MODE_CMD 0x02
23
24 /* CONTROL COMMAND CODES */
25 #define CTL_RESET_DEVICE 0x0000
26 #define CTL_START_EXE 0x0001
27 #define CTL_RESUME_EXE 0x0002
28 #define CTL_HALT_EXE_IDLE 0x0003
29 #define CTL_HALT_EXE_DONE 0x0004
30 #define CTL_FLUSH_COMM_CMDS 0x0007
31 #define CTL_FLUSH_RCV_BUFFER 0x0008
32 #define CTL_FLUSH_XMT_BUFFER 0x0009
33 #define CTL_GET_COMM_CMDS 0x000A
34
35 /* MODE COMMAND CODES */
36 #define MOD_PULSE_EN 0x0000
37 #define MOD_SPEED_CHANGE_EN 0x0001
38 #define MOD_1WIRE_SPEED 0x0002
39 #define MOD_STRONG_PU_DURATION 0x0003
40 #define MOD_PULLDOWN_SLEWRATE 0x0004
41 #define MOD_PROG_PULSE_DURATION 0x0005
42 #define MOD_WRITE1_LOWTIME 0x0006
43 #define MOD_DSOW0_TREC 0x0007
44
45 /* COMMUNICATION COMMAND CODES */
46 #define COMM_ERROR_ESCAPE 0x0601
47 #define COMM_SET_DURATION 0x0012
48 #define COMM_BIT_IO 0x0020
49 #define COMM_PULSE 0x0030
50 #define COMM_1_WIRE_RESET 0x0042
51 #define COMM_BYTE_IO 0x0052
52 #define COMM_MATCH_ACCESS 0x0064
53 #define COMM_BLOCK_IO 0x0074
54 #define COMM_READ_STRAIGHT 0x0080
55 #define COMM_DO_RELEASE 0x6092
56 #define COMM_SET_PATH 0x00A2
57 #define COMM_WRITE_SRAM_PAGE 0x00B2
58 #define COMM_WRITE_EPROM 0x00C4
59 #define COMM_READ_CRC_PROT_PAGE 0x00D4
60 #define COMM_READ_REDIRECT_PAGE_CRC 0x21E4
61 #define COMM_SEARCH_ACCESS 0x00F4
62
63 /* Communication command bits */
64 #define COMM_TYPE 0x0008
65 #define COMM_SE 0x0008
66 #define COMM_D 0x0008
67 #define COMM_Z 0x0008
68 #define COMM_CH 0x0008
69 #define COMM_SM 0x0008
70 #define COMM_R 0x0008
71 #define COMM_IM 0x0001
72
73 #define COMM_PS 0x4000
74 #define COMM_PST 0x4000
75 #define COMM_CIB 0x4000
76 #define COMM_RTS 0x4000
77 #define COMM_DT 0x2000
78 #define COMM_SPU 0x1000
79 #define COMM_F 0x0800
80 #define COMM_NTF 0x0400
81 #define COMM_ICP 0x0200
82 #define COMM_RST 0x0100
83
84 #define PULSE_PROG 0x01
85 #define PULSE_SPUE 0x02
86
87 #define BRANCH_MAIN 0xCC
88 #define BRANCH_AUX 0x33
89
90 /* Status flags */
91 #define ST_SPUA 0x01 /* Strong Pull-up is active */
92 #define ST_PRGA 0x02 /* 12V programming pulse is being generated */
93 #define ST_12VP 0x04 /* external 12V programming voltage is present */
94 #define ST_PMOD 0x08 /* DS2490 powered from USB and external sources */
95 #define ST_HALT 0x10 /* DS2490 is currently halted */
96 #define ST_IDLE 0x20 /* DS2490 is currently idle */
97 #define ST_EPOF 0x80
98 /* Status transfer size, 16 bytes status, 16 byte result flags */
99 #define ST_SIZE 0x20
100 /* 1-wire data i/o fifo size, 128 bytes */
101 #define FIFO_SIZE 0x80
102
103 /* Result Register flags */
104 #define RR_DETECT 0xA5 /* New device detected */
105 #define RR_NRS 0x01 /* Reset no presence or ... */
106 #define RR_SH 0x02 /* short on reset or set path */
107 #define RR_APP 0x04 /* alarming presence on reset */
108 #define RR_VPP 0x08 /* 12V expected not seen */
109 #define RR_CMP 0x10 /* compare error */
110 #define RR_CRC 0x20 /* CRC error detected */
111 #define RR_RDP 0x40 /* redirected page */
112 #define RR_EOS 0x80 /* end of search error */
113
114 #define SPEED_NORMAL 0x00
115 #define SPEED_FLEXIBLE 0x01
116 #define SPEED_OVERDRIVE 0x02
117
118 #define NUM_EP 4
119 #define EP_CONTROL 0
120 #define EP_STATUS 1
121 #define EP_DATA_OUT 2
122 #define EP_DATA_IN 3
123
124 struct ds_device {
125 struct list_head ds_entry;
126
127 struct usb_device *udev;
128 struct usb_interface *intf;
129
130 int ep[NUM_EP];
131
132 /* Strong PullUp
133 * 0: pullup not active, else duration in milliseconds
134 */
135 int spu_sleep;
136 /* spu_bit contains COMM_SPU or 0 depending on if the strong pullup
137 * should be active or not for writes.
138 */
139 u16 spu_bit;
140
141 u8 st_buf[ST_SIZE];
142 u8 byte_buf;
143
144 struct w1_bus_master master;
145 };
146
147 struct ds_status {
148 u8 enable;
149 u8 speed;
150 u8 pullup_dur;
151 u8 ppuls_dur;
152 u8 pulldown_slew;
153 u8 write1_time;
154 u8 write0_time;
155 u8 reserved0;
156 u8 status;
157 u8 command0;
158 u8 command1;
159 u8 command_buffer_status;
160 u8 data_out_buffer_status;
161 u8 data_in_buffer_status;
162 u8 reserved1;
163 u8 reserved2;
164 };
165
166 static LIST_HEAD(ds_devices);
167 static DEFINE_MUTEX(ds_mutex);
168
ds_send_control_cmd(struct ds_device * dev,u16 value,u16 index)169 static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
170 {
171 int err;
172
173 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
174 CONTROL_CMD, VENDOR, value, index, NULL, 0, 1000);
175 if (err < 0) {
176 dev_err(&dev->udev->dev,
177 "Failed to send command control message %x.%x: err=%d.\n",
178 value, index, err);
179 return err;
180 }
181
182 return err;
183 }
184
ds_send_control_mode(struct ds_device * dev,u16 value,u16 index)185 static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
186 {
187 int err;
188
189 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
190 MODE_CMD, VENDOR, value, index, NULL, 0, 1000);
191 if (err < 0) {
192 dev_err(&dev->udev->dev,
193 "Failed to send mode control message %x.%x: err=%d.\n",
194 value, index, err);
195 return err;
196 }
197
198 return err;
199 }
200
ds_send_control(struct ds_device * dev,u16 value,u16 index)201 static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
202 {
203 int err;
204
205 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
206 COMM_CMD, VENDOR, value, index, NULL, 0, 1000);
207 if (err < 0) {
208 dev_err(&dev->udev->dev,
209 "Failed to send control message %x.%x: err=%d.\n",
210 value, index, err);
211 return err;
212 }
213
214 return err;
215 }
216
ds_dump_status(struct ds_device * ds_dev,unsigned char * buf,int count)217 static void ds_dump_status(struct ds_device *ds_dev, unsigned char *buf, int count)
218 {
219 struct device *dev = &ds_dev->udev->dev;
220 int i;
221
222 dev_info(dev, "ep_status=0x%x, count=%d, status=%*phC",
223 ds_dev->ep[EP_STATUS], count, count, buf);
224
225 if (count >= 16) {
226 dev_dbg(dev, "enable flag: 0x%02x", buf[0]);
227 dev_dbg(dev, "1-wire speed: 0x%02x", buf[1]);
228 dev_dbg(dev, "strong pullup duration: 0x%02x", buf[2]);
229 dev_dbg(dev, "programming pulse duration: 0x%02x", buf[3]);
230 dev_dbg(dev, "pulldown slew rate control: 0x%02x", buf[4]);
231 dev_dbg(dev, "write-1 low time: 0x%02x", buf[5]);
232 dev_dbg(dev, "data sample offset/write-0 recovery time: 0x%02x", buf[6]);
233 dev_dbg(dev, "reserved (test register): 0x%02x", buf[7]);
234 dev_dbg(dev, "device status flags: 0x%02x", buf[8]);
235 dev_dbg(dev, "communication command byte 1: 0x%02x", buf[9]);
236 dev_dbg(dev, "communication command byte 2: 0x%02x", buf[10]);
237 dev_dbg(dev, "communication command buffer status: 0x%02x", buf[11]);
238 dev_dbg(dev, "1-wire data output buffer status: 0x%02x", buf[12]);
239 dev_dbg(dev, "1-wire data input buffer status: 0x%02x", buf[13]);
240 dev_dbg(dev, "reserved: 0x%02x", buf[14]);
241 dev_dbg(dev, "reserved: 0x%02x", buf[15]);
242 }
243
244 for (i = 16; i < count; ++i) {
245 if (buf[i] == RR_DETECT) {
246 dev_dbg(dev, "New device detect.\n");
247 continue;
248 }
249 dev_dbg(dev, "Result Register Value: 0x%02x", buf[i]);
250 if (buf[i] & RR_NRS)
251 dev_dbg(dev, "NRS: Reset no presence or ...\n");
252 if (buf[i] & RR_SH)
253 dev_dbg(dev, "SH: short on reset or set path\n");
254 if (buf[i] & RR_APP)
255 dev_dbg(dev, "APP: alarming presence on reset\n");
256 if (buf[i] & RR_VPP)
257 dev_dbg(dev, "VPP: 12V expected not seen\n");
258 if (buf[i] & RR_CMP)
259 dev_dbg(dev, "CMP: compare error\n");
260 if (buf[i] & RR_CRC)
261 dev_dbg(dev, "CRC: CRC error detected\n");
262 if (buf[i] & RR_RDP)
263 dev_dbg(dev, "RDP: redirected page\n");
264 if (buf[i] & RR_EOS)
265 dev_dbg(dev, "EOS: end of search error\n");
266 }
267 }
268
ds_recv_status(struct ds_device * dev,struct ds_status * st)269 static int ds_recv_status(struct ds_device *dev, struct ds_status *st)
270 {
271 int count, err;
272
273 if (st)
274 memset(st, 0, sizeof(*st));
275
276 count = 0;
277 err = usb_interrupt_msg(dev->udev,
278 usb_rcvintpipe(dev->udev,
279 dev->ep[EP_STATUS]),
280 dev->st_buf, sizeof(dev->st_buf),
281 &count, 1000);
282 if (err < 0) {
283 dev_err(&dev->udev->dev,
284 "Failed to read 1-wire data from 0x%x: err=%d.\n",
285 dev->ep[EP_STATUS], err);
286 return err;
287 }
288
289 if (st && count >= sizeof(*st))
290 memcpy(st, dev->st_buf, sizeof(*st));
291
292 return count;
293 }
294
ds_reset_device(struct ds_device * dev)295 static void ds_reset_device(struct ds_device *dev)
296 {
297 ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
298 /* Always allow strong pullup which allow individual writes to use
299 * the strong pullup.
300 */
301 if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
302 dev_err(&dev->udev->dev,
303 "%s: Error allowing strong pullup\n", __func__);
304 /* Chip strong pullup time was cleared. */
305 if (dev->spu_sleep) {
306 /* lower 4 bits are 0, see ds_set_pullup */
307 u8 del = dev->spu_sleep>>4;
308
309 if (ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del))
310 dev_err(&dev->udev->dev,
311 "%s: Error setting duration\n", __func__);
312 }
313 }
314
ds_recv_data(struct ds_device * dev,unsigned char * buf,int size)315 static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
316 {
317 int count, err;
318
319 /* Careful on size. If size is less than what is available in
320 * the input buffer, the device fails the bulk transfer and
321 * clears the input buffer. It could read the maximum size of
322 * the data buffer, but then do you return the first, last, or
323 * some set of the middle size bytes? As long as the rest of
324 * the code is correct there will be size bytes waiting. A
325 * call to ds_wait_status will wait until the device is idle
326 * and any data to be received would have been available.
327 */
328 count = 0;
329 err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
330 buf, size, &count, 1000);
331 if (err < 0) {
332 int recv_len;
333
334 dev_info(&dev->udev->dev, "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
335 usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
336
337 /* status might tell us why endpoint is stuck? */
338 recv_len = ds_recv_status(dev, NULL);
339 if (recv_len >= 0)
340 ds_dump_status(dev, dev->st_buf, recv_len);
341
342 return err;
343 }
344
345 #if 0
346 {
347 int i;
348
349 printk("%s: count=%d: ", __func__, count);
350 for (i = 0; i < count; ++i)
351 printk("%02x ", buf[i]);
352 printk("\n");
353 }
354 #endif
355 return count;
356 }
357
ds_send_data(struct ds_device * dev,unsigned char * buf,int len)358 static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
359 {
360 int count, err;
361
362 count = 0;
363 err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
364 if (err < 0) {
365 dev_err(&dev->udev->dev, "Failed to write 1-wire data to ep0x%x: "
366 "err=%d.\n", dev->ep[EP_DATA_OUT], err);
367 return err;
368 }
369
370 return err;
371 }
372
373 #if 0
374
375 int ds_stop_pulse(struct ds_device *dev, int limit)
376 {
377 struct ds_status st;
378 int count = 0, err = 0;
379
380 do {
381 err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
382 if (err)
383 break;
384 err = ds_send_control(dev, CTL_RESUME_EXE, 0);
385 if (err)
386 break;
387 err = ds_recv_status(dev, &st);
388 if (err)
389 break;
390
391 if ((st.status & ST_SPUA) == 0) {
392 err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
393 if (err)
394 break;
395 }
396 } while (++count < limit);
397
398 return err;
399 }
400
401 int ds_detect(struct ds_device *dev, struct ds_status *st)
402 {
403 int err;
404
405 err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
406 if (err)
407 return err;
408
409 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
410 if (err)
411 return err;
412
413 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
414 if (err)
415 return err;
416
417 err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
418 if (err)
419 return err;
420
421 err = ds_dump_status(dev, st);
422
423 return err;
424 }
425
426 #endif /* 0 */
427
ds_wait_status(struct ds_device * dev,struct ds_status * st)428 static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
429 {
430 int err, count = 0;
431
432 do {
433 st->status = 0;
434 err = ds_recv_status(dev, st);
435 #if 0
436 if (err >= 0) {
437 int i;
438 printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
439 for (i = 0; i < err; ++i)
440 printk("%02x ", dev->st_buf[i]);
441 printk("\n");
442 }
443 #endif
444 } while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
445
446 if (err >= 16 && st->status & ST_EPOF) {
447 dev_info(&dev->udev->dev, "Resetting device after ST_EPOF.\n");
448 ds_reset_device(dev);
449 /* Always dump the device status. */
450 count = 101;
451 }
452
453 /* Dump the status for errors or if there is extended return data.
454 * The extended status includes new device detection (maybe someone
455 * can do something with it).
456 */
457 if (err > 16 || count >= 100 || err < 0)
458 ds_dump_status(dev, dev->st_buf, err);
459
460 /* Extended data isn't an error. Well, a short is, but the dump
461 * would have already told the user that and we can't do anything
462 * about it in software anyway.
463 */
464 if (count >= 100 || err < 0)
465 return -1;
466 else
467 return 0;
468 }
469
ds_reset(struct ds_device * dev)470 static int ds_reset(struct ds_device *dev)
471 {
472 int err;
473
474 /* Other potentionally interesting flags for reset.
475 *
476 * COMM_NTF: Return result register feedback. This could be used to
477 * detect some conditions such as short, alarming presence, or
478 * detect if a new device was detected.
479 *
480 * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
481 * Select the data transfer rate.
482 */
483 err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
484 if (err)
485 return err;
486
487 return 0;
488 }
489
490 #if 0
491 static int ds_set_speed(struct ds_device *dev, int speed)
492 {
493 int err;
494
495 if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
496 return -EINVAL;
497
498 if (speed != SPEED_OVERDRIVE)
499 speed = SPEED_FLEXIBLE;
500
501 speed &= 0xff;
502
503 err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
504 if (err)
505 return err;
506
507 return err;
508 }
509 #endif /* 0 */
510
ds_set_pullup(struct ds_device * dev,int delay)511 static int ds_set_pullup(struct ds_device *dev, int delay)
512 {
513 int err = 0;
514 u8 del = 1 + (u8)(delay >> 4);
515 /* Just storing delay would not get the trunication and roundup. */
516 int ms = del<<4;
517
518 /* Enable spu_bit if a delay is set. */
519 dev->spu_bit = delay ? COMM_SPU : 0;
520 /* If delay is zero, it has already been disabled, if the time is
521 * the same as the hardware was last programmed to, there is also
522 * nothing more to do. Compare with the recalculated value ms
523 * rather than del or delay which can have a different value.
524 */
525 if (delay == 0 || ms == dev->spu_sleep)
526 return err;
527
528 err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
529 if (err)
530 return err;
531
532 dev->spu_sleep = ms;
533
534 return err;
535 }
536
ds_touch_bit(struct ds_device * dev,u8 bit,u8 * tbit)537 static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
538 {
539 int err;
540 struct ds_status st;
541
542 err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
543 0);
544 if (err)
545 return err;
546
547 ds_wait_status(dev, &st);
548
549 err = ds_recv_data(dev, tbit, sizeof(*tbit));
550 if (err < 0)
551 return err;
552
553 return 0;
554 }
555
556 #if 0
557 static int ds_write_bit(struct ds_device *dev, u8 bit)
558 {
559 int err;
560 struct ds_status st;
561
562 /* Set COMM_ICP to write without a readback. Note, this will
563 * produce one time slot, a down followed by an up with COMM_D
564 * only determing the timing.
565 */
566 err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
567 (bit ? COMM_D : 0), 0);
568 if (err)
569 return err;
570
571 ds_wait_status(dev, &st);
572
573 return 0;
574 }
575 #endif
576
ds_write_byte(struct ds_device * dev,u8 byte)577 static int ds_write_byte(struct ds_device *dev, u8 byte)
578 {
579 int err;
580 struct ds_status st;
581
582 err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | dev->spu_bit, byte);
583 if (err)
584 return err;
585
586 if (dev->spu_bit)
587 msleep(dev->spu_sleep);
588
589 err = ds_wait_status(dev, &st);
590 if (err)
591 return err;
592
593 err = ds_recv_data(dev, &dev->byte_buf, 1);
594 if (err < 0)
595 return err;
596
597 return !(byte == dev->byte_buf);
598 }
599
ds_read_byte(struct ds_device * dev,u8 * byte)600 static int ds_read_byte(struct ds_device *dev, u8 *byte)
601 {
602 int err;
603 struct ds_status st;
604
605 err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM, 0xff);
606 if (err)
607 return err;
608
609 ds_wait_status(dev, &st);
610
611 err = ds_recv_data(dev, byte, sizeof(*byte));
612 if (err < 0)
613 return err;
614
615 return 0;
616 }
617
read_block_chunk(struct ds_device * dev,u8 * buf,int len)618 static int read_block_chunk(struct ds_device *dev, u8 *buf, int len)
619 {
620 struct ds_status st;
621 int err;
622
623 memset(buf, 0xFF, len);
624
625 err = ds_send_data(dev, buf, len);
626 if (err < 0)
627 return err;
628
629 err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
630 if (err)
631 return err;
632
633 ds_wait_status(dev, &st);
634
635 memset(buf, 0x00, len);
636 err = ds_recv_data(dev, buf, len);
637
638 return err;
639 }
640
ds_read_block(struct ds_device * dev,u8 * buf,int len)641 static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
642 {
643 int err, to_read, rem = len;
644
645 if (len > 64 * 1024)
646 return -E2BIG;
647
648 do {
649 to_read = rem <= FIFO_SIZE ? rem : FIFO_SIZE;
650 err = read_block_chunk(dev, &buf[len - rem], to_read);
651 if (err < 0)
652 return err;
653 rem -= to_read;
654 } while (rem);
655
656 return err;
657 }
658
ds_write_block(struct ds_device * dev,u8 * buf,int len)659 static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
660 {
661 int err;
662 struct ds_status st;
663
664 err = ds_send_data(dev, buf, len);
665 if (err < 0)
666 return err;
667
668 err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | dev->spu_bit, len);
669 if (err)
670 return err;
671
672 if (dev->spu_bit)
673 msleep(dev->spu_sleep);
674
675 ds_wait_status(dev, &st);
676
677 err = ds_recv_data(dev, buf, len);
678 if (err < 0)
679 return err;
680
681 return !(err == len);
682 }
683
ds9490r_search(void * data,struct w1_master * master,u8 search_type,w1_slave_found_callback callback)684 static void ds9490r_search(void *data, struct w1_master *master,
685 u8 search_type, w1_slave_found_callback callback)
686 {
687 /* When starting with an existing id, the first id returned will
688 * be that device (if it is still on the bus most likely).
689 *
690 * If the number of devices found is less than or equal to the
691 * search_limit, that number of IDs will be returned. If there are
692 * more, search_limit IDs will be returned followed by a non-zero
693 * discrepency value.
694 */
695 struct ds_device *dev = data;
696 int err;
697 u16 value, index;
698 struct ds_status st;
699 int search_limit;
700 int found = 0;
701 int i;
702
703 /* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
704 * discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
705 */
706 const unsigned long jtime = msecs_to_jiffies(1000*8/75);
707 /* FIFO 128 bytes, bulk packet size 64, read a multiple of the
708 * packet size.
709 */
710 const size_t bufsize = 2 * 64;
711 u64 *buf, *found_ids;
712
713 buf = kmalloc(bufsize, GFP_KERNEL);
714 if (!buf)
715 return;
716
717 /*
718 * We are holding the bus mutex during the scan, but adding devices via the
719 * callback needs the bus to be unlocked. So we queue up found ids here.
720 */
721 found_ids = kmalloc_array(master->max_slave_count, sizeof(u64), GFP_KERNEL);
722 if (!found_ids) {
723 kfree(buf);
724 return;
725 }
726
727 mutex_lock(&master->bus_mutex);
728
729 /* address to start searching at */
730 if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
731 goto search_out;
732 master->search_id = 0;
733
734 value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
735 COMM_RTS;
736 search_limit = master->max_slave_count;
737 if (search_limit > 255)
738 search_limit = 0;
739 index = search_type | (search_limit << 8);
740 if (ds_send_control(dev, value, index) < 0)
741 goto search_out;
742
743 do {
744 schedule_timeout(jtime);
745
746 err = ds_recv_status(dev, &st);
747 if (err < 0 || err < sizeof(st))
748 break;
749
750 if (st.data_in_buffer_status) {
751 /*
752 * Bulk in can receive partial ids, but when it does
753 * they fail crc and will be discarded anyway.
754 * That has only been seen when status in buffer
755 * is 0 and bulk is read anyway, so don't read
756 * bulk without first checking if status says there
757 * is data to read.
758 */
759 err = ds_recv_data(dev, (u8 *)buf, bufsize);
760 if (err < 0)
761 break;
762 for (i = 0; i < err/8; ++i) {
763 found_ids[found++] = buf[i];
764 /*
765 * can't know if there will be a discrepancy
766 * value after until the next id
767 */
768 if (found == search_limit) {
769 master->search_id = buf[i];
770 break;
771 }
772 }
773 }
774
775 if (test_bit(W1_ABORT_SEARCH, &master->flags))
776 break;
777 } while (!(st.status & (ST_IDLE | ST_HALT)));
778
779 /* only continue the search if some weren't found */
780 if (found <= search_limit) {
781 master->search_id = 0;
782 } else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
783 /*
784 * Only max_slave_count will be scanned in a search,
785 * but it will start where it left off next search
786 * until all ids are identified and then it will start
787 * over. A continued search will report the previous
788 * last id as the first id (provided it is still on the
789 * bus).
790 */
791 dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
792 "will continue next search.\n", __func__,
793 master->max_slave_count);
794 set_bit(W1_WARN_MAX_COUNT, &master->flags);
795 }
796
797 search_out:
798 mutex_unlock(&master->bus_mutex);
799 kfree(buf);
800
801 for (i = 0; i < found; i++) /* run callback for all queued up IDs */
802 callback(master, found_ids[i]);
803 kfree(found_ids);
804 }
805
806 #if 0
807 /*
808 * FIXME: if this disabled code is ever used in the future all ds_send_data()
809 * calls must be changed to use a DMAable buffer.
810 */
811 static int ds_match_access(struct ds_device *dev, u64 init)
812 {
813 int err;
814 struct ds_status st;
815
816 err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
817 if (err)
818 return err;
819
820 ds_wait_status(dev, &st);
821
822 err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
823 if (err)
824 return err;
825
826 ds_wait_status(dev, &st);
827
828 return 0;
829 }
830
831 static int ds_set_path(struct ds_device *dev, u64 init)
832 {
833 int err;
834 struct ds_status st;
835 u8 buf[9];
836
837 memcpy(buf, &init, 8);
838 buf[8] = BRANCH_MAIN;
839
840 err = ds_send_data(dev, buf, sizeof(buf));
841 if (err)
842 return err;
843
844 ds_wait_status(dev, &st);
845
846 err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
847 if (err)
848 return err;
849
850 ds_wait_status(dev, &st);
851
852 return 0;
853 }
854
855 #endif /* 0 */
856
ds9490r_touch_bit(void * data,u8 bit)857 static u8 ds9490r_touch_bit(void *data, u8 bit)
858 {
859 struct ds_device *dev = data;
860
861 if (ds_touch_bit(dev, bit, &dev->byte_buf))
862 return 0;
863
864 return dev->byte_buf;
865 }
866
867 #if 0
868 static void ds9490r_write_bit(void *data, u8 bit)
869 {
870 struct ds_device *dev = data;
871
872 ds_write_bit(dev, bit);
873 }
874
875 static u8 ds9490r_read_bit(void *data)
876 {
877 struct ds_device *dev = data;
878 int err;
879
880 err = ds_touch_bit(dev, 1, &dev->byte_buf);
881 if (err)
882 return 0;
883
884 return dev->byte_buf & 1;
885 }
886 #endif
887
ds9490r_write_byte(void * data,u8 byte)888 static void ds9490r_write_byte(void *data, u8 byte)
889 {
890 struct ds_device *dev = data;
891
892 ds_write_byte(dev, byte);
893 }
894
ds9490r_read_byte(void * data)895 static u8 ds9490r_read_byte(void *data)
896 {
897 struct ds_device *dev = data;
898 int err;
899
900 err = ds_read_byte(dev, &dev->byte_buf);
901 if (err)
902 return 0;
903
904 return dev->byte_buf;
905 }
906
ds9490r_write_block(void * data,const u8 * buf,int len)907 static void ds9490r_write_block(void *data, const u8 *buf, int len)
908 {
909 struct ds_device *dev = data;
910 u8 *tbuf;
911
912 if (len <= 0)
913 return;
914
915 tbuf = kmemdup(buf, len, GFP_KERNEL);
916 if (!tbuf)
917 return;
918
919 ds_write_block(dev, tbuf, len);
920
921 kfree(tbuf);
922 }
923
ds9490r_read_block(void * data,u8 * buf,int len)924 static u8 ds9490r_read_block(void *data, u8 *buf, int len)
925 {
926 struct ds_device *dev = data;
927 int err;
928 u8 *tbuf;
929
930 if (len <= 0)
931 return 0;
932
933 tbuf = kmalloc(len, GFP_KERNEL);
934 if (!tbuf)
935 return 0;
936
937 err = ds_read_block(dev, tbuf, len);
938 if (err >= 0)
939 memcpy(buf, tbuf, len);
940
941 kfree(tbuf);
942
943 return err >= 0 ? len : 0;
944 }
945
ds9490r_reset(void * data)946 static u8 ds9490r_reset(void *data)
947 {
948 struct ds_device *dev = data;
949 int err;
950
951 err = ds_reset(dev);
952 if (err)
953 return 1;
954
955 return 0;
956 }
957
ds9490r_set_pullup(void * data,int delay)958 static u8 ds9490r_set_pullup(void *data, int delay)
959 {
960 struct ds_device *dev = data;
961
962 if (ds_set_pullup(dev, delay))
963 return 1;
964
965 return 0;
966 }
967
ds_w1_init(struct ds_device * dev)968 static int ds_w1_init(struct ds_device *dev)
969 {
970 memset(&dev->master, 0, sizeof(struct w1_bus_master));
971
972 /* Reset the device as it can be in a bad state.
973 * This is necessary because a block write will wait for data
974 * to be placed in the output buffer and block any later
975 * commands which will keep accumulating and the device will
976 * not be idle. Another case is removing the ds2490 module
977 * while a bus search is in progress, somehow a few commands
978 * get through, but the input transfers fail leaving data in
979 * the input buffer. This will cause the next read to fail
980 * see the note in ds_recv_data.
981 */
982 ds_reset_device(dev);
983
984 dev->master.data = dev;
985 dev->master.touch_bit = &ds9490r_touch_bit;
986 /* read_bit and write_bit in w1_bus_master are expected to set and
987 * sample the line level. For write_bit that means it is expected to
988 * set it to that value and leave it there. ds2490 only supports an
989 * individual time slot at the lowest level. The requirement from
990 * pulling the bus state down to reading the state is 15us, something
991 * that isn't realistic on the USB bus anyway.
992 dev->master.read_bit = &ds9490r_read_bit;
993 dev->master.write_bit = &ds9490r_write_bit;
994 */
995 dev->master.read_byte = &ds9490r_read_byte;
996 dev->master.write_byte = &ds9490r_write_byte;
997 dev->master.read_block = &ds9490r_read_block;
998 dev->master.write_block = &ds9490r_write_block;
999 dev->master.reset_bus = &ds9490r_reset;
1000 dev->master.set_pullup = &ds9490r_set_pullup;
1001 dev->master.search = &ds9490r_search;
1002
1003 return w1_add_master_device(&dev->master);
1004 }
1005
ds_w1_fini(struct ds_device * dev)1006 static void ds_w1_fini(struct ds_device *dev)
1007 {
1008 w1_remove_master_device(&dev->master);
1009 }
1010
ds_probe(struct usb_interface * intf,const struct usb_device_id * udev_id)1011 static int ds_probe(struct usb_interface *intf,
1012 const struct usb_device_id *udev_id)
1013 {
1014 struct usb_device *udev = interface_to_usbdev(intf);
1015 struct usb_endpoint_descriptor *endpoint;
1016 struct usb_host_interface *iface_desc;
1017 struct ds_device *dev;
1018 int i, err, alt;
1019
1020 dev = kzalloc_obj(struct ds_device);
1021 if (!dev)
1022 return -ENOMEM;
1023
1024 dev->udev = udev;
1025
1026 memset(dev->ep, 0, sizeof(dev->ep));
1027
1028 usb_set_intfdata(intf, dev);
1029
1030 err = usb_reset_configuration(dev->udev);
1031 if (err) {
1032 dev_err(&dev->udev->dev,
1033 "Failed to reset configuration: err=%d.\n", err);
1034 goto err_out_clear;
1035 }
1036
1037 /* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
1038 alt = 3;
1039 err = usb_set_interface(dev->udev,
1040 intf->cur_altsetting->desc.bInterfaceNumber, alt);
1041 if (err) {
1042 dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
1043 "for %d interface: err=%d.\n", alt,
1044 intf->cur_altsetting->desc.bInterfaceNumber, err);
1045 goto err_out_clear;
1046 }
1047
1048 iface_desc = intf->cur_altsetting;
1049 if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
1050 dev_err(&dev->udev->dev, "Num endpoints=%d. It is not DS9490R.\n",
1051 iface_desc->desc.bNumEndpoints);
1052 err = -EINVAL;
1053 goto err_out_clear;
1054 }
1055
1056 /*
1057 * This loop doesn'd show control 0 endpoint,
1058 * so we will fill only 1-3 endpoints entry.
1059 */
1060 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1061 endpoint = &iface_desc->endpoint[i].desc;
1062
1063 dev->ep[i+1] = endpoint->bEndpointAddress;
1064 #if 0
1065 printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
1066 i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
1067 (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
1068 endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1069 #endif
1070 }
1071
1072 err = ds_w1_init(dev);
1073 if (err)
1074 goto err_out_clear;
1075
1076 mutex_lock(&ds_mutex);
1077 list_add_tail(&dev->ds_entry, &ds_devices);
1078 mutex_unlock(&ds_mutex);
1079
1080 return 0;
1081
1082 err_out_clear:
1083 usb_set_intfdata(intf, NULL);
1084 kfree(dev);
1085
1086 return err;
1087 }
1088
ds_disconnect(struct usb_interface * intf)1089 static void ds_disconnect(struct usb_interface *intf)
1090 {
1091 struct ds_device *dev;
1092
1093 dev = usb_get_intfdata(intf);
1094 if (!dev)
1095 return;
1096
1097 mutex_lock(&ds_mutex);
1098 list_del(&dev->ds_entry);
1099 mutex_unlock(&ds_mutex);
1100
1101 ds_w1_fini(dev);
1102
1103 usb_set_intfdata(intf, NULL);
1104
1105 kfree(dev);
1106 }
1107
1108 static const struct usb_device_id ds_id_table[] = {
1109 { USB_DEVICE(0x04fa, 0x2490) },
1110 { },
1111 };
1112 MODULE_DEVICE_TABLE(usb, ds_id_table);
1113
1114 static struct usb_driver ds_driver = {
1115 .name = "DS9490R",
1116 .probe = ds_probe,
1117 .disconnect = ds_disconnect,
1118 .id_table = ds_id_table,
1119 };
1120 module_usb_driver(ds_driver);
1121
1122 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1123 MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
1124 MODULE_LICENSE("GPL");
1125