ds2490.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. struct list_head ds_entry;
  123. struct usb_device *udev;
  124. struct usb_interface *intf;
  125. int ep[NUM_EP];
  126. /* Strong PullUp
  127. * 0: pullup not active, else duration in milliseconds
  128. */
  129. int spu_sleep;
  130. /* spu_bit contains COMM_SPU or 0 depending on if the strong pullup
  131. * should be active or not for writes.
  132. */
  133. u16 spu_bit;
  134. u8 st_buf[ST_SIZE];
  135. u8 byte_buf;
  136. struct w1_bus_master master;
  137. };
  138. struct ds_status {
  139. u8 enable;
  140. u8 speed;
  141. u8 pullup_dur;
  142. u8 ppuls_dur;
  143. u8 pulldown_slew;
  144. u8 write1_time;
  145. u8 write0_time;
  146. u8 reserved0;
  147. u8 status;
  148. u8 command0;
  149. u8 command1;
  150. u8 command_buffer_status;
  151. u8 data_out_buffer_status;
  152. u8 data_in_buffer_status;
  153. u8 reserved1;
  154. u8 reserved2;
  155. };
  156. static LIST_HEAD(ds_devices);
  157. static DEFINE_MUTEX(ds_mutex);
  158. static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
  159. {
  160. int err;
  161. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  162. CONTROL_CMD, VENDOR, value, index, NULL, 0, 1000);
  163. if (err < 0) {
  164. pr_err("Failed to send command control message %x.%x: err=%d.\n",
  165. value, index, err);
  166. return err;
  167. }
  168. return err;
  169. }
  170. static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
  171. {
  172. int err;
  173. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  174. MODE_CMD, VENDOR, value, index, NULL, 0, 1000);
  175. if (err < 0) {
  176. pr_err("Failed to send mode control message %x.%x: err=%d.\n",
  177. value, index, err);
  178. return err;
  179. }
  180. return err;
  181. }
  182. static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
  183. {
  184. int err;
  185. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, dev->ep[EP_CONTROL]),
  186. COMM_CMD, VENDOR, value, index, NULL, 0, 1000);
  187. if (err < 0) {
  188. pr_err("Failed to send control message %x.%x: err=%d.\n",
  189. value, index, err);
  190. return err;
  191. }
  192. return err;
  193. }
  194. static inline void ds_print_msg(unsigned char *buf, unsigned char *str, int off)
  195. {
  196. pr_info("%45s: %8x\n", str, buf[off]);
  197. }
  198. static void ds_dump_status(struct ds_device *dev, unsigned char *buf, int count)
  199. {
  200. int i;
  201. pr_info("0x%x: count=%d, status: ", dev->ep[EP_STATUS], count);
  202. for (i = 0; i < count; ++i)
  203. pr_info("%02x ", buf[i]);
  204. pr_info("\n");
  205. if (count >= 16) {
  206. ds_print_msg(buf, "enable flag", 0);
  207. ds_print_msg(buf, "1-wire speed", 1);
  208. ds_print_msg(buf, "strong pullup duration", 2);
  209. ds_print_msg(buf, "programming pulse duration", 3);
  210. ds_print_msg(buf, "pulldown slew rate control", 4);
  211. ds_print_msg(buf, "write-1 low time", 5);
  212. ds_print_msg(buf, "data sample offset/write-0 recovery time",
  213. 6);
  214. ds_print_msg(buf, "reserved (test register)", 7);
  215. ds_print_msg(buf, "device status flags", 8);
  216. ds_print_msg(buf, "communication command byte 1", 9);
  217. ds_print_msg(buf, "communication command byte 2", 10);
  218. ds_print_msg(buf, "communication command buffer status", 11);
  219. ds_print_msg(buf, "1-wire data output buffer status", 12);
  220. ds_print_msg(buf, "1-wire data input buffer status", 13);
  221. ds_print_msg(buf, "reserved", 14);
  222. ds_print_msg(buf, "reserved", 15);
  223. }
  224. for (i = 16; i < count; ++i) {
  225. if (buf[i] == RR_DETECT) {
  226. ds_print_msg(buf, "new device detect", i);
  227. continue;
  228. }
  229. ds_print_msg(buf, "Result Register Value: ", i);
  230. if (buf[i] & RR_NRS)
  231. pr_info("NRS: Reset no presence or ...\n");
  232. if (buf[i] & RR_SH)
  233. pr_info("SH: short on reset or set path\n");
  234. if (buf[i] & RR_APP)
  235. pr_info("APP: alarming presence on reset\n");
  236. if (buf[i] & RR_VPP)
  237. pr_info("VPP: 12V expected not seen\n");
  238. if (buf[i] & RR_CMP)
  239. pr_info("CMP: compare error\n");
  240. if (buf[i] & RR_CRC)
  241. pr_info("CRC: CRC error detected\n");
  242. if (buf[i] & RR_RDP)
  243. pr_info("RDP: redirected page\n");
  244. if (buf[i] & RR_EOS)
  245. pr_info("EOS: end of search error\n");
  246. }
  247. }
  248. static int ds_recv_status(struct ds_device *dev, struct ds_status *st,
  249. bool dump)
  250. {
  251. int count, err;
  252. if (st)
  253. memset(st, 0, sizeof(*st));
  254. count = 0;
  255. err = usb_interrupt_msg(dev->udev,
  256. usb_rcvintpipe(dev->udev,
  257. dev->ep[EP_STATUS]),
  258. dev->st_buf, sizeof(dev->st_buf),
  259. &count, 1000);
  260. if (err < 0) {
  261. pr_err("Failed to read 1-wire data from 0x%x: err=%d.\n",
  262. dev->ep[EP_STATUS], err);
  263. return err;
  264. }
  265. if (dump)
  266. ds_dump_status(dev, dev->st_buf, count);
  267. if (st && count >= sizeof(*st))
  268. memcpy(st, dev->st_buf, sizeof(*st));
  269. return count;
  270. }
  271. static void ds_reset_device(struct ds_device *dev)
  272. {
  273. ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  274. /* Always allow strong pullup which allow individual writes to use
  275. * the strong pullup.
  276. */
  277. if (ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE))
  278. pr_err("ds_reset_device: Error allowing strong pullup\n");
  279. /* Chip strong pullup time was cleared. */
  280. if (dev->spu_sleep) {
  281. /* lower 4 bits are 0, see ds_set_pullup */
  282. u8 del = dev->spu_sleep>>4;
  283. if (ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del))
  284. pr_err("ds_reset_device: Error setting duration\n");
  285. }
  286. }
  287. static int ds_recv_data(struct ds_device *dev, unsigned char *buf, int size)
  288. {
  289. int count, err;
  290. /* Careful on size. If size is less than what is available in
  291. * the input buffer, the device fails the bulk transfer and
  292. * clears the input buffer. It could read the maximum size of
  293. * the data buffer, but then do you return the first, last, or
  294. * some set of the middle size bytes? As long as the rest of
  295. * the code is correct there will be size bytes waiting. A
  296. * call to ds_wait_status will wait until the device is idle
  297. * and any data to be received would have been available.
  298. */
  299. count = 0;
  300. err = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]),
  301. buf, size, &count, 1000);
  302. if (err < 0) {
  303. pr_info("Clearing ep0x%x.\n", dev->ep[EP_DATA_IN]);
  304. usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->ep[EP_DATA_IN]));
  305. ds_recv_status(dev, NULL, true);
  306. return err;
  307. }
  308. #if 0
  309. {
  310. int i;
  311. printk("%s: count=%d: ", __func__, count);
  312. for (i = 0; i < count; ++i)
  313. printk("%02x ", buf[i]);
  314. printk("\n");
  315. }
  316. #endif
  317. return count;
  318. }
  319. static int ds_send_data(struct ds_device *dev, unsigned char *buf, int len)
  320. {
  321. int count, err;
  322. count = 0;
  323. err = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->ep[EP_DATA_OUT]), buf, len, &count, 1000);
  324. if (err < 0) {
  325. pr_err("Failed to write 1-wire data to ep0x%x: "
  326. "err=%d.\n", dev->ep[EP_DATA_OUT], err);
  327. return err;
  328. }
  329. return err;
  330. }
  331. #if 0
  332. int ds_stop_pulse(struct ds_device *dev, int limit)
  333. {
  334. struct ds_status st;
  335. int count = 0, err = 0;
  336. do {
  337. err = ds_send_control(dev, CTL_HALT_EXE_IDLE, 0);
  338. if (err)
  339. break;
  340. err = ds_send_control(dev, CTL_RESUME_EXE, 0);
  341. if (err)
  342. break;
  343. err = ds_recv_status(dev, &st, false);
  344. if (err)
  345. break;
  346. if ((st.status & ST_SPUA) == 0) {
  347. err = ds_send_control_mode(dev, MOD_PULSE_EN, 0);
  348. if (err)
  349. break;
  350. }
  351. } while (++count < limit);
  352. return err;
  353. }
  354. int ds_detect(struct ds_device *dev, struct ds_status *st)
  355. {
  356. int err;
  357. err = ds_send_control_cmd(dev, CTL_RESET_DEVICE, 0);
  358. if (err)
  359. return err;
  360. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, 0);
  361. if (err)
  362. return err;
  363. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM | COMM_TYPE, 0x40);
  364. if (err)
  365. return err;
  366. err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_PROG);
  367. if (err)
  368. return err;
  369. err = ds_dump_status(dev, st);
  370. return err;
  371. }
  372. #endif /* 0 */
  373. static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
  374. {
  375. int err, count = 0;
  376. do {
  377. st->status = 0;
  378. err = ds_recv_status(dev, st, false);
  379. #if 0
  380. if (err >= 0) {
  381. int i;
  382. printk("0x%x: count=%d, status: ", dev->ep[EP_STATUS], err);
  383. for (i = 0; i < err; ++i)
  384. printk("%02x ", dev->st_buf[i]);
  385. printk("\n");
  386. }
  387. #endif
  388. } while (!(st->status & ST_IDLE) && !(err < 0) && ++count < 100);
  389. if (err >= 16 && st->status & ST_EPOF) {
  390. pr_info("Resetting device after ST_EPOF.\n");
  391. ds_reset_device(dev);
  392. /* Always dump the device status. */
  393. count = 101;
  394. }
  395. /* Dump the status for errors or if there is extended return data.
  396. * The extended status includes new device detection (maybe someone
  397. * can do something with it).
  398. */
  399. if (err > 16 || count >= 100 || err < 0)
  400. ds_dump_status(dev, dev->st_buf, err);
  401. /* Extended data isn't an error. Well, a short is, but the dump
  402. * would have already told the user that and we can't do anything
  403. * about it in software anyway.
  404. */
  405. if (count >= 100 || err < 0)
  406. return -1;
  407. else
  408. return 0;
  409. }
  410. static int ds_reset(struct ds_device *dev)
  411. {
  412. int err;
  413. /* Other potentionally interesting flags for reset.
  414. *
  415. * COMM_NTF: Return result register feedback. This could be used to
  416. * detect some conditions such as short, alarming presence, or
  417. * detect if a new device was detected.
  418. *
  419. * COMM_SE which allows SPEED_NORMAL, SPEED_FLEXIBLE, SPEED_OVERDRIVE:
  420. * Select the data transfer rate.
  421. */
  422. err = ds_send_control(dev, COMM_1_WIRE_RESET | COMM_IM, SPEED_NORMAL);
  423. if (err)
  424. return err;
  425. return 0;
  426. }
  427. #if 0
  428. static int ds_set_speed(struct ds_device *dev, int speed)
  429. {
  430. int err;
  431. if (speed != SPEED_NORMAL && speed != SPEED_FLEXIBLE && speed != SPEED_OVERDRIVE)
  432. return -EINVAL;
  433. if (speed != SPEED_OVERDRIVE)
  434. speed = SPEED_FLEXIBLE;
  435. speed &= 0xff;
  436. err = ds_send_control_mode(dev, MOD_1WIRE_SPEED, speed);
  437. if (err)
  438. return err;
  439. return err;
  440. }
  441. #endif /* 0 */
  442. static int ds_set_pullup(struct ds_device *dev, int delay)
  443. {
  444. int err = 0;
  445. u8 del = 1 + (u8)(delay >> 4);
  446. /* Just storing delay would not get the trunication and roundup. */
  447. int ms = del<<4;
  448. /* Enable spu_bit if a delay is set. */
  449. dev->spu_bit = delay ? COMM_SPU : 0;
  450. /* If delay is zero, it has already been disabled, if the time is
  451. * the same as the hardware was last programmed to, there is also
  452. * nothing more to do. Compare with the recalculated value ms
  453. * rather than del or delay which can have a different value.
  454. */
  455. if (delay == 0 || ms == dev->spu_sleep)
  456. return err;
  457. err = ds_send_control(dev, COMM_SET_DURATION | COMM_IM, del);
  458. if (err)
  459. return err;
  460. dev->spu_sleep = ms;
  461. return err;
  462. }
  463. static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
  464. {
  465. int err;
  466. struct ds_status st;
  467. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | (bit ? COMM_D : 0),
  468. 0);
  469. if (err)
  470. return err;
  471. ds_wait_status(dev, &st);
  472. err = ds_recv_data(dev, tbit, sizeof(*tbit));
  473. if (err < 0)
  474. return err;
  475. return 0;
  476. }
  477. #if 0
  478. static int ds_write_bit(struct ds_device *dev, u8 bit)
  479. {
  480. int err;
  481. struct ds_status st;
  482. /* Set COMM_ICP to write without a readback. Note, this will
  483. * produce one time slot, a down followed by an up with COMM_D
  484. * only determing the timing.
  485. */
  486. err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_ICP |
  487. (bit ? COMM_D : 0), 0);
  488. if (err)
  489. return err;
  490. ds_wait_status(dev, &st);
  491. return 0;
  492. }
  493. #endif
  494. static int ds_write_byte(struct ds_device *dev, u8 byte)
  495. {
  496. int err;
  497. struct ds_status st;
  498. err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM | dev->spu_bit, byte);
  499. if (err)
  500. return err;
  501. if (dev->spu_bit)
  502. msleep(dev->spu_sleep);
  503. err = ds_wait_status(dev, &st);
  504. if (err)
  505. return err;
  506. err = ds_recv_data(dev, &dev->byte_buf, 1);
  507. if (err < 0)
  508. return err;
  509. return !(byte == dev->byte_buf);
  510. }
  511. static int ds_read_byte(struct ds_device *dev, u8 *byte)
  512. {
  513. int err;
  514. struct ds_status st;
  515. err = ds_send_control(dev, COMM_BYTE_IO | COMM_IM, 0xff);
  516. if (err)
  517. return err;
  518. ds_wait_status(dev, &st);
  519. err = ds_recv_data(dev, byte, sizeof(*byte));
  520. if (err < 0)
  521. return err;
  522. return 0;
  523. }
  524. static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
  525. {
  526. struct ds_status st;
  527. int err;
  528. if (len > 64*1024)
  529. return -E2BIG;
  530. memset(buf, 0xFF, len);
  531. err = ds_send_data(dev, buf, len);
  532. if (err < 0)
  533. return err;
  534. err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM, len);
  535. if (err)
  536. return err;
  537. ds_wait_status(dev, &st);
  538. memset(buf, 0x00, len);
  539. err = ds_recv_data(dev, buf, len);
  540. return err;
  541. }
  542. static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
  543. {
  544. int err;
  545. struct ds_status st;
  546. err = ds_send_data(dev, buf, len);
  547. if (err < 0)
  548. return err;
  549. err = ds_send_control(dev, COMM_BLOCK_IO | COMM_IM | dev->spu_bit, len);
  550. if (err)
  551. return err;
  552. if (dev->spu_bit)
  553. msleep(dev->spu_sleep);
  554. ds_wait_status(dev, &st);
  555. err = ds_recv_data(dev, buf, len);
  556. if (err < 0)
  557. return err;
  558. return !(err == len);
  559. }
  560. static void ds9490r_search(void *data, struct w1_master *master,
  561. u8 search_type, w1_slave_found_callback callback)
  562. {
  563. /* When starting with an existing id, the first id returned will
  564. * be that device (if it is still on the bus most likely).
  565. *
  566. * If the number of devices found is less than or equal to the
  567. * search_limit, that number of IDs will be returned. If there are
  568. * more, search_limit IDs will be returned followed by a non-zero
  569. * discrepency value.
  570. */
  571. struct ds_device *dev = data;
  572. int err;
  573. u16 value, index;
  574. struct ds_status st;
  575. int search_limit;
  576. int found = 0;
  577. int i;
  578. /* DS18b20 spec, 13.16 ms per device, 75 per second, sleep for
  579. * discovering 8 devices (1 bulk transfer and 1/2 FIFO size) at a time.
  580. */
  581. const unsigned long jtime = msecs_to_jiffies(1000*8/75);
  582. /* FIFO 128 bytes, bulk packet size 64, read a multiple of the
  583. * packet size.
  584. */
  585. const size_t bufsize = 2 * 64;
  586. u64 *buf;
  587. buf = kmalloc(bufsize, GFP_KERNEL);
  588. if (!buf)
  589. return;
  590. mutex_lock(&master->bus_mutex);
  591. /* address to start searching at */
  592. if (ds_send_data(dev, (u8 *)&master->search_id, 8) < 0)
  593. goto search_out;
  594. master->search_id = 0;
  595. value = COMM_SEARCH_ACCESS | COMM_IM | COMM_RST | COMM_SM | COMM_F |
  596. COMM_RTS;
  597. search_limit = master->max_slave_count;
  598. if (search_limit > 255)
  599. search_limit = 0;
  600. index = search_type | (search_limit << 8);
  601. if (ds_send_control(dev, value, index) < 0)
  602. goto search_out;
  603. do {
  604. schedule_timeout(jtime);
  605. err = ds_recv_status(dev, &st, false);
  606. if (err < 0 || err < sizeof(st))
  607. break;
  608. if (st.data_in_buffer_status) {
  609. /* Bulk in can receive partial ids, but when it does
  610. * they fail crc and will be discarded anyway.
  611. * That has only been seen when status in buffer
  612. * is 0 and bulk is read anyway, so don't read
  613. * bulk without first checking if status says there
  614. * is data to read.
  615. */
  616. err = ds_recv_data(dev, (u8 *)buf, bufsize);
  617. if (err < 0)
  618. break;
  619. for (i = 0; i < err/8; ++i) {
  620. ++found;
  621. if (found <= search_limit)
  622. callback(master, buf[i]);
  623. /* can't know if there will be a discrepancy
  624. * value after until the next id */
  625. if (found == search_limit)
  626. master->search_id = buf[i];
  627. }
  628. }
  629. if (test_bit(W1_ABORT_SEARCH, &master->flags))
  630. break;
  631. } while (!(st.status & (ST_IDLE | ST_HALT)));
  632. /* only continue the search if some weren't found */
  633. if (found <= search_limit) {
  634. master->search_id = 0;
  635. } else if (!test_bit(W1_WARN_MAX_COUNT, &master->flags)) {
  636. /* Only max_slave_count will be scanned in a search,
  637. * but it will start where it left off next search
  638. * until all ids are identified and then it will start
  639. * over. A continued search will report the previous
  640. * last id as the first id (provided it is still on the
  641. * bus).
  642. */
  643. dev_info(&dev->udev->dev, "%s: max_slave_count %d reached, "
  644. "will continue next search.\n", __func__,
  645. master->max_slave_count);
  646. set_bit(W1_WARN_MAX_COUNT, &master->flags);
  647. }
  648. search_out:
  649. mutex_unlock(&master->bus_mutex);
  650. kfree(buf);
  651. }
  652. #if 0
  653. /*
  654. * FIXME: if this disabled code is ever used in the future all ds_send_data()
  655. * calls must be changed to use a DMAable buffer.
  656. */
  657. static int ds_match_access(struct ds_device *dev, u64 init)
  658. {
  659. int err;
  660. struct ds_status st;
  661. err = ds_send_data(dev, (unsigned char *)&init, sizeof(init));
  662. if (err)
  663. return err;
  664. ds_wait_status(dev, &st);
  665. err = ds_send_control(dev, COMM_MATCH_ACCESS | COMM_IM | COMM_RST, 0x0055);
  666. if (err)
  667. return err;
  668. ds_wait_status(dev, &st);
  669. return 0;
  670. }
  671. static int ds_set_path(struct ds_device *dev, u64 init)
  672. {
  673. int err;
  674. struct ds_status st;
  675. u8 buf[9];
  676. memcpy(buf, &init, 8);
  677. buf[8] = BRANCH_MAIN;
  678. err = ds_send_data(dev, buf, sizeof(buf));
  679. if (err)
  680. return err;
  681. ds_wait_status(dev, &st);
  682. err = ds_send_control(dev, COMM_SET_PATH | COMM_IM | COMM_RST, 0);
  683. if (err)
  684. return err;
  685. ds_wait_status(dev, &st);
  686. return 0;
  687. }
  688. #endif /* 0 */
  689. static u8 ds9490r_touch_bit(void *data, u8 bit)
  690. {
  691. struct ds_device *dev = data;
  692. if (ds_touch_bit(dev, bit, &dev->byte_buf))
  693. return 0;
  694. return dev->byte_buf;
  695. }
  696. #if 0
  697. static void ds9490r_write_bit(void *data, u8 bit)
  698. {
  699. struct ds_device *dev = data;
  700. ds_write_bit(dev, bit);
  701. }
  702. static u8 ds9490r_read_bit(void *data)
  703. {
  704. struct ds_device *dev = data;
  705. int err;
  706. err = ds_touch_bit(dev, 1, &dev->byte_buf);
  707. if (err)
  708. return 0;
  709. return dev->byte_buf & 1;
  710. }
  711. #endif
  712. static void ds9490r_write_byte(void *data, u8 byte)
  713. {
  714. struct ds_device *dev = data;
  715. ds_write_byte(dev, byte);
  716. }
  717. static u8 ds9490r_read_byte(void *data)
  718. {
  719. struct ds_device *dev = data;
  720. int err;
  721. err = ds_read_byte(dev, &dev->byte_buf);
  722. if (err)
  723. return 0;
  724. return dev->byte_buf;
  725. }
  726. static void ds9490r_write_block(void *data, const u8 *buf, int len)
  727. {
  728. struct ds_device *dev = data;
  729. u8 *tbuf;
  730. if (len <= 0)
  731. return;
  732. tbuf = kmemdup(buf, len, GFP_KERNEL);
  733. if (!tbuf)
  734. return;
  735. ds_write_block(dev, tbuf, len);
  736. kfree(tbuf);
  737. }
  738. static u8 ds9490r_read_block(void *data, u8 *buf, int len)
  739. {
  740. struct ds_device *dev = data;
  741. int err;
  742. u8 *tbuf;
  743. if (len <= 0)
  744. return 0;
  745. tbuf = kmalloc(len, GFP_KERNEL);
  746. if (!tbuf)
  747. return 0;
  748. err = ds_read_block(dev, tbuf, len);
  749. if (err >= 0)
  750. memcpy(buf, tbuf, len);
  751. kfree(tbuf);
  752. return err >= 0 ? len : 0;
  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. pr_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->cur_altsetting->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->cur_altsetting->desc.bInterfaceNumber, err);
  842. goto err_out_clear;
  843. }
  844. iface_desc = intf->cur_altsetting;
  845. if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
  846. pr_info("Num endpoints=%d. It is not DS9490R.\n",
  847. iface_desc->desc.bNumEndpoints);
  848. err = -EINVAL;
  849. goto err_out_clear;
  850. }
  851. /*
  852. * This loop doesn'd show control 0 endpoint,
  853. * so we will fill only 1-3 endpoints entry.
  854. */
  855. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  856. endpoint = &iface_desc->endpoint[i].desc;
  857. dev->ep[i+1] = endpoint->bEndpointAddress;
  858. #if 0
  859. printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
  860. i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
  861. (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
  862. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  863. #endif
  864. }
  865. err = ds_w1_init(dev);
  866. if (err)
  867. goto err_out_clear;
  868. mutex_lock(&ds_mutex);
  869. list_add_tail(&dev->ds_entry, &ds_devices);
  870. mutex_unlock(&ds_mutex);
  871. return 0;
  872. err_out_clear:
  873. usb_set_intfdata(intf, NULL);
  874. usb_put_dev(dev->udev);
  875. err_out_free:
  876. kfree(dev);
  877. return err;
  878. }
  879. static void ds_disconnect(struct usb_interface *intf)
  880. {
  881. struct ds_device *dev;
  882. dev = usb_get_intfdata(intf);
  883. if (!dev)
  884. return;
  885. mutex_lock(&ds_mutex);
  886. list_del(&dev->ds_entry);
  887. mutex_unlock(&ds_mutex);
  888. ds_w1_fini(dev);
  889. usb_set_intfdata(intf, NULL);
  890. usb_put_dev(dev->udev);
  891. kfree(dev);
  892. }
  893. static const struct usb_device_id ds_id_table[] = {
  894. { USB_DEVICE(0x04fa, 0x2490) },
  895. { },
  896. };
  897. MODULE_DEVICE_TABLE(usb, ds_id_table);
  898. static struct usb_driver ds_driver = {
  899. .name = "DS9490R",
  900. .probe = ds_probe,
  901. .disconnect = ds_disconnect,
  902. .id_table = ds_id_table,
  903. };
  904. module_usb_driver(ds_driver);
  905. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  906. MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
  907. MODULE_LICENSE("GPL");