ds2490.c 26 KB

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