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. printk(KERN_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. printk(KERN_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. printk(KERN_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, 100);
  218. if (err < 0) {
  219. printk(KERN_ERR "Failed to read 1-wire data from 0x%x: err=%d.\n", dev->ep[EP_STATUS], err);
  220. return err;
  221. }
  222. if (count >= sizeof(*st))
  223. memcpy(st, buf, sizeof(*st));
  224. return count;
  225. }
  226. static inline void ds_print_msg(unsigned char *buf, unsigned char *str, int off)
  227. {
  228. printk(KERN_INFO "%45s: %8x\n", str, buf[off]);
  229. }
  230. static void ds_dump_status(struct ds_device *dev, unsigned char *buf, int count)
  231. {
  232. int i;
  233. printk(KERN_INFO "0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
  234. for (i=0; i<count; ++i)
  235. printk("%02x ", buf[i]);
  236. printk(KERN_INFO "\n");
  237. if (count >= 16) {
  238. ds_print_msg(buf, "enable flag", 0);
  239. ds_print_msg(buf, "1-wire speed", 1);
  240. ds_print_msg(buf, "strong pullup duration", 2);
  241. ds_print_msg(buf, "programming pulse duration", 3);
  242. ds_print_msg(buf, "pulldown slew rate control", 4);
  243. ds_print_msg(buf, "write-1 low time", 5);
  244. ds_print_msg(buf, "data sample offset/write-0 recovery time",
  245. 6);
  246. ds_print_msg(buf, "reserved (test register)", 7);
  247. ds_print_msg(buf, "device status flags", 8);
  248. ds_print_msg(buf, "communication command byte 1", 9);
  249. ds_print_msg(buf, "communication command byte 2", 10);
  250. ds_print_msg(buf, "communication command buffer status", 11);
  251. ds_print_msg(buf, "1-wire data output buffer status", 12);
  252. ds_print_msg(buf, "1-wire data input buffer status", 13);
  253. ds_print_msg(buf, "reserved", 14);
  254. ds_print_msg(buf, "reserved", 15);
  255. }
  256. for (i = 16; i < count; ++i) {
  257. if (buf[i] == RR_DETECT) {
  258. ds_print_msg(buf, "new device detect", i);
  259. continue;
  260. }
  261. ds_print_msg(buf, "Result Register Value: ", i);
  262. if (buf[i] & RR_NRS)
  263. printk(KERN_INFO "NRS: Reset no presence or ...\n");
  264. if (buf[i] & RR_SH)
  265. printk(KERN_INFO "SH: short on reset or set path\n");
  266. if (buf[i] & RR_APP)
  267. printk(KERN_INFO "APP: alarming presence on reset\n");
  268. if (buf[i] & RR_VPP)
  269. printk(KERN_INFO "VPP: 12V expected not seen\n");
  270. if (buf[i] & RR_CMP)
  271. printk(KERN_INFO "CMP: compare error\n");
  272. if (buf[i] & RR_CRC)
  273. printk(KERN_INFO "CRC: CRC error detected\n");
  274. if (buf[i] & RR_RDP)
  275. printk(KERN_INFO "RDP: redirected page\n");
  276. if (buf[i] & RR_EOS)
  277. printk(KERN_INFO "EOS: end of search error\n");
  278. }
  279. }
  280. static void ds_reset_device(struct ds_device *dev)
  281. {
  282. ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  283. /* Always allow strong pullup which allow individual writes to use
  284. * the strong pullup.
  285. */
  286. if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
  287. printk(KERN_ERR "ds_reset_device: "
  288. "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. printk(KERN_ERR "ds_reset_device: "
  295. "Error setting duration\n");
  296. }
  297. }
  298. static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
  299. {
  300. int count, err;
  301. struct ds_status st;
  302. /* Careful on size. If size is less than what is available in
  303. * the input buffer, the device fails the bulk transfer and
  304. * clears the input buffer. It could read the maximum size of
  305. * the data buffer, but then do you return the first, last, or
  306. * some set of the middle size bytes? As long as the rest of
  307. * the code is correct there will be size bytes waiting. A
  308. * call to ds_wait_status will wait until the device is idle
  309. * and any data to be received would have been available.
  310. */
  311. count = 0;
  312. err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
  313. buf, size, &count, 1000);
  314. if (err < 0) {
  315. u8 buf[ST_SIZE];
  316. int count;
  317. printk(KERN_INFO "Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
  318. usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
  319. count = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
  320. ds_dump_status(dev, buf, count);
  321. return err;
  322. }
  323. #if 0
  324. {
  325. int i;
  326. printk("%s: count=%d: ", __func__, count);
  327. for (i=0; i<count; ++i)
  328. printk("%02x ", buf[i]);
  329. printk("\n");
  330. }
  331. #endif
  332. return count;
  333. }
  334. static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
  335. {
  336. int count, err;
  337. count = 0;
  338. err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
  339. if (err < 0) {
  340. printk(KERN_ERR "Failed to write 1-wire data to ep0x%x: "
  341. "err=%d.\n", dev->ep[EP_DATA_OUT], err);
  342. return err;
  343. }
  344. return err;
  345. }
  346. #if 0
  347. int ds_stop_pulse(struct ds_device *dev, int limit)
  348. {
  349. struct ds_status st;
  350. int count = 0, err = 0;
  351. u8 buf[ST_SIZE];
  352. do {
  353. err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
  354. if (err)
  355. break;
  356. err = ds_send_control(dev, CTL_RESUME_EXE, 0);
  357. if (err)
  358. break;
  359. err = ds_recv_status_nodump(dev, &st, buf, sizeof(buf));
  360. if (err)
  361. break;
  362. if ((st.status & ST_SPUA) == 0) {
  363. err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
  364. if (err)
  365. break;
  366. }
  367. } while(++count < limit);
  368. return err;
  369. }
  370. int ds_detect(struct ds_device *dev, struct ds_status *st)
  371. {
  372. int err;
  373. err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  374. if (err)
  375. return err;
  376. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
  377. if (err)
  378. return err;
  379. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
  380. if (err)
  381. return err;
  382. err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
  383. if (err)
  384. return err;
  385. err = ds_dump_status(dev, st);
  386. return err;
  387. }
  388. #endif /* 0 */
  389. static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
  390. {
  391. u8 buf[ST_SIZE];
  392. int err, count = 0;
  393. do {
  394. st->status = 0;
  395. err = ds_recv_status_nodump(dev, st, buf, sizeof(buf));
  396. #if 0
  397. if (err >= 0) {
  398. int i;
  399. printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
  400. for (i=0; i<err; ++i)
  401. printk("%02x ", buf[i]);
  402. printk("\n");
  403. }
  404. #endif
  405. } while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
  406. if (err >= 16 && st->status & ST_EPOF) {
  407. printk(KERN_INFO "Resetting device after ST_EPOF.\n");
  408. ds_reset_device(dev);
  409. /* Always dump the device status. */
  410. count = 101;
  411. }
  412. /* Dump the status for errors or if there is extended return data.
  413. * The extended status includes new device detection (maybe someone
  414. * can do something with it).
  415. */
  416. if (err > 16 || count >= 100 || err < 0)
  417. ds_dump_status(dev, buf, err);
  418. /* Extended data isn't an error. Well, a short is, but the dump
  419. * would have already told the user that and we can't do anything
  420. * about it in software anyway.
  421. */
  422. if (count >= 100 || err < 0)
  423. return -1;
  424. else
  425. return 0;
  426. }
  427. static int ds_reset(struct ds_device *dev)
  428. {
  429. int err;
  430. /* Other potentionally interesting flags for reset.
  431. *
  432. * COMM_NTF: Return result register feedback. This could be used to
  433. * detect some conditions such as short, alarming presence, or
  434. * detect if a new device was detected.
  435. *
  436. * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
  437. * Select the data transfer rate.
  438. */
  439. err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
  440. if (err)
  441. return err;
  442. return 0;
  443. }
  444. #if 0
  445. static int ds_set_speed(struct ds_device *dev, int speed)
  446. {
  447. int err;
  448. if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
  449. return -EINVAL;
  450. if (speed != SPEED_OVERDRIVE)
  451. speed = SPEED_FLEXIBLE;
  452. speed &= 0xff;
  453. err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
  454. if (err)
  455. return err;
  456. return err;
  457. }
  458. #endif /* 0 */
  459. static int ds_set_pullup(struct ds_device *dev, int delay)
  460. {
  461. int err = 0;
  462. u8 del = 1 + (u8)(delay >> 4);
  463. /* Just storing delay would not get the trunication and roundup. */
  464. int ms = del<<4;
  465. /* Enable spu_bit if a delay is set. */
  466. dev->spu_bit = delay ? COMM_SPU : 0;
  467. /* If delay is zero, it has already been disabled, if the time is
  468. * the same as the hardware was last programmed to, there is also
  469. * nothing more to do. Compare with the recalculated value ms
  470. * rather than del or delay which can have a different value.
  471. */
  472. if (delay == 0 || ms == dev->spu_sleep)
  473. return err;
  474. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
  475. if (err)
  476. return err;
  477. dev->spu_sleep = ms;
  478. return err;
  479. }
  480. static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
  481. {
  482. int err;
  483. struct ds_status st;
  484. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
  485. 0);
  486. if (err)
  487. return err;
  488. ds_wait_status(dev, &st);
  489. err = ds_recv_data(dev, tbit, sizeof(*tbit));
  490. if (err < 0)
  491. return err;
  492. return 0;
  493. }
  494. #if 0
  495. static int ds_write_bit(struct ds_device *dev, u8 bit)
  496. {
  497. int err;
  498. struct ds_status st;
  499. /* Set COMM_ICP to write without a readback. Note, this will
  500. * produce one time slot, a down followed by an up with COMM_D
  501. * only determing the timing.
  502. */
  503. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
  504. (bit ? COMM_D : 0), 0);
  505. if (err)
  506. return err;
  507. ds_wait_status(dev, &st);
  508. return 0;
  509. }
  510. #endif
  511. static int ds_write_byte(struct ds_device *dev, u8 byte)
  512. {
  513. int err;
  514. struct ds_status st;
  515. u8 rbyte;
  516. err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | dev->spu_bit, byte);
  517. if (err)
  518. return err;
  519. if (dev->spu_bit)
  520. msleep(dev->spu_sleep);
  521. err = ds_wait_status(dev, &st);
  522. if (err)
  523. return err;
  524. err = ds_recv_data(dev, &rbyte, sizeof(rbyte));
  525. if (err < 0)
  526. return err;
  527. return !(byte == rbyte);
  528. }
  529. static int ds_read_byte(struct ds_device *dev, u8 *byte)
  530. {
  531. int err;
  532. struct ds_status st;
  533. err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM , 0xff);
  534. if (err)
  535. return err;
  536. ds_wait_status(dev, &st);
  537. err = ds_recv_data(dev, byte, sizeof(*byte));
  538. if (err < 0)
  539. return err;
  540. return 0;
  541. }
  542. static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
  543. {
  544. struct ds_status st;
  545. int err;
  546. if (len > 64*1024)
  547. return -E2BIG;
  548. memset(buf, 0xFF, len);
  549. err = ds_send_data(dev, buf, len);
  550. if (err < 0)
  551. return err;
  552. err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
  553. if (err)
  554. return err;
  555. ds_wait_status(dev, &st);
  556. memset(buf, 0x00, len);
  557. err = ds_recv_data(dev, buf, len);
  558. return err;
  559. }
  560. static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
  561. {
  562. int err;
  563. struct ds_status st;
  564. err = ds_send_data(dev, buf, len);
  565. if (err < 0)
  566. return err;
  567. err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | dev->spu_bit, len);
  568. if (err)
  569. return err;
  570. if (dev->spu_bit)
  571. msleep(dev->spu_sleep);
  572. ds_wait_status(dev, &st);
  573. err = ds_recv_data(dev, buf, len);
  574. if (err < 0)
  575. return err;
  576. return !(err == len);
  577. }
  578. static void ds9490r_search(void *data, struct w1_master *master,
  579. u8 search_type, w1_slave_found_callback callback)
  580. {
  581. /* When starting with an existing id, the first id returned will
  582. * be that device (if it is still on the bus most likely).
  583. *
  584. * If the number of devices found is less than or equal to the
  585. * search_limit, that number of IDs will be returned. If there are
  586. * more, search_limit IDs will be returned followed by a non-zero
  587. * discrepency value.
  588. */
  589. struct ds_device *dev = data;
  590. int err;
  591. u16 value, index;
  592. struct ds_status st;
  593. u8 st_buf[ST_SIZE];
  594. int search_limit;
  595. int found = 0;
  596. int i;
  597. /* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
  598. * discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
  599. */
  600. const unsigned long jtime = msecs_to_jiffies(1000*8/75);
  601. /* FIFO 128 bytes, bulk packet size 64, read a multiple of the
  602. * packet size.
  603. */
  604. u64 buf[2*64/8];
  605. mutex_lock(&master->bus_mutex);
  606. /* address to start searching at */
  607. if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
  608. goto search_out;
  609. master->search_id = 0;
  610. value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
  611. COMM_RTS;
  612. search_limit = master->max_slave_count;
  613. if (search_limit > 255)
  614. search_limit = 0;
  615. index = search_type | (search_limit << 8);
  616. if (ds_send_control(dev, value, index) < 0)
  617. goto search_out;
  618. do {
  619. schedule_timeout(jtime);
  620. if (ds_recv_status_nodump(dev, &st, st_buf, sizeof(st_buf)) <
  621. sizeof(st)) {
  622. break;
  623. }
  624. if (st.data_in_buffer_status) {
  625. /* Bulk in can receive partial ids, but when it does
  626. * they fail crc and will be discarded anyway.
  627. * That has only been seen when status in buffer
  628. * is 0 and bulk is read anyway, so don't read
  629. * bulk without first checking if status says there
  630. * is data to read.
  631. */
  632. err = ds_recv_data(dev, (u8 *)buf, sizeof(buf));
  633. if (err < 0)
  634. break;
  635. for (i = 0; i < err/8; ++i) {
  636. ++found;
  637. if (found <= search_limit)
  638. callback(master, buf[i]);
  639. /* can't know if there will be a discrepancy
  640. * value after until the next id */
  641. if (found == search_limit)
  642. master->search_id = buf[i];
  643. }
  644. }
  645. if (test_bit(W1_ABORT_SEARCH, &master->flags))
  646. break;
  647. } while (!(st.status & (ST_IDLE | ST_HALT)));
  648. /* only continue the search if some weren't found */
  649. if (found <= search_limit) {
  650. master->search_id = 0;
  651. } else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
  652. /* Only max_slave_count will be scanned in a search,
  653. * but it will start where it left off next search
  654. * until all ids are identified and then it will start
  655. * over. A continued search will report the previous
  656. * last id as the first id (provided it is still on the
  657. * bus).
  658. */
  659. dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
  660. "will continue next search.\n", __func__,
  661. master->max_slave_count);
  662. set_bit(W1_WARN_MAX_COUNT, &master->flags);
  663. }
  664. search_out:
  665. mutex_unlock(&master->bus_mutex);
  666. }
  667. #if 0
  668. static int ds_match_access(struct ds_device *dev, u64 init)
  669. {
  670. int err;
  671. struct ds_status st;
  672. err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
  673. if (err)
  674. return err;
  675. ds_wait_status(dev, &st);
  676. err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
  677. if (err)
  678. return err;
  679. ds_wait_status(dev, &st);
  680. return 0;
  681. }
  682. static int ds_set_path(struct ds_device *dev, u64 init)
  683. {
  684. int err;
  685. struct ds_status st;
  686. u8 buf[9];
  687. memcpy(buf, &init, 8);
  688. buf[8] = BRANCH_MAIN;
  689. err = ds_send_data(dev, buf, sizeof(buf));
  690. if (err)
  691. return err;
  692. ds_wait_status(dev, &st);
  693. err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
  694. if (err)
  695. return err;
  696. ds_wait_status(dev, &st);
  697. return 0;
  698. }
  699. #endif /* 0 */
  700. static u8 ds9490r_touch_bit(void *data, u8 bit)
  701. {
  702. u8 ret;
  703. struct ds_device *dev = data;
  704. if (ds_touch_bit(dev, bit, &ret))
  705. return 0;
  706. return ret;
  707. }
  708. #if 0
  709. static void ds9490r_write_bit(void *data, u8 bit)
  710. {
  711. struct ds_device *dev = data;
  712. ds_write_bit(dev, bit);
  713. }
  714. static u8 ds9490r_read_bit(void *data)
  715. {
  716. struct ds_device *dev = data;
  717. int err;
  718. u8 bit = 0;
  719. err = ds_touch_bit(dev, 1, &bit);
  720. if (err)
  721. return 0;
  722. return bit & 1;
  723. }
  724. #endif
  725. static void ds9490r_write_byte(void *data, u8 byte)
  726. {
  727. struct ds_device *dev = data;
  728. ds_write_byte(dev, byte);
  729. }
  730. static u8 ds9490r_read_byte(void *data)
  731. {
  732. struct ds_device *dev = data;
  733. int err;
  734. u8 byte = 0;
  735. err = ds_read_byte(dev, &byte);
  736. if (err)
  737. return 0;
  738. return byte;
  739. }
  740. static void ds9490r_write_block(void *data, const u8 *buf, int len)
  741. {
  742. struct ds_device *dev = data;
  743. ds_write_block(dev, (u8 *)buf, len);
  744. }
  745. static u8 ds9490r_read_block(void *data, u8 *buf, int len)
  746. {
  747. struct ds_device *dev = data;
  748. int err;
  749. err = ds_read_block(dev, buf, len);
  750. if (err < 0)
  751. return 0;
  752. return len;
  753. }
  754. static u8 ds9490r_reset(void *data)
  755. {
  756. struct ds_device *dev = data;
  757. int err;
  758. err = ds_reset(dev);
  759. if (err)
  760. return 1;
  761. return 0;
  762. }
  763. static u8 ds9490r_set_pullup(void *data, int delay)
  764. {
  765. struct ds_device *dev = data;
  766. if (ds_set_pullup(dev, delay))
  767. return 1;
  768. return 0;
  769. }
  770. static int ds_w1_init(struct ds_device *dev)
  771. {
  772. memset(&dev->master, 0, sizeof(struct w1_bus_master));
  773. /* Reset the device as it can be in a bad state.
  774. * This is necessary because a block write will wait for data
  775. * to be placed in the output buffer and block any later
  776. * commands which will keep accumulating and the device will
  777. * not be idle. Another case is removing the ds2490 module
  778. * while a bus search is in progress, somehow a few commands
  779. * get through, but the input transfers fail leaving data in
  780. * the input buffer. This will cause the next read to fail
  781. * see the note in ds_recv_data.
  782. */
  783. ds_reset_device(dev);
  784. dev->master.data = dev;
  785. dev->master.touch_bit = &ds9490r_touch_bit;
  786. /* read_bit and write_bit in w1_bus_master are expected to set and
  787. * sample the line level. For write_bit that means it is expected to
  788. * set it to that value and leave it there. ds2490 only supports an
  789. * individual time slot at the lowest level. The requirement from
  790. * pulling the bus state down to reading the state is 15us, something
  791. * that isn't realistic on the USB bus anyway.
  792. dev->master.read_bit = &ds9490r_read_bit;
  793. dev->master.write_bit = &ds9490r_write_bit;
  794. */
  795. dev->master.read_byte = &ds9490r_read_byte;
  796. dev->master.write_byte = &ds9490r_write_byte;
  797. dev->master.read_block = &ds9490r_read_block;
  798. dev->master.write_block = &ds9490r_write_block;
  799. dev->master.reset_bus = &ds9490r_reset;
  800. dev->master.set_pullup = &ds9490r_set_pullup;
  801. dev->master.search = &ds9490r_search;
  802. return w1_add_master_device(&dev->master);
  803. }
  804. static void ds_w1_fini(struct ds_device *dev)
  805. {
  806. w1_remove_master_device(&dev->master);
  807. }
  808. static int ds_probe(struct usb_interface *intf,
  809. const struct usb_device_id *udev_id)
  810. {
  811. struct usb_device *udev = interface_to_usbdev(intf);
  812. struct usb_endpoint_descriptor *endpoint;
  813. struct usb_host_interface *iface_desc;
  814. struct ds_device *dev;
  815. int i, err, alt;
  816. dev = kzalloc(sizeof(struct ds_device), GFP_KERNEL);
  817. if (!dev) {
  818. printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
  819. return -ENOMEM;
  820. }
  821. dev->udev = usb_get_dev(udev);
  822. if (!dev->udev) {
  823. err = -ENOMEM;
  824. goto err_out_free;
  825. }
  826. memset(dev->ep, 0, sizeof(dev->ep));
  827. usb_set_intfdata(intf, dev);
  828. err = usb_reset_configuration(dev->udev);
  829. if (err) {
  830. dev_err(&dev->udev->dev,
  831. "Failed to reset configuration: err=%d.\n", err);
  832. goto err_out_clear;
  833. }
  834. /* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
  835. alt = 3;
  836. err = usb_set_interface(dev->udev,
  837. intf->altsetting[alt].desc.bInterfaceNumber, alt);
  838. if (err) {
  839. dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
  840. "for %d interface: err=%d.\n", alt,
  841. intf->altsetting[alt].desc.bInterfaceNumber, err);
  842. goto err_out_clear;
  843. }
  844. iface_desc = &intf->altsetting[alt];
  845. if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
  846. printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", 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*)");