ds2490.c 26 KB

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