ftdi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /* Future Technology Devices International serial ports */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <thread.h>
  5. #include "usb.h"
  6. #include "usbfs.h"
  7. #include "serial.h"
  8. #include "ftdi.h"
  9. /*
  10. * BUG: This keeps growing, there has to be a better way, but without
  11. * devices to try it... We can probably simply look for FTDI in the
  12. * string, or use regular expressions somehow.
  13. */
  14. Cinfo ftinfo[] = {
  15. { FTVid, FTACTZWAVEDid },
  16. { FTSheevaVid, FTSheevaDid },
  17. { FTVid, FTIRTRANSDid },
  18. { FTVid, FTIPLUSDid },
  19. { FTVid, FTSIODid },
  20. { FTVid, FT8U232AMDid },
  21. { FTVid, FT8U232AMALTDid },
  22. { FTVid, FT8U2232CDid },
  23. { FTVid, FTRELAISDid },
  24. { INTERBIOMVid, INTERBIOMIOBRDDid },
  25. { INTERBIOMVid, INTERBIOMMINIIOBRDDid },
  26. { FTVid, FTXF632Did },
  27. { FTVid, FTXF634Did },
  28. { FTVid, FTXF547Did },
  29. { FTVid, FTXF633Did },
  30. { FTVid, FTXF631Did },
  31. { FTVid, FTXF635Did },
  32. { FTVid, FTXF640Did },
  33. { FTVid, FTXF642Did },
  34. { FTVid, FTDSS20Did },
  35. { FTNFRICVid, FTNFRICDid },
  36. { FTVid, FTVNHCPCUSBDDid },
  37. { FTVid, FTMTXORB0Did },
  38. { FTVid, FTMTXORB1Did },
  39. { FTVid, FTMTXORB2Did },
  40. { FTVid, FTMTXORB3Did },
  41. { FTVid, FTMTXORB4Did },
  42. { FTVid, FTMTXORB5Did },
  43. { FTVid, FTMTXORB6Did },
  44. { FTVid, FTPERLEULTRAPORTDid },
  45. { FTVid, FTPIEGROUPDid },
  46. { SEALEVELVid, SEALEVEL2101Did },
  47. { SEALEVELVid, SEALEVEL2102Did },
  48. { SEALEVELVid, SEALEVEL2103Did },
  49. { SEALEVELVid, SEALEVEL2104Did },
  50. { SEALEVELVid, SEALEVEL22011Did },
  51. { SEALEVELVid, SEALEVEL22012Did },
  52. { SEALEVELVid, SEALEVEL22021Did },
  53. { SEALEVELVid, SEALEVEL22022Did },
  54. { SEALEVELVid, SEALEVEL22031Did },
  55. { SEALEVELVid, SEALEVEL22032Did },
  56. { SEALEVELVid, SEALEVEL24011Did },
  57. { SEALEVELVid, SEALEVEL24012Did },
  58. { SEALEVELVid, SEALEVEL24013Did },
  59. { SEALEVELVid, SEALEVEL24014Did },
  60. { SEALEVELVid, SEALEVEL24021Did },
  61. { SEALEVELVid, SEALEVEL24022Did },
  62. { SEALEVELVid, SEALEVEL24023Did },
  63. { SEALEVELVid, SEALEVEL24024Did },
  64. { SEALEVELVid, SEALEVEL24031Did },
  65. { SEALEVELVid, SEALEVEL24032Did },
  66. { SEALEVELVid, SEALEVEL24033Did },
  67. { SEALEVELVid, SEALEVEL24034Did },
  68. { SEALEVELVid, SEALEVEL28011Did },
  69. { SEALEVELVid, SEALEVEL28012Did },
  70. { SEALEVELVid, SEALEVEL28013Did },
  71. { SEALEVELVid, SEALEVEL28014Did },
  72. { SEALEVELVid, SEALEVEL28015Did },
  73. { SEALEVELVid, SEALEVEL28016Did },
  74. { SEALEVELVid, SEALEVEL28017Did },
  75. { SEALEVELVid, SEALEVEL28018Did },
  76. { SEALEVELVid, SEALEVEL28021Did },
  77. { SEALEVELVid, SEALEVEL28022Did },
  78. { SEALEVELVid, SEALEVEL28023Did },
  79. { SEALEVELVid, SEALEVEL28024Did },
  80. { SEALEVELVid, SEALEVEL28025Did },
  81. { SEALEVELVid, SEALEVEL28026Did },
  82. { SEALEVELVid, SEALEVEL28027Did },
  83. { SEALEVELVid, SEALEVEL28028Did },
  84. { SEALEVELVid, SEALEVEL28031Did },
  85. { SEALEVELVid, SEALEVEL28032Did },
  86. { SEALEVELVid, SEALEVEL28033Did },
  87. { SEALEVELVid, SEALEVEL28034Did },
  88. { SEALEVELVid, SEALEVEL28035Did },
  89. { SEALEVELVid, SEALEVEL28036Did },
  90. { SEALEVELVid, SEALEVEL28037Did },
  91. { SEALEVELVid, SEALEVEL28038Did },
  92. { IDTECHVid, IDTECHIDT1221UDid },
  93. { OCTVid, OCTUS101Did },
  94. { FTVid, FTHETIRA1Did }, /* special quirk div = 240 baud = B38400 rtscts = 1 */
  95. { FTVid, FTUSBUIRTDid }, /* special quirk div = 77, baud = B38400 */
  96. { FTVid, PROTEGOSPECIAL1 },
  97. { FTVid, PROTEGOR2X0 },
  98. { FTVid, PROTEGOSPECIAL3 },
  99. { FTVid, PROTEGOSPECIAL4 },
  100. { FTVid, FTGUDEADSE808Did },
  101. { FTVid, FTGUDEADSE809Did },
  102. { FTVid, FTGUDEADSE80ADid },
  103. { FTVid, FTGUDEADSE80BDid },
  104. { FTVid, FTGUDEADSE80CDid },
  105. { FTVid, FTGUDEADSE80DDid },
  106. { FTVid, FTGUDEADSE80EDid },
  107. { FTVid, FTGUDEADSE80FDid },
  108. { FTVid, FTGUDEADSE888Did },
  109. { FTVid, FTGUDEADSE889Did },
  110. { FTVid, FTGUDEADSE88ADid },
  111. { FTVid, FTGUDEADSE88BDid },
  112. { FTVid, FTGUDEADSE88CDid },
  113. { FTVid, FTGUDEADSE88DDid },
  114. { FTVid, FTGUDEADSE88EDid },
  115. { FTVid, FTGUDEADSE88FDid },
  116. { FTVid, FTELVUO100Did },
  117. { FTVid, FTELVUM100Did },
  118. { FTVid, FTELVUR100Did },
  119. { FTVid, FTELVALC8500Did },
  120. { FTVid, FTPYRAMIDDid },
  121. { FTVid, FTELVFHZ1000PCDid },
  122. { FTVid, FTELVCLI7000Did },
  123. { FTVid, FTELVPPS7330Did },
  124. { FTVid, FTELVTFM100Did },
  125. { FTVid, FTELVUDF77Did },
  126. { FTVid, FTELVUIO88Did },
  127. { FTVid, FTELVUAD8Did },
  128. { FTVid, FTELVUDA7Did },
  129. { FTVid, FTELVUSI2Did },
  130. { FTVid, FTELVT1100Did },
  131. { FTVid, FTELVPCD200Did },
  132. { FTVid, FTELVULA200Did },
  133. { FTVid, FTELVCSI8Did },
  134. { FTVid, FTELVEM1000DLDid },
  135. { FTVid, FTELVPCK100Did },
  136. { FTVid, FTELVRFP500Did },
  137. { FTVid, FTELVFS20SIGDid },
  138. { FTVid, FTELVWS300PCDid },
  139. { FTVid, FTELVFHZ1300PCDid },
  140. { FTVid, FTELVWS500Did },
  141. { FTVid, LINXSDMUSBQSSDid },
  142. { FTVid, LINXMASTERDEVEL2Did },
  143. { FTVid, LINXFUTURE0Did },
  144. { FTVid, LINXFUTURE1Did },
  145. { FTVid, LINXFUTURE2Did },
  146. { FTVid, FTCCSICDU200Did },
  147. { FTVid, FTCCSICDU401Did },
  148. { FTVid, INSIDEACCESSO },
  149. { INTREDidVid, INTREDidVALUECANDid },
  150. { INTREDidVid, INTREDidNEOVIDid },
  151. { FALCOMVid, FALCOMTWISTDid },
  152. { FALCOMVid, FALCOMSAMBADid },
  153. { FTVid, FTSUUNTOSPORTSDid },
  154. { FTVid, FTRMCANVIEWDid },
  155. { BANDBVid, BANDBUSOTL4Did },
  156. { BANDBVid, BANDBUSTL4Did },
  157. { BANDBVid, BANDBUSO9ML2Did },
  158. { FTVid, EVERECOPROCDSDid },
  159. { FTVid, FT4NGALAXYDE0Did },
  160. { FTVid, FT4NGALAXYDE1Did },
  161. { FTVid, FT4NGALAXYDE2Did },
  162. { FTVid, XSENSCONVERTER0Did },
  163. { FTVid, XSENSCONVERTER1Did },
  164. { FTVid, XSENSCONVERTER2Did },
  165. { FTVid, XSENSCONVERTER3Did },
  166. { FTVid, XSENSCONVERTER4Did },
  167. { FTVid, XSENSCONVERTER5Did },
  168. { FTVid, XSENSCONVERTER6Did },
  169. { FTVid, XSENSCONVERTER7Did },
  170. { MOBILITYVid, MOBILITYUSBSERIALDid },
  171. { FTVid, FTACTIVEROBOTSDid },
  172. { FTVid, FTMHAMKWDid },
  173. { FTVid, FTMHAMYSDid },
  174. { FTVid, FTMHAMY6Did },
  175. { FTVid, FTMHAMY8Did },
  176. { FTVid, FTMHAMICDid },
  177. { FTVid, FTMHAMDB9Did },
  178. { FTVid, FTMHAMRS232Did },
  179. { FTVid, FTMHAMY9Did },
  180. { FTVid, FTTERATRONIKVCPDid },
  181. { FTVid, FTTERATRONIKD2XXDid },
  182. { EVOLUTIONVid, EVOLUTIONER1Did },
  183. { FTVid, FTARTEMISDid },
  184. { FTVid, FTATIKATK16Did },
  185. { FTVid, FTATIKATK16CDid },
  186. { FTVid, FTATIKATK16HRDid },
  187. { FTVid, FTATIKATK16HRCDid },
  188. { KOBILVid, KOBILCONVB1Did },
  189. { KOBILVid, KOBILCONVKAANDid },
  190. { POSIFLEXVid, POSIFLEXPP7000Did },
  191. { FTVid, FTTTUSBDid },
  192. { FTVid, FTECLOCOM1WIREDid },
  193. { FTVid, FTWESTREXMODEL777Did },
  194. { FTVid, FTWESTREXMODEL8900FDid },
  195. { FTVid, FTPCDJDAC2Did },
  196. { FTVid, FTRRCIRKITSLOCOBUFFERDid },
  197. { FTVid, FTASKRDR400Did },
  198. { ICOMID1Vid, ICOMID1Did },
  199. { PAPOUCHVid, PAPOUCHTMUDid },
  200. { FTVid, FTACGHFDUALDid },
  201. { 0, 0 },
  202. };
  203. enum {
  204. Packsz = 1024,
  205. };
  206. static int
  207. ftdiread(Serial *ser, int val, int index, int req, uchar *buf)
  208. {
  209. int res;
  210. if(req != FTGETE2READ)
  211. index |= ser->interfc + 1;
  212. dsprint(2, "serial: ftdiread req: %#x val: %#x idx:%d buf:%p\n",
  213. req, val, index, buf);
  214. res = usbcmd(ser->dev, Rd2h | Rftdireq | Rdev, req, val, index, buf, 1);
  215. dsprint(2, "serial: ftdiread res:%d\n", res);
  216. return res;
  217. }
  218. static int
  219. ftdiwrite(Serial *ser, int val, int index, int req)
  220. {
  221. int res;
  222. index |= ser->interfc + 1;
  223. dsprint(2, "serial: ftdiwrite req: %#x val: %#x idx:%d\n",
  224. req, val, index);
  225. res = usbcmd(ser->dev, Rh2d | Rftdireq | Rdev, req, val, index, nil, 0);
  226. dsprint(2, "serial: ftdiwrite res:%d\n", res);
  227. return res;
  228. }
  229. static int
  230. ftmodemctl(Serial *ser, int set)
  231. {
  232. if(set == 0){
  233. ser->mctl = 0;
  234. ftdiwrite(ser, 0, 0, FTSETMODEMCTRL);
  235. return 0;
  236. }
  237. ser->mctl = 1;
  238. ftdiwrite(ser, 0, FTRTSCTSHS, FTSETFLOWCTRL);
  239. return 0;
  240. }
  241. static ushort
  242. ft232ambaudbase2div(int baud, int base)
  243. {
  244. int divisor3;
  245. ushort divisor;
  246. divisor3 = (base / 2) / baud;
  247. if((divisor3 & 7) == 7)
  248. divisor3++; /* round x.7/8 up to x+1 */
  249. divisor = divisor3 >> 3;
  250. divisor3 &= 7;
  251. if(divisor3 == 1)
  252. divisor |= 0xc000; /* 0.125 */
  253. else if(divisor3 >= 4)
  254. divisor |= 0x4000; /* 0.5 */
  255. else if(divisor3 != 0)
  256. divisor |= 0x8000; /* 0.25 */
  257. if( divisor == 1)
  258. divisor = 0; /* special case for maximum baud rate */
  259. return divisor;
  260. }
  261. enum{
  262. ClockNew = 48000000,
  263. ClockOld = 12000000 / 16,
  264. HetiraDiv = 240,
  265. UirtDiv = 77,
  266. };
  267. static ushort
  268. ft232ambaud2div(int baud)
  269. {
  270. return ft232ambaudbase2div(baud, ClockNew);
  271. }
  272. static ulong divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7};
  273. static ulong
  274. ft232bmbaudbase2div(int baud, int base)
  275. {
  276. int divisor3;
  277. u32int divisor;
  278. divisor3 = (base / 2) / baud;
  279. divisor = divisor3 >> 3 | divfrac[divisor3 & 7] << 14;
  280. /* Deal with special cases for highest baud rates. */
  281. if( divisor == 1)
  282. divisor = 0; /* 1.0 */
  283. else if( divisor == 0x4001)
  284. divisor = 1; /* 1.5 */
  285. return divisor;
  286. }
  287. static ulong
  288. ft232bmbaud2div (int baud)
  289. {
  290. return ft232bmbaudbase2div (baud, ClockNew);
  291. }
  292. static int
  293. customdiv(Serial *ser)
  294. {
  295. if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTHETIRA1Did)
  296. return HetiraDiv;
  297. else if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTUSBUIRTDid)
  298. return UirtDiv;
  299. fprint(2, "serial: weird custom divisor\n");
  300. return 0; /* shouldn't happen, break as much as I can */
  301. }
  302. static ulong
  303. ftbaudcalcdiv(Serial *ser, int baud)
  304. {
  305. int cusdiv;
  306. ulong divval;
  307. if(baud == 38400 && (cusdiv = customdiv(ser)) != 0)
  308. baud = ser->baudbase / cusdiv;
  309. if(baud == 0)
  310. baud = 9600;
  311. switch(ser->type) {
  312. case SIO:
  313. switch(baud) {
  314. case 300:
  315. divval = FTb300;
  316. break;
  317. case 600:
  318. divval = FTb600;
  319. break;
  320. case 1200:
  321. divval = FTb1200;
  322. break;
  323. case 2400:
  324. divval = FTb2400;
  325. break;
  326. case 4800:
  327. divval = FTb4800;
  328. break;
  329. case 9600:
  330. divval = FTb9600;
  331. break;
  332. case 19200:
  333. divval = FTb19200;
  334. break;
  335. case 38400:
  336. divval = FTb38400;
  337. break;
  338. case 57600:
  339. divval = FTb57600;
  340. break;
  341. case 115200:
  342. divval = FTb115200;
  343. break;
  344. default:
  345. divval = FTb9600;
  346. break;
  347. }
  348. break;
  349. case FT8U232AM:
  350. if(baud <= 3000000)
  351. divval = ft232ambaud2div(baud);
  352. else
  353. divval = ft232ambaud2div(9600);
  354. break;
  355. case FT232BM:
  356. case FT2232C:
  357. if(baud <= 3000000)
  358. divval = ft232bmbaud2div(baud);
  359. else
  360. divval = ft232bmbaud2div(9600);
  361. break;
  362. default:
  363. divval = ft232bmbaud2div(9600);
  364. break;
  365. }
  366. return divval;
  367. }
  368. static int
  369. ftsetparam(Serial *ser)
  370. {
  371. int res;
  372. ushort val;
  373. ulong bauddiv;
  374. val = 0;
  375. if(ser->stop == 1)
  376. val |= FTSETDATASTOPBITS1;
  377. else if(ser->stop == 2)
  378. val |= FTSETDATASTOPBITS2;
  379. else if(ser->stop == 15)
  380. val |= FTSETDATASTOPBITS15;
  381. switch(ser->parity){
  382. case 0:
  383. val |= FTSETDATAParNONE;
  384. break;
  385. case 1:
  386. val |= FTSETDATAParODD;
  387. break;
  388. case 2:
  389. val |= FTSETDATAParEVEN;
  390. break;
  391. case 3:
  392. val |= FTSETDATAParMARK;
  393. break;
  394. case 4:
  395. val |= FTSETDATAParSPACE;
  396. break;
  397. };
  398. dsprint(2, "serial: setparam\n");
  399. res = ftdiwrite(ser, val, 0, FTSETDATA);
  400. if(res < 0)
  401. return res;
  402. res = ftmodemctl(ser, ser->mctl);
  403. if(res < 0)
  404. return res;
  405. bauddiv = ftbaudcalcdiv(ser, ser->baud);
  406. res = ftdiwrite(ser, bauddiv, 0x1&(bauddiv>>16), FTSETBaudRate);
  407. dsprint(2, "serial: setparam res: %d\n", res);
  408. return res;
  409. }
  410. /* ser locked */
  411. static void
  412. ftgettype(Serial *ser)
  413. {
  414. int inter, nifcs, i, outhdrsz, dno;
  415. ulong baudbase;
  416. Conf *cnf;
  417. inter = 0;
  418. /* Assume it is not the original SIO device for now. */
  419. baudbase = ClockNew / 2;
  420. outhdrsz = 0;
  421. nifcs = 0;
  422. dno = ser->dev->usb->dno;
  423. cnf = ser->dev->usb->conf[0];
  424. for(i = 0; i < Niface; i++)
  425. if(cnf->iface[i] != nil)
  426. nifcs++;
  427. if(nifcs> 1) {
  428. /* Multiple interfaces. Assume FT2232C. */
  429. ser->type = FT2232C;
  430. /*
  431. * BUG: If there is more than one interface, we use the second.
  432. * We only support 1 interface at the moment...
  433. * This works well for the sheeva/JTAG, but in other
  434. * cases we are ignoring nifcs-1 interfaces and using
  435. * the second (weird). The problem is we have to rethink
  436. * the whole scheme to make it work with various ifaces.
  437. */
  438. inter = PITA;
  439. /*
  440. * BM-type devices have a bug where dno gets set
  441. * to 0x200 when serial is 0.
  442. */
  443. if(dno < 0x500)
  444. fprint(2, "serial: warning: dno too low for "
  445. "multi-interface device\n");
  446. } else if(dno < 0x200) {
  447. /* Old device. Assume it is the original SIO. */
  448. ser->type = SIO;
  449. baudbase = ClockOld/16;
  450. outhdrsz = 1;
  451. } else if(dno < 0x400)
  452. /*
  453. * Assume its an FT8U232AM (or FT8U245AM)
  454. * (It might be a BM because of the iSerialNumber bug,
  455. * but it will still work as an AM device.)
  456. */
  457. ser->type = FT8U232AM;
  458. else /* Assume it is an FT232BM (or FT245BM) */
  459. ser->type = FT232BM;
  460. ser->baudbase = baudbase;
  461. ser->outhdrsz = outhdrsz;
  462. ser->inhdrsz = 2;
  463. ser->interfc = inter;
  464. dsprint (2, "serial: detected type: %#x\n", ser->type);
  465. }
  466. int
  467. ftmatch(Serial *ser, char *info)
  468. {
  469. Cinfo *ip;
  470. char buf[50];
  471. for(ip = ftinfo; ip->vid != 0; ip++){
  472. snprint(buf, sizeof buf, "vid %#06x did %#06x", ip->vid, ip->did);
  473. dsprint(2, "serial: %s %s\n", buf, info);
  474. if(strstr(info, buf) != nil){
  475. if(ser != nil){
  476. qlock(ser);
  477. ftgettype(ser);
  478. qunlock(ser);
  479. }
  480. return 0;
  481. }
  482. }
  483. return -1;
  484. }
  485. static int
  486. ftuseinhdr(Serial *ser, uchar *b)
  487. {
  488. if(b[0] & FTICTS)
  489. ser->cts = 1;
  490. else
  491. ser->cts = 0;
  492. if(b[0] & FTIDSR)
  493. ser->dsr = 1;
  494. else
  495. ser->dsr = 0;
  496. if(b[0] & FTIRI)
  497. ser->ring = 1;
  498. else
  499. ser->ring = 0;
  500. if(b[0] & FTIRLSD)
  501. ser->rlsd = 1;
  502. else
  503. ser->rlsd = 0;
  504. if(b[1] & FTIOE)
  505. ser->novererr++;
  506. if(b[1] & FTIPE)
  507. ser->nparityerr++;
  508. if(b[1] & FTIFE)
  509. ser->nframeerr++;
  510. if(b[1] & FTIBI)
  511. ser->nbreakerr++;
  512. return 0;
  513. }
  514. static int
  515. ftsetouthdr(Serial *ser, uchar *b, int len)
  516. {
  517. if(ser->outhdrsz != 0)
  518. b[0] = FTOPORT | (FTOLENMSK & len);
  519. return ser->outhdrsz;
  520. }
  521. static int
  522. wait4data(Serial *ser, uchar *data, int count)
  523. {
  524. qunlock(ser);
  525. recvul(ser->w4data);
  526. qlock(ser);
  527. if(ser->ndata >= count)
  528. ser->ndata -= count;
  529. else{
  530. count = ser->ndata;
  531. ser->ndata = 0;
  532. }
  533. assert(count >= 0);
  534. assert(ser->ndata >= 0);
  535. memmove(data, ser->data, count);
  536. if(ser->ndata != 0)
  537. memmove(ser->data, ser->data+count, ser->ndata);
  538. sendul(ser->gotdata, 1);
  539. return count;
  540. }
  541. static int
  542. wait4write(Serial *ser, uchar *data, int count)
  543. {
  544. int off, fd;
  545. uchar *b;
  546. // qunlock(ser);
  547. // recvul(ser->w4empty); should I really?
  548. // qlock(ser);
  549. b = emallocz(count+ser->outhdrsz, 1);
  550. off = ftsetouthdr(ser, b, count);
  551. memmove(b+off, data, count);
  552. fd = ser->epout->dfd;
  553. qunlock(ser);
  554. count = write(fd, b, count+off);
  555. qlock(ser);
  556. free(b);
  557. return count;
  558. }
  559. typedef struct Packser Packser;
  560. struct Packser{
  561. int nb;
  562. uchar b[Packsz];
  563. };
  564. typedef struct Areader Areader;
  565. struct Areader{
  566. Serial *s;
  567. Channel *c;
  568. };
  569. static void
  570. epreader(void *u)
  571. {
  572. int dfd, rcount;
  573. char err[40];
  574. Areader *a;
  575. Channel *c;
  576. Packser *p;
  577. Serial *ser;
  578. threadsetname("epreader proc");
  579. a = u;
  580. ser = a->s;
  581. c = a->c;
  582. free(a);
  583. qlock(ser);
  584. dfd = ser->epin->dfd;
  585. qunlock(ser);
  586. p = nil;
  587. do {
  588. if (p == nil)
  589. p = emallocz(sizeof(Packser), 1);
  590. rcount = read(dfd, p->b, sizeof p->b);
  591. if(serialdebug > 5)
  592. dsprint(2, "%d %#ux%#ux ", rcount, ser->data[0],
  593. ser->data[1]);
  594. if(rcount <= 0)
  595. break;
  596. if(rcount >= ser->inhdrsz){
  597. ftuseinhdr(ser, p->b);
  598. rcount -= ser->inhdrsz;
  599. memmove(p->b, p->b + ser->inhdrsz, rcount);
  600. if(rcount != 0){
  601. p->nb = rcount;
  602. sendp(c, p);
  603. p = nil;
  604. }
  605. }
  606. } while(rcount >= 0 || (rcount < 0 && strstr(err, "timed out") != nil));
  607. if(rcount < 0)
  608. fprint(2, "%s: error reading %s: %r\n", argv0, ser->fs.name);
  609. free(p);
  610. sendp(c, nil);
  611. closedev(ser->dev);
  612. }
  613. static void
  614. statusreader(void *u)
  615. {
  616. Areader *a;
  617. Channel *c;
  618. Packser *p;
  619. Serial *ser;
  620. ser = u;
  621. threadsetname("statusreader thread");
  622. /* big buffering, fewer bytes lost */
  623. c = chancreate(sizeof(Packser *), 128);
  624. a = emallocz(sizeof(Areader), 1);
  625. a->s = ser;
  626. a->c = c;
  627. incref(ser->dev);
  628. proccreate(epreader, a, 16*1024);
  629. while((p = recvp(c)) != nil){
  630. memmove(ser->data, p->b, p->nb);
  631. ser->ndata = p->nb;
  632. free(p);
  633. dsprint(2, "serial: status reader %d \n", ser->ndata);
  634. /* consume it all */
  635. while(ser->ndata != 0){
  636. dsprint(2, "serial: status reader to consume: %d\n",
  637. ser->ndata);
  638. sendul(ser->w4data, 1);
  639. recvul(ser->gotdata);
  640. }
  641. }
  642. /* don't free a; epreader may still be using it. */
  643. free(c);
  644. closedev(ser->dev);
  645. }
  646. static int
  647. ftreset(Serial *ser)
  648. {
  649. ftdiwrite(ser, FTRESETCTLVAL, 0, FTRESET);
  650. return 0;
  651. }
  652. static int
  653. ftinit(Serial *ser)
  654. {
  655. qlock(ser);
  656. serialreset(ser);
  657. qunlock(ser);
  658. incref(ser->dev);
  659. threadcreate(statusreader, ser, 8*1024);
  660. return 0;
  661. }
  662. static int
  663. ftsetbreak(Serial *ser, int val)
  664. {
  665. return ftdiwrite(ser, (val != 0? FTSETBREAK: 0), 0, FTSETDATA);
  666. }
  667. static int
  668. ftclearpipes(Serial *ser)
  669. {
  670. /* maybe can be done in one... */
  671. ftdiwrite(ser, FTRESETCTLVALPURGETX, 0, FTRESET);
  672. ftdiwrite(ser, FTRESETCTLVALPURGERX, 0, FTRESET);
  673. return 0;
  674. }
  675. static int
  676. setctlline(Serial *ser, uchar val)
  677. {
  678. return ftdiwrite(ser, val | (val << 8), 0, FTSETMODEMCTRL);
  679. }
  680. static void
  681. updatectlst(Serial *ser, int val)
  682. {
  683. if(ser->rts)
  684. ser->ctlstate |= val;
  685. else
  686. ser->ctlstate &= ~val;
  687. }
  688. static int
  689. setctl(Serial *ser)
  690. {
  691. int res;
  692. if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTHETIRA1Did){
  693. fprint(2, "serial: cannot set lines for this device\n");
  694. updatectlst(ser, CtlRTS|CtlDTR);
  695. ser->rts = ser->dtr = 1;
  696. return -1;
  697. }
  698. /* NB: you can not set DTR and RTS with one control message */
  699. updatectlst(ser, CtlRTS);
  700. res = setctlline(ser, (CtlRTS<<8)|ser->ctlstate);
  701. if(res < 0)
  702. return res;
  703. updatectlst(ser, CtlDTR);
  704. res = setctlline(ser, (CtlDTR<<8)|ser->ctlstate);
  705. if(res < 0)
  706. return res;
  707. return 0;
  708. }
  709. static int
  710. ftsendlines(Serial *ser)
  711. {
  712. int res;
  713. dsprint(2, "serial: sendlines: %#2.2x\n", ser->ctlstate);
  714. res = setctl(ser);
  715. dsprint(2, "serial: sendlines res: %d\n", res);
  716. return 0;
  717. }
  718. static int
  719. ftseteps(Serial *ser)
  720. {
  721. char *s;
  722. s = smprint("maxpkt %d", Packsz);
  723. devctl(ser->epin, s);
  724. devctl(ser->epout, s);
  725. ser->maxread = ser->maxwrite = Packsz;
  726. free(s);
  727. return 0;
  728. }
  729. Serialops ftops = {
  730. .init = ftinit,
  731. .seteps = ftseteps,
  732. .setparam = ftsetparam,
  733. .clearpipes = ftclearpipes,
  734. .reset = ftreset,
  735. .sendlines = ftsendlines,
  736. .modemctl = ftmodemctl,
  737. .setbreak = ftsetbreak,
  738. .wait4data = wait4data,
  739. .wait4write = wait4write,
  740. };