ds2490.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. u8 st_buf[ST_SIZE];
  137. u8 byte_buf;
  138. struct w1_bus_master master;
  139. };
  140. struct ds_status
  141. {
  142. u8 enable;
  143. u8 speed;
  144. u8 pullup_dur;
  145. u8 ppuls_dur;
  146. u8 pulldown_slew;
  147. u8 write1_time;
  148. u8 write0_time;
  149. u8 reserved0;
  150. u8 status;
  151. u8 command0;
  152. u8 command1;
  153. u8 command_buffer_status;
  154. u8 data_out_buffer_status;
  155. u8 data_in_buffer_status;
  156. u8 reserved1;
  157. u8 reserved2;
  158. };
  159. static struct usb_device_id ds_id_table [] = {
  160. { USB_DEVICE(0x04fa, 0x2490) },
  161. { },
  162. };
  163. MODULE_DEVICE_TABLE(usb, ds_id_table);
  164. static int ds_probe(struct usb_interface *, const struct usb_device_id *);
  165. static void ds_disconnect(struct usb_interface *);
  166. static int ds_send_control(struct ds_device *, u16, u16);
  167. static int ds_send_control_cmd(struct ds_device *, u16, u16);
  168. static LIST_HEAD(ds_devices);
  169. static DEFINE_MUTEX(ds_mutex);
  170. static struct usb_driver ds_driver = {
  171. .name = "DS9490R",
  172. .probe = ds_probe,
  173. .disconnect = ds_disconnect,
  174. .id_table = ds_id_table,
  175. };
  176. static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
  177. {
  178. int err;
  179. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  180. CONTROL_CMD, VENDOR, value, index, NULL, 0, 1000);
  181. if (err < 0) {
  182. pr_err("Failed to send command control message %x.%x: err=%d.\n",
  183. value, index, err);
  184. return err;
  185. }
  186. return err;
  187. }
  188. static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
  189. {
  190. int err;
  191. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  192. MODE_CMD, VENDOR, value, index, NULL, 0, 1000);
  193. if (err < 0) {
  194. pr_err("Failed to send mode control message %x.%x: err=%d.\n",
  195. value, index, err);
  196. return err;
  197. }
  198. return err;
  199. }
  200. static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
  201. {
  202. int err;
  203. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  204. COMM_CMD, VENDOR, value, index, NULL, 0, 1000);
  205. if (err < 0) {
  206. pr_err("Failed to send control message %x.%x: err=%d.\n",
  207. value, index, err);
  208. return err;
  209. }
  210. return err;
  211. }
  212. static inline void ds_print_msg(unsigned char *buf, unsigned char *str, int off)
  213. {
  214. pr_info("%45s: %8x\n", str, buf[off]);
  215. }
  216. static void ds_dump_status(struct ds_device *dev, unsigned char *buf, int count)
  217. {
  218. int i;
  219. pr_info("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
  220. for (i=0; i<count; ++i)
  221. pr_info("%02x ", buf[i]);
  222. pr_info("\n");
  223. if (count >= 16) {
  224. ds_print_msg(buf, "enable flag", 0);
  225. ds_print_msg(buf, "1-wire speed", 1);
  226. ds_print_msg(buf, "strong pullup duration", 2);
  227. ds_print_msg(buf, "programming pulse duration", 3);
  228. ds_print_msg(buf, "pulldown slew rate control", 4);
  229. ds_print_msg(buf, "write-1 low time", 5);
  230. ds_print_msg(buf, "data sample offset/write-0 recovery time",
  231. 6);
  232. ds_print_msg(buf, "reserved (test register)", 7);
  233. ds_print_msg(buf, "device status flags", 8);
  234. ds_print_msg(buf, "communication command byte 1", 9);
  235. ds_print_msg(buf, "communication command byte 2", 10);
  236. ds_print_msg(buf, "communication command buffer status", 11);
  237. ds_print_msg(buf, "1-wire data output buffer status", 12);
  238. ds_print_msg(buf, "1-wire data input buffer status", 13);
  239. ds_print_msg(buf, "reserved", 14);
  240. ds_print_msg(buf, "reserved", 15);
  241. }
  242. for (i = 16; i < count; ++i) {
  243. if (buf[i] == RR_DETECT) {
  244. ds_print_msg(buf, "new device detect", i);
  245. continue;
  246. }
  247. ds_print_msg(buf, "Result Register Value: ", i);
  248. if (buf[i] & RR_NRS)
  249. pr_info("NRS: Reset no presence or ...\n");
  250. if (buf[i] & RR_SH)
  251. pr_info("SH: short on reset or set path\n");
  252. if (buf[i] & RR_APP)
  253. pr_info("APP: alarming presence on reset\n");
  254. if (buf[i] & RR_VPP)
  255. pr_info("VPP: 12V expected not seen\n");
  256. if (buf[i] & RR_CMP)
  257. pr_info("CMP: compare error\n");
  258. if (buf[i] & RR_CRC)
  259. pr_info("CRC: CRC error detected\n");
  260. if (buf[i] & RR_RDP)
  261. pr_info("RDP: redirected page\n");
  262. if (buf[i] & RR_EOS)
  263. pr_info("EOS: end of search error\n");
  264. }
  265. }
  266. static int ds_recv_status(struct ds_device *dev, struct ds_status *st,
  267. bool dump)
  268. {
  269. int count, err;
  270. if (st)
  271. memset(st, 0, sizeof(*st));
  272. count = 0;
  273. err = usb_interrupt_msg(dev->udev,
  274. usb_rcvintpipe(dev->udev,
  275. dev->ep[EP_STATUS]),
  276. dev->st_buf, sizeof(dev->st_buf),
  277. &count, 1000);
  278. if (err < 0) {
  279. pr_err("Failed to read 1-wire data from 0x%x: err=%d.\n",
  280. dev->ep[EP_STATUS], err);
  281. return err;
  282. }
  283. if (dump)
  284. ds_dump_status(dev, dev->st_buf, count);
  285. if (st && count >= sizeof(*st))
  286. memcpy(st, dev->st_buf, sizeof(*st));
  287. return count;
  288. }
  289. static void ds_reset_device(struct ds_device *dev)
  290. {
  291. ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  292. /* Always allow strong pullup which allow individual writes to use
  293. * the strong pullup.
  294. */
  295. if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
  296. pr_err("ds_reset_device: Error allowing strong pullup\n");
  297. /* Chip strong pullup time was cleared. */
  298. if (dev->spu_sleep) {
  299. /* lower 4 bits are 0, see ds_set_pullup */
  300. u8 del = dev->spu_sleep>>4;
  301. if (ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del))
  302. pr_err("ds_reset_device: Error setting duration\n");
  303. }
  304. }
  305. static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
  306. {
  307. int count, err;
  308. /* Careful on size. If size is less than what is available in
  309. * the input buffer, the device fails the bulk transfer and
  310. * clears the input buffer. It could read the maximum size of
  311. * the data buffer, but then do you return the first, last, or
  312. * some set of the middle size bytes? As long as the rest of
  313. * the code is correct there will be size bytes waiting. A
  314. * call to ds_wait_status will wait until the device is idle
  315. * and any data to be received would have been available.
  316. */
  317. count = 0;
  318. err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
  319. buf, size, &count, 1000);
  320. if (err < 0) {
  321. pr_info("Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
  322. usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
  323. ds_recv_status(dev, NULL, true);
  324. return err;
  325. }
  326. #if 0
  327. {
  328. int i;
  329. printk("%s: count=%d: ", __func__, count);
  330. for (i=0; i<count; ++i)
  331. printk("%02x ", buf[i]);
  332. printk("\n");
  333. }
  334. #endif
  335. return count;
  336. }
  337. static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
  338. {
  339. int count, err;
  340. count = 0;
  341. err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
  342. if (err < 0) {
  343. pr_err("Failed to write 1-wire data to ep0x%x: "
  344. "err=%d.\n", dev->ep[EP_DATA_OUT], err);
  345. return err;
  346. }
  347. return err;
  348. }
  349. #if 0
  350. int ds_stop_pulse(struct ds_device *dev, int limit)
  351. {
  352. struct ds_status st;
  353. int count = 0, err = 0;
  354. do {
  355. err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
  356. if (err)
  357. break;
  358. err = ds_send_control(dev, CTL_RESUME_EXE, 0);
  359. if (err)
  360. break;
  361. err = ds_recv_status(dev, &st, false);
  362. if (err)
  363. break;
  364. if ((st.status & ST_SPUA) == 0) {
  365. err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
  366. if (err)
  367. break;
  368. }
  369. } while(++count < limit);
  370. return err;
  371. }
  372. int ds_detect(struct ds_device *dev, struct ds_status *st)
  373. {
  374. int err;
  375. err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  376. if (err)
  377. return err;
  378. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
  379. if (err)
  380. return err;
  381. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
  382. if (err)
  383. return err;
  384. err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
  385. if (err)
  386. return err;
  387. err = ds_dump_status(dev, st);
  388. return err;
  389. }
  390. #endif /* 0 */
  391. static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
  392. {
  393. int err, count = 0;
  394. do {
  395. st->status = 0;
  396. err = ds_recv_status(dev, st, false);
  397. #if 0
  398. if (err >= 0) {
  399. int i;
  400. printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
  401. for (i=0; i<err; ++i)
  402. printk("%02x ", dev->st_buf[i]);
  403. printk("\n");
  404. }
  405. #endif
  406. } while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
  407. if (err >= 16 && st->status & ST_EPOF) {
  408. pr_info("Resetting device after ST_EPOF.\n");
  409. ds_reset_device(dev);
  410. /* Always dump the device status. */
  411. count = 101;
  412. }
  413. /* Dump the status for errors or if there is extended return data.
  414. * The extended status includes new device detection (maybe someone
  415. * can do something with it).
  416. */
  417. if (err > 16 || count >= 100 || err < 0)
  418. ds_dump_status(dev, dev->st_buf, err);
  419. /* Extended data isn't an error. Well, a short is, but the dump
  420. * would have already told the user that and we can't do anything
  421. * about it in software anyway.
  422. */
  423. if (count >= 100 || err < 0)
  424. return -1;
  425. else
  426. return 0;
  427. }
  428. static int ds_reset(struct ds_device *dev)
  429. {
  430. int err;
  431. /* Other potentionally interesting flags for reset.
  432. *
  433. * COMM_NTF: Return result register feedback. This could be used to
  434. * detect some conditions such as short, alarming presence, or
  435. * detect if a new device was detected.
  436. *
  437. * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
  438. * Select the data transfer rate.
  439. */
  440. err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
  441. if (err)
  442. return err;
  443. return 0;
  444. }
  445. #if 0
  446. static int ds_set_speed(struct ds_device *dev, int speed)
  447. {
  448. int err;
  449. if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
  450. return -EINVAL;
  451. if (speed != SPEED_OVERDRIVE)
  452. speed = SPEED_FLEXIBLE;
  453. speed &= 0xff;
  454. err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
  455. if (err)
  456. return err;
  457. return err;
  458. }
  459. #endif /* 0 */
  460. static int ds_set_pullup(struct ds_device *dev, int delay)
  461. {
  462. int err = 0;
  463. u8 del = 1 + (u8)(delay >> 4);
  464. /* Just storing delay would not get the trunication and roundup. */
  465. int ms = del<<4;
  466. /* Enable spu_bit if a delay is set. */
  467. dev->spu_bit = delay ? COMM_SPU : 0;
  468. /* If delay is zero, it has already been disabled, if the time is
  469. * the same as the hardware was last programmed to, there is also
  470. * nothing more to do. Compare with the recalculated value ms
  471. * rather than del or delay which can have a different value.
  472. */
  473. if (delay == 0 || ms == dev->spu_sleep)
  474. return err;
  475. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
  476. if (err)
  477. return err;
  478. dev->spu_sleep = ms;
  479. return err;
  480. }
  481. static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
  482. {
  483. int err;
  484. struct ds_status st;
  485. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
  486. 0);
  487. if (err)
  488. return err;
  489. ds_wait_status(dev, &st);
  490. err = ds_recv_data(dev, tbit, sizeof(*tbit));
  491. if (err < 0)
  492. return err;
  493. return 0;
  494. }
  495. #if 0
  496. static int ds_write_bit(struct ds_device *dev, u8 bit)
  497. {
  498. int err;
  499. struct ds_status st;
  500. /* Set COMM_ICP to write without a readback. Note, this will
  501. * produce one time slot, a down followed by an up with COMM_D
  502. * only determing the timing.
  503. */
  504. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
  505. (bit ? COMM_D : 0), 0);
  506. if (err)
  507. return err;
  508. ds_wait_status(dev, &st);
  509. return 0;
  510. }
  511. #endif
  512. static int ds_write_byte(struct ds_device *dev, u8 byte)
  513. {
  514. int err;
  515. struct ds_status st;
  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, &dev->byte_buf, 1);
  525. if (err < 0)
  526. return err;
  527. return !(byte == dev->byte_buf);
  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. 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. const size_t bufsize = 2 * 64;
  604. u64 *buf;
  605. buf = kmalloc(bufsize, GFP_KERNEL);
  606. if (!buf)
  607. return;
  608. mutex_lock(&master->bus_mutex);
  609. /* address to start searching at */
  610. if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
  611. goto search_out;
  612. master->search_id = 0;
  613. value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
  614. COMM_RTS;
  615. search_limit = master->max_slave_count;
  616. if (search_limit > 255)
  617. search_limit = 0;
  618. index = search_type | (search_limit << 8);
  619. if (ds_send_control(dev, value, index) < 0)
  620. goto search_out;
  621. do {
  622. schedule_timeout(jtime);
  623. err = ds_recv_status(dev, &st, false);
  624. if (err < 0 || err < sizeof(st))
  625. break;
  626. if (st.data_in_buffer_status) {
  627. /* Bulk in can receive partial ids, but when it does
  628. * they fail crc and will be discarded anyway.
  629. * That has only been seen when status in buffer
  630. * is 0 and bulk is read anyway, so don't read
  631. * bulk without first checking if status says there
  632. * is data to read.
  633. */
  634. err = ds_recv_data(dev, (u8 *)buf, bufsize);
  635. if (err < 0)
  636. break;
  637. for (i = 0; i < err/8; ++i) {
  638. ++found;
  639. if (found <= search_limit)
  640. callback(master, buf[i]);
  641. /* can't know if there will be a discrepancy
  642. * value after until the next id */
  643. if (found == search_limit)
  644. master->search_id = buf[i];
  645. }
  646. }
  647. if (test_bit(W1_ABORT_SEARCH, &master->flags))
  648. break;
  649. } while (!(st.status & (ST_IDLE | ST_HALT)));
  650. /* only continue the search if some weren't found */
  651. if (found <= search_limit) {
  652. master->search_id = 0;
  653. } else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
  654. /* Only max_slave_count will be scanned in a search,
  655. * but it will start where it left off next search
  656. * until all ids are identified and then it will start
  657. * over. A continued search will report the previous
  658. * last id as the first id (provided it is still on the
  659. * bus).
  660. */
  661. dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
  662. "will continue next search.\n", __func__,
  663. master->max_slave_count);
  664. set_bit(W1_WARN_MAX_COUNT, &master->flags);
  665. }
  666. search_out:
  667. mutex_unlock(&master->bus_mutex);
  668. kfree(buf);
  669. }
  670. #if 0
  671. /*
  672. * FIXME: if this disabled code is ever used in the future all ds_send_data()
  673. * calls must be changed to use a DMAable buffer.
  674. */
  675. static int ds_match_access(struct ds_device *dev, u64 init)
  676. {
  677. int err;
  678. struct ds_status st;
  679. err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
  680. if (err)
  681. return err;
  682. ds_wait_status(dev, &st);
  683. err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
  684. if (err)
  685. return err;
  686. ds_wait_status(dev, &st);
  687. return 0;
  688. }
  689. static int ds_set_path(struct ds_device *dev, u64 init)
  690. {
  691. int err;
  692. struct ds_status st;
  693. u8 buf[9];
  694. memcpy(buf, &init, 8);
  695. buf[8] = BRANCH_MAIN;
  696. err = ds_send_data(dev, buf, sizeof(buf));
  697. if (err)
  698. return err;
  699. ds_wait_status(dev, &st);
  700. err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
  701. if (err)
  702. return err;
  703. ds_wait_status(dev, &st);
  704. return 0;
  705. }
  706. #endif /* 0 */
  707. static u8 ds9490r_touch_bit(void *data, u8 bit)
  708. {
  709. struct ds_device *dev = data;
  710. if (ds_touch_bit(dev, bit, &dev->byte_buf))
  711. return 0;
  712. return dev->byte_buf;
  713. }
  714. #if 0
  715. static void ds9490r_write_bit(void *data, u8 bit)
  716. {
  717. struct ds_device *dev = data;
  718. ds_write_bit(dev, bit);
  719. }
  720. static u8 ds9490r_read_bit(void *data)
  721. {
  722. struct ds_device *dev = data;
  723. int err;
  724. err = ds_touch_bit(dev, 1, &dev->byte_buf);
  725. if (err)
  726. return 0;
  727. return dev->byte_buf & 1;
  728. }
  729. #endif
  730. static void ds9490r_write_byte(void *data, u8 byte)
  731. {
  732. struct ds_device *dev = data;
  733. ds_write_byte(dev, byte);
  734. }
  735. static u8 ds9490r_read_byte(void *data)
  736. {
  737. struct ds_device *dev = data;
  738. int err;
  739. err = ds_read_byte(dev, &dev->byte_buf);
  740. if (err)
  741. return 0;
  742. return dev->byte_buf;
  743. }
  744. static void ds9490r_write_block(void *data, const u8 *buf, int len)
  745. {
  746. struct ds_device *dev = data;
  747. u8 *tbuf;
  748. if (len <= 0)
  749. return;
  750. tbuf = kmalloc(len, GFP_KERNEL);
  751. if (!tbuf)
  752. return;
  753. memcpy(tbuf, buf, len);
  754. ds_write_block(dev, tbuf, len);
  755. kfree(tbuf);
  756. }
  757. static u8 ds9490r_read_block(void *data, u8 *buf, int len)
  758. {
  759. struct ds_device *dev = data;
  760. int err;
  761. u8 *tbuf;
  762. if (len <= 0)
  763. return 0;
  764. tbuf = kmalloc(len, GFP_KERNEL);
  765. if (!tbuf)
  766. return 0;
  767. err = ds_read_block(dev, tbuf, len);
  768. if (err >= 0)
  769. memcpy(buf, tbuf, len);
  770. kfree(tbuf);
  771. return err >= 0 ? len : 0;
  772. }
  773. static u8 ds9490r_reset(void *data)
  774. {
  775. struct ds_device *dev = data;
  776. int err;
  777. err = ds_reset(dev);
  778. if (err)
  779. return 1;
  780. return 0;
  781. }
  782. static u8 ds9490r_set_pullup(void *data, int delay)
  783. {
  784. struct ds_device *dev = data;
  785. if (ds_set_pullup(dev, delay))
  786. return 1;
  787. return 0;
  788. }
  789. static int ds_w1_init(struct ds_device *dev)
  790. {
  791. memset(&dev->master, 0, sizeof(struct w1_bus_master));
  792. /* Reset the device as it can be in a bad state.
  793. * This is necessary because a block write will wait for data
  794. * to be placed in the output buffer and block any later
  795. * commands which will keep accumulating and the device will
  796. * not be idle. Another case is removing the ds2490 module
  797. * while a bus search is in progress, somehow a few commands
  798. * get through, but the input transfers fail leaving data in
  799. * the input buffer. This will cause the next read to fail
  800. * see the note in ds_recv_data.
  801. */
  802. ds_reset_device(dev);
  803. dev->master.data = dev;
  804. dev->master.touch_bit = &ds9490r_touch_bit;
  805. /* read_bit and write_bit in w1_bus_master are expected to set and
  806. * sample the line level. For write_bit that means it is expected to
  807. * set it to that value and leave it there. ds2490 only supports an
  808. * individual time slot at the lowest level. The requirement from
  809. * pulling the bus state down to reading the state is 15us, something
  810. * that isn't realistic on the USB bus anyway.
  811. dev->master.read_bit = &ds9490r_read_bit;
  812. dev->master.write_bit = &ds9490r_write_bit;
  813. */
  814. dev->master.read_byte = &ds9490r_read_byte;
  815. dev->master.write_byte = &ds9490r_write_byte;
  816. dev->master.read_block = &ds9490r_read_block;
  817. dev->master.write_block = &ds9490r_write_block;
  818. dev->master.reset_bus = &ds9490r_reset;
  819. dev->master.set_pullup = &ds9490r_set_pullup;
  820. dev->master.search = &ds9490r_search;
  821. return w1_add_master_device(&dev->master);
  822. }
  823. static void ds_w1_fini(struct ds_device *dev)
  824. {
  825. w1_remove_master_device(&dev->master);
  826. }
  827. static int ds_probe(struct usb_interface *intf,
  828. const struct usb_device_id *udev_id)
  829. {
  830. struct usb_device *udev = interface_to_usbdev(intf);
  831. struct usb_endpoint_descriptor *endpoint;
  832. struct usb_host_interface *iface_desc;
  833. struct ds_device *dev;
  834. int i, err, alt;
  835. dev = kzalloc(sizeof(struct ds_device), GFP_KERNEL);
  836. if (!dev) {
  837. pr_info("Failed to allocate new DS9490R structure.\n");
  838. return -ENOMEM;
  839. }
  840. dev->udev = usb_get_dev(udev);
  841. if (!dev->udev) {
  842. err = -ENOMEM;
  843. goto err_out_free;
  844. }
  845. memset(dev->ep, 0, sizeof(dev->ep));
  846. usb_set_intfdata(intf, dev);
  847. err = usb_reset_configuration(dev->udev);
  848. if (err) {
  849. dev_err(&dev->udev->dev,
  850. "Failed to reset configuration: err=%d.\n", err);
  851. goto err_out_clear;
  852. }
  853. /* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
  854. alt = 3;
  855. err = usb_set_interface(dev->udev,
  856. intf->altsetting[alt].desc.bInterfaceNumber, alt);
  857. if (err) {
  858. dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
  859. "for %d interface: err=%d.\n", alt,
  860. intf->altsetting[alt].desc.bInterfaceNumber, err);
  861. goto err_out_clear;
  862. }
  863. iface_desc = &intf->altsetting[alt];
  864. if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
  865. pr_info("Num endpoints=%d. It is not DS9490R.\n",
  866. iface_desc->desc.bNumEndpoints);
  867. err = -EINVAL;
  868. goto err_out_clear;
  869. }
  870. /*
  871. * This loop doesn'd show control 0 endpoint,
  872. * so we will fill only 1-3 endpoints entry.
  873. */
  874. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  875. endpoint = &iface_desc->endpoint[i].desc;
  876. dev->ep[i+1] = endpoint->bEndpointAddress;
  877. #if 0
  878. printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
  879. i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
  880. (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
  881. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  882. #endif
  883. }
  884. err = ds_w1_init(dev);
  885. if (err)
  886. goto err_out_clear;
  887. mutex_lock(&ds_mutex);
  888. list_add_tail(&dev->ds_entry, &ds_devices);
  889. mutex_unlock(&ds_mutex);
  890. return 0;
  891. err_out_clear:
  892. usb_set_intfdata(intf, NULL);
  893. usb_put_dev(dev->udev);
  894. err_out_free:
  895. kfree(dev);
  896. return err;
  897. }
  898. static void ds_disconnect(struct usb_interface *intf)
  899. {
  900. struct ds_device *dev;
  901. dev = usb_get_intfdata(intf);
  902. if (!dev)
  903. return;
  904. mutex_lock(&ds_mutex);
  905. list_del(&dev->ds_entry);
  906. mutex_unlock(&ds_mutex);
  907. ds_w1_fini(dev);
  908. usb_set_intfdata(intf, NULL);
  909. usb_put_dev(dev->udev);
  910. kfree(dev);
  911. }
  912. module_usb_driver(ds_driver);
  913. MODULE_LICENSE("GPL");
  914. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  915. MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");