ds2490.c 24 KB

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