ds2490.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 = kmemdup(buf, len, GFP_KERNEL);
  751. if (!tbuf)
  752. return;
  753. ds_write_block(dev, tbuf, len);
  754. kfree(tbuf);
  755. }
  756. static u8 ds9490r_read_block(void *data, u8 *buf, int len)
  757. {
  758. struct ds_device *dev = data;
  759. int err;
  760. u8 *tbuf;
  761. if (len <= 0)
  762. return 0;
  763. tbuf = kmalloc(len, GFP_KERNEL);
  764. if (!tbuf)
  765. return 0;
  766. err = ds_read_block(dev, tbuf, len);
  767. if (err >= 0)
  768. memcpy(buf, tbuf, len);
  769. kfree(tbuf);
  770. return err >= 0 ? len : 0;
  771. }
  772. static u8 ds9490r_reset(void *data)
  773. {
  774. struct ds_device *dev = data;
  775. int err;
  776. err = ds_reset(dev);
  777. if (err)
  778. return 1;
  779. return 0;
  780. }
  781. static u8 ds9490r_set_pullup(void *data, int delay)
  782. {
  783. struct ds_device *dev = data;
  784. if (ds_set_pullup(dev, delay))
  785. return 1;
  786. return 0;
  787. }
  788. static int ds_w1_init(struct ds_device *dev)
  789. {
  790. memset(&dev->master, 0, sizeof(struct w1_bus_master));
  791. /* Reset the device as it can be in a bad state.
  792. * This is necessary because a block write will wait for data
  793. * to be placed in the output buffer and block any later
  794. * commands which will keep accumulating and the device will
  795. * not be idle. Another case is removing the ds2490 module
  796. * while a bus search is in progress, somehow a few commands
  797. * get through, but the input transfers fail leaving data in
  798. * the input buffer. This will cause the next read to fail
  799. * see the note in ds_recv_data.
  800. */
  801. ds_reset_device(dev);
  802. dev->master.data = dev;
  803. dev->master.touch_bit = &ds9490r_touch_bit;
  804. /* read_bit and write_bit in w1_bus_master are expected to set and
  805. * sample the line level. For write_bit that means it is expected to
  806. * set it to that value and leave it there. ds2490 only supports an
  807. * individual time slot at the lowest level. The requirement from
  808. * pulling the bus state down to reading the state is 15us, something
  809. * that isn't realistic on the USB bus anyway.
  810. dev->master.read_bit = &ds9490r_read_bit;
  811. dev->master.write_bit = &ds9490r_write_bit;
  812. */
  813. dev->master.read_byte = &ds9490r_read_byte;
  814. dev->master.write_byte = &ds9490r_write_byte;
  815. dev->master.read_block = &ds9490r_read_block;
  816. dev->master.write_block = &ds9490r_write_block;
  817. dev->master.reset_bus = &ds9490r_reset;
  818. dev->master.set_pullup = &ds9490r_set_pullup;
  819. dev->master.search = &ds9490r_search;
  820. return w1_add_master_device(&dev->master);
  821. }
  822. static void ds_w1_fini(struct ds_device *dev)
  823. {
  824. w1_remove_master_device(&dev->master);
  825. }
  826. static int ds_probe(struct usb_interface *intf,
  827. const struct usb_device_id *udev_id)
  828. {
  829. struct usb_device *udev = interface_to_usbdev(intf);
  830. struct usb_endpoint_descriptor *endpoint;
  831. struct usb_host_interface *iface_desc;
  832. struct ds_device *dev;
  833. int i, err, alt;
  834. dev = kzalloc(sizeof(struct ds_device), GFP_KERNEL);
  835. if (!dev) {
  836. pr_info("Failed to allocate new DS9490R structure.\n");
  837. return -ENOMEM;
  838. }
  839. dev->udev = usb_get_dev(udev);
  840. if (!dev->udev) {
  841. err = -ENOMEM;
  842. goto err_out_free;
  843. }
  844. memset(dev->ep, 0, sizeof(dev->ep));
  845. usb_set_intfdata(intf, dev);
  846. err = usb_reset_configuration(dev->udev);
  847. if (err) {
  848. dev_err(&dev->udev->dev,
  849. "Failed to reset configuration: err=%d.\n", err);
  850. goto err_out_clear;
  851. }
  852. /* alternative 3, 1ms interrupt (greatly speeds search), 64 byte bulk */
  853. alt = 3;
  854. err = usb_set_interface(dev->udev,
  855. intf->altsetting[alt].desc.bInterfaceNumber, alt);
  856. if (err) {
  857. dev_err(&dev->udev->dev, "Failed to set alternative setting %d "
  858. "for %d interface: err=%d.\n", alt,
  859. intf->altsetting[alt].desc.bInterfaceNumber, err);
  860. goto err_out_clear;
  861. }
  862. iface_desc = &intf->altsetting[alt];
  863. if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
  864. pr_info("Num endpoints=%d. It is not DS9490R.\n",
  865. iface_desc->desc.bNumEndpoints);
  866. err = -EINVAL;
  867. goto err_out_clear;
  868. }
  869. /*
  870. * This loop doesn'd show control 0 endpoint,
  871. * so we will fill only 1-3 endpoints entry.
  872. */
  873. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  874. endpoint = &iface_desc->endpoint[i].desc;
  875. dev->ep[i+1] = endpoint->bEndpointAddress;
  876. #if 0
  877. printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
  878. i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
  879. (endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
  880. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  881. #endif
  882. }
  883. err = ds_w1_init(dev);
  884. if (err)
  885. goto err_out_clear;
  886. mutex_lock(&ds_mutex);
  887. list_add_tail(&dev->ds_entry, &ds_devices);
  888. mutex_unlock(&ds_mutex);
  889. return 0;
  890. err_out_clear:
  891. usb_set_intfdata(intf, NULL);
  892. usb_put_dev(dev->udev);
  893. err_out_free:
  894. kfree(dev);
  895. return err;
  896. }
  897. static void ds_disconnect(struct usb_interface *intf)
  898. {
  899. struct ds_device *dev;
  900. dev = usb_get_intfdata(intf);
  901. if (!dev)
  902. return;
  903. mutex_lock(&ds_mutex);
  904. list_del(&dev->ds_entry);
  905. mutex_unlock(&ds_mutex);
  906. ds_w1_fini(dev);
  907. usb_set_intfdata(intf, NULL);
  908. usb_put_dev(dev->udev);
  909. kfree(dev);
  910. }
  911. module_usb_driver(ds_driver);
  912. MODULE_LICENSE("GPL");
  913. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  914. MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");