ftdi.c 17 KB

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