ds2490.c 24 KB

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