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