ds2490.c 26 KB

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