ftdi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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, FTOpenRDUltDid},
  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. { FT8U232AMDid, FT4232HDid },
  203. { 0, 0 },
  204. };
  205. enum {
  206. Packsz = 64, /* default size */
  207. Maxpacksz = 512,
  208. Bufsiz = 4 * 1024,
  209. };
  210. static int
  211. ftdiread(Serialport *p, int index, int req, uchar *buf, int len)
  212. {
  213. int res;
  214. Serial *ser;
  215. ser = p->s;
  216. if(req != FTGETE2READ)
  217. index |= p->interfc + 1;
  218. dsprint(2, "serial: ftdiread %#p [%d] req: %#x val: %#x idx:%d buf:%p len:%d\n",
  219. p, p->interfc, req, 0, index, buf, len);
  220. res = usbcmd(ser->dev, Rd2h | Rftdireq | Rdev, req, 0, index, buf, len);
  221. dsprint(2, "serial: ftdiread res:%d\n", res);
  222. return res;
  223. }
  224. static int
  225. ftdiwrite(Serialport *p, int val, int index, int req)
  226. {
  227. int res;
  228. Serial *ser;
  229. ser = p->s;
  230. if(req != FTGETE2READ || req != FTSETE2ERASE || req != FTSETBAUDRATE)
  231. index |= p->interfc + 1;
  232. dsprint(2, "serial: ftdiwrite %#p [%d] req: %#x val: %#x idx:%d\n",
  233. p, p->interfc, req, val, index);
  234. res = usbcmd(ser->dev, Rh2d | Rftdireq | Rdev, req, val, index, nil, 0);
  235. dsprint(2, "serial: ftdiwrite res:%d\n", res);
  236. return res;
  237. }
  238. static int
  239. ftmodemctl(Serialport *p, int set)
  240. {
  241. if(set == 0){
  242. p->mctl = 0;
  243. ftdiwrite(p, 0, 0, FTSETMODEMCTRL);
  244. return 0;
  245. }
  246. p->mctl = 1;
  247. ftdiwrite(p, 0, FTRTSCTSHS, FTSETFLOWCTRL);
  248. return 0;
  249. }
  250. static ushort
  251. ft232ambaudbase2div(int baud, int base)
  252. {
  253. int divisor3;
  254. ushort divisor;
  255. divisor3 = (base / 2) / baud;
  256. if((divisor3 & 7) == 7)
  257. divisor3++; /* round x.7/8 up to x+1 */
  258. divisor = divisor3 >> 3;
  259. divisor3 &= 7;
  260. if(divisor3 == 1)
  261. divisor |= 0xc000; /* 0.125 */
  262. else if(divisor3 >= 4)
  263. divisor |= 0x4000; /* 0.5 */
  264. else if(divisor3 != 0)
  265. divisor |= 0x8000; /* 0.25 */
  266. if( divisor == 1)
  267. divisor = 0; /* special case for maximum baud rate */
  268. return divisor;
  269. }
  270. enum{
  271. ClockNew = 48000000,
  272. ClockOld = 12000000 / 16,
  273. HetiraDiv = 240,
  274. UirtDiv = 77,
  275. };
  276. static ushort
  277. ft232ambaud2div(int baud)
  278. {
  279. return ft232ambaudbase2div(baud, ClockNew);
  280. }
  281. static ulong divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7};
  282. static ulong
  283. ft232bmbaudbase2div(int baud, int base)
  284. {
  285. int divisor3;
  286. u32int divisor;
  287. divisor3 = (base / 2) / baud;
  288. divisor = divisor3 >> 3 | divfrac[divisor3 & 7] << 14;
  289. /* Deal with special cases for highest baud rates. */
  290. if( divisor == 1)
  291. divisor = 0; /* 1.0 */
  292. else if( divisor == 0x4001)
  293. divisor = 1; /* 1.5 */
  294. return divisor;
  295. }
  296. static ulong
  297. ft232bmbaud2div (int baud)
  298. {
  299. return ft232bmbaudbase2div (baud, ClockNew);
  300. }
  301. static int
  302. customdiv(Serial *ser)
  303. {
  304. if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTHETIRA1Did)
  305. return HetiraDiv;
  306. else if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTUSBUIRTDid)
  307. return UirtDiv;
  308. fprint(2, "serial: weird custom divisor\n");
  309. return 0; /* shouldn't happen, break as much as I can */
  310. }
  311. static ulong
  312. ftbaudcalcdiv(Serial *ser, int baud)
  313. {
  314. int cusdiv;
  315. ulong divval;
  316. if(baud == 38400 && (cusdiv = customdiv(ser)) != 0)
  317. baud = ser->baudbase / cusdiv;
  318. if(baud == 0)
  319. baud = 9600;
  320. switch(ser->type) {
  321. case SIO:
  322. switch(baud) {
  323. case 300:
  324. divval = FTb300;
  325. break;
  326. case 600:
  327. divval = FTb600;
  328. break;
  329. case 1200:
  330. divval = FTb1200;
  331. break;
  332. case 2400:
  333. divval = FTb2400;
  334. break;
  335. case 4800:
  336. divval = FTb4800;
  337. break;
  338. case 9600:
  339. divval = FTb9600;
  340. break;
  341. case 19200:
  342. divval = FTb19200;
  343. break;
  344. case 38400:
  345. divval = FTb38400;
  346. break;
  347. case 57600:
  348. divval = FTb57600;
  349. break;
  350. case 115200:
  351. divval = FTb115200;
  352. break;
  353. default:
  354. divval = FTb9600;
  355. break;
  356. }
  357. break;
  358. case FT8U232AM:
  359. if(baud <= 3000000)
  360. divval = ft232ambaud2div(baud);
  361. else
  362. divval = ft232ambaud2div(9600);
  363. break;
  364. case FT232BM:
  365. case FT2232C:
  366. case FTKINDR:
  367. case FT2232H:
  368. case FT4232H:
  369. if(baud <= 3000000)
  370. divval = ft232bmbaud2div(baud);
  371. else
  372. divval = ft232bmbaud2div(9600);
  373. break;
  374. default:
  375. divval = ft232bmbaud2div(9600);
  376. break;
  377. }
  378. return divval;
  379. }
  380. static int
  381. ftsetparam(Serialport *p)
  382. {
  383. int res;
  384. ushort val;
  385. ulong bauddiv;
  386. val = 0;
  387. if(p->stop == 1)
  388. val |= FTSETDATASTOPBITS1;
  389. else if(p->stop == 2)
  390. val |= FTSETDATASTOPBITS2;
  391. else if(p->stop == 15)
  392. val |= FTSETDATASTOPBITS15;
  393. switch(p->parity){
  394. case 0:
  395. val |= FTSETDATAParNONE;
  396. break;
  397. case 1:
  398. val |= FTSETDATAParODD;
  399. break;
  400. case 2:
  401. val |= FTSETDATAParEVEN;
  402. break;
  403. case 3:
  404. val |= FTSETDATAParMARK;
  405. break;
  406. case 4:
  407. val |= FTSETDATAParSPACE;
  408. break;
  409. };
  410. dsprint(2, "serial: setparam\n");
  411. res = ftdiwrite(p, val, 0, FTSETDATA);
  412. if(res < 0)
  413. return res;
  414. res = ftmodemctl(p, p->mctl);
  415. if(res < 0)
  416. return res;
  417. bauddiv = ftbaudcalcdiv(p->s, p->baud);
  418. res = ftdiwrite(p, bauddiv, (bauddiv>>16) & 1, FTSETBAUDRATE);
  419. dsprint(2, "serial: setparam res: %d\n", res);
  420. return res;
  421. }
  422. /* ser locked */
  423. static void
  424. ftgettype(Serial *ser)
  425. {
  426. int i, outhdrsz, dno, pksz;
  427. ulong baudbase;
  428. Conf *cnf;
  429. pksz = Packsz;
  430. /* Assume it is not the original SIO device for now. */
  431. baudbase = ClockNew / 2;
  432. outhdrsz = 0;
  433. dno = ser->dev->usb->dno;
  434. cnf = ser->dev->usb->conf[0];
  435. ser->nifcs = 0;
  436. for(i = 0; i < Niface; i++)
  437. if(cnf->iface[i] != nil)
  438. ser->nifcs++;
  439. if(ser->nifcs > 1) {
  440. /*
  441. * Multiple interfaces. default assume FT2232C,
  442. */
  443. if(dno == 0x500)
  444. ser->type = FT2232C;
  445. else if(dno == 0x600)
  446. ser->type = FTKINDR;
  447. else if(dno == 0x700){
  448. ser->type = FT2232H;
  449. pksz = Maxpacksz;
  450. } else if(dno == 0x800){
  451. ser->type = FT4232H;
  452. pksz = Maxpacksz;
  453. } else
  454. ser->type = FT2232C;
  455. ser->jtag = 0;
  456. /*
  457. * BM-type devices have a bug where dno gets set
  458. * to 0x200 when serial is 0.
  459. */
  460. if(dno < 0x500)
  461. fprint(2, "serial: warning: dno %d too low for "
  462. "multi-interface device\n", dno);
  463. } else if(dno < 0x200) {
  464. /* Old device. Assume it is the original SIO. */
  465. ser->type = SIO;
  466. baudbase = ClockOld/16;
  467. outhdrsz = 1;
  468. } else if(dno < 0x400)
  469. /*
  470. * Assume its an FT8U232AM (or FT8U245AM)
  471. * (It might be a BM because of the iSerialNumber bug,
  472. * but it will still work as an AM device.)
  473. */
  474. ser->type = FT8U232AM;
  475. else /* Assume it is an FT232BM (or FT245BM) */
  476. ser->type = FT232BM;
  477. ser->maxrtrans = ser->maxwtrans = pksz;
  478. ser->baudbase = baudbase;
  479. ser->outhdrsz = outhdrsz;
  480. ser->inhdrsz = 2;
  481. dsprint (2, "serial: detected type: %#x\n", ser->type);
  482. }
  483. int
  484. ftmatch(Serial *ser, char *info)
  485. {
  486. Cinfo *ip;
  487. char buf[50];
  488. for(ip = ftinfo; ip->vid != 0; ip++){
  489. snprint(buf, sizeof buf, "vid %#06x did %#06x", ip->vid, ip->did);
  490. dsprint(2, "serial: %s %s\n", buf, info);
  491. if(strstr(info, buf) != nil){
  492. if(ser != nil){
  493. qlock(ser);
  494. ftgettype(ser);
  495. qunlock(ser);
  496. }
  497. return 0;
  498. }
  499. }
  500. return -1;
  501. }
  502. static int
  503. ftuseinhdr(Serialport *p, uchar *b)
  504. {
  505. if(b[0] & FTICTS)
  506. p->cts = 1;
  507. else
  508. p->cts = 0;
  509. if(b[0] & FTIDSR)
  510. p->dsr = 1;
  511. else
  512. p->dsr = 0;
  513. if(b[0] & FTIRI)
  514. p->ring = 1;
  515. else
  516. p->ring = 0;
  517. if(b[0] & FTIRLSD)
  518. p->rlsd = 1;
  519. else
  520. p->rlsd = 0;
  521. if(b[1] & FTIOE)
  522. p->novererr++;
  523. if(b[1] & FTIPE)
  524. p->nparityerr++;
  525. if(b[1] & FTIFE)
  526. p->nframeerr++;
  527. if(b[1] & FTIBI)
  528. p->nbreakerr++;
  529. return 0;
  530. }
  531. static int
  532. ftsetouthdr(Serialport *p, uchar *b, int len)
  533. {
  534. if(p->s->outhdrsz != 0)
  535. b[0] = FTOPORT | (FTOLENMSK & len);
  536. return p->s->outhdrsz;
  537. }
  538. static int
  539. wait4data(Serialport *p, uchar *data, int count)
  540. {
  541. int d;
  542. Serial *ser;
  543. ser = p->s;
  544. qunlock(ser);
  545. d = sendul(p->w4data, 1);
  546. qlock(ser);
  547. if(d <= 0)
  548. return -1;
  549. if(p->ndata >= count)
  550. p->ndata -= count;
  551. else{
  552. count = p->ndata;
  553. p->ndata = 0;
  554. }
  555. assert(count >= 0);
  556. assert(p->ndata >= 0);
  557. memmove(data, p->data, count);
  558. if(p->ndata != 0)
  559. memmove(p->data, p->data+count, p->ndata);
  560. recvul(p->gotdata);
  561. return count;
  562. }
  563. static int
  564. wait4write(Serialport *p, uchar *data, int count)
  565. {
  566. int off, fd;
  567. uchar *b;
  568. Serial *ser;
  569. ser = p->s;
  570. b = emallocz(count+ser->outhdrsz, 1);
  571. off = ftsetouthdr(p, b, count);
  572. memmove(b+off, data, count);
  573. fd = p->epout->dfd;
  574. qunlock(ser);
  575. count = write(fd, b, count+off);
  576. qlock(ser);
  577. free(b);
  578. return count;
  579. }
  580. typedef struct Packser Packser;
  581. struct Packser{
  582. int nb;
  583. uchar b[Bufsiz];
  584. };
  585. typedef struct Areader Areader;
  586. struct Areader{
  587. Serialport *p;
  588. Channel *c;
  589. };
  590. static void
  591. shutdownchan(Channel *c)
  592. {
  593. Packser *bp;
  594. while((bp=nbrecvp(c)) != nil)
  595. free(bp);
  596. chanfree(c);
  597. }
  598. int
  599. cpdata(Serial *ser, Serialport *port, uchar *out, uchar *in, int sz)
  600. {
  601. int i, ncp, ntotcp, pksz;
  602. pksz = ser->maxrtrans;
  603. ntotcp = 0;
  604. for(i = 0; i < sz; i+= pksz){
  605. ftuseinhdr(port, in + i);
  606. if(sz - i > pksz)
  607. ncp = pksz - ser->inhdrsz;
  608. else
  609. ncp = sz - i - ser->inhdrsz;
  610. memmove(out, in + i + ser->inhdrsz, ncp);
  611. out += ncp;
  612. ntotcp += ncp;
  613. }
  614. return ntotcp;
  615. }
  616. static void
  617. epreader(void *u)
  618. {
  619. int dfd, rcount, cl;
  620. char err[40];
  621. Areader *a;
  622. Channel *c;
  623. Packser *pk;
  624. Serial *ser;
  625. Serialport *p;
  626. threadsetname("epreader proc");
  627. a = u;
  628. p = a->p;
  629. ser = p->s;
  630. c = a->c;
  631. free(a);
  632. qlock(ser);
  633. dfd = p->epin->dfd;
  634. qunlock(ser);
  635. pk = nil;
  636. do {
  637. if (pk == nil)
  638. pk = emallocz(sizeof(Packser), 1);
  639. rcount = read(dfd, pk->b, sizeof pk->b);
  640. if(serialdebug > 5)
  641. dsprint(2, "%d %#ux%#ux ", rcount, p->data[0],
  642. p->data[1]);
  643. if(rcount < 0)
  644. break;
  645. if(rcount == 0)
  646. continue;
  647. if(rcount >= ser->inhdrsz){
  648. rcount = cpdata(ser, p, pk->b, pk->b, rcount);
  649. if(rcount != 0){
  650. pk->nb = rcount;
  651. cl = sendp(c, pk);
  652. if(cl < 0){
  653. /*
  654. * if it was a time-out, I don't want
  655. * to give back an error.
  656. */
  657. rcount = 0;
  658. break;
  659. }
  660. }else
  661. free(pk);
  662. pk = nil;
  663. }
  664. } while(rcount >= 0 || (rcount < 0 && strstr(err, "timed out") != nil));
  665. if(rcount < 0)
  666. fprint(2, "%s: error reading %s: %r\n", argv0, p->fs.name);
  667. free(pk);
  668. nbsendp(c, nil);
  669. if(p->w4data != nil)
  670. chanclose(p->w4data);
  671. if(p->gotdata != nil)
  672. chanclose(p->gotdata);
  673. devctl(ser->dev, "detach");
  674. closedev(ser->dev);
  675. usbfsdel(&p->fs);
  676. }
  677. static void
  678. statusreader(void *u)
  679. {
  680. Areader *a;
  681. Channel *c;
  682. Packser *pk;
  683. Serialport *p;
  684. Serial *ser;
  685. int cl;
  686. p = u;
  687. ser = p->s;
  688. threadsetname("statusreader thread");
  689. /* big buffering, fewer bytes lost */
  690. c = chancreate(sizeof(Packser *), 128);
  691. a = emallocz(sizeof(Areader), 1);
  692. a->p = p;
  693. a->c = c;
  694. incref(ser->dev);
  695. proccreate(epreader, a, 16*1024);
  696. while((pk = recvp(c)) != nil){
  697. memmove(p->data, pk->b, pk->nb);
  698. p->ndata = pk->nb;
  699. free(pk);
  700. dsprint(2, "serial %p: status reader %d \n", p, p->ndata);
  701. /* consume it all */
  702. while(p->ndata != 0){
  703. dsprint(2, "serial %p: status reader to consume: %d\n",
  704. p, p->ndata);
  705. cl = recvul(p->w4data);
  706. if(cl < 0)
  707. break;
  708. cl = sendul(p->gotdata, 1);
  709. if(cl < 0)
  710. break;
  711. }
  712. }
  713. shutdownchan(c);
  714. devctl(ser->dev, "detach");
  715. closedev(ser->dev);
  716. usbfsdel(&p->fs);
  717. }
  718. static int
  719. ftreset(Serial *ser)
  720. {
  721. Serialport *p;
  722. int i;
  723. p = ser->p;
  724. for(i = 0; i < Maxifc; i++)
  725. if(p[i].s != nil)
  726. ftdiwrite(&p[i], FTRESETCTLVAL, 0, FTRESET);
  727. return 0;
  728. }
  729. static int
  730. ftinit(Serialport *p)
  731. {
  732. Serial *ser;
  733. uint timerval;
  734. int res;
  735. ser = p->s;
  736. if(p->isjtag){
  737. res = ftdiwrite(p, FTSETFLOWCTRL, 0, FTDISABLEFLOWCTRL);
  738. if(res < 0)
  739. return -1;
  740. res = ftdiread(p, FTSETLATENCYTIMER, 0, (uchar *)&timerval,
  741. FTLATENCYTIMERSZ);
  742. if(res < 0)
  743. return -1;
  744. dsprint(2, "serial: jtag latency timer is %d\n", timerval);
  745. timerval = 2;
  746. ftdiwrite(p, FTLATENCYDEFAULT, 0, FTSETLATENCYTIMER);
  747. res = ftdiread(p, FTSETLATENCYTIMER, 0, (uchar *)&timerval,
  748. FTLATENCYTIMERSZ);
  749. if(res < 0)
  750. return -1;
  751. dsprint(2, "serial: jtag latency timer set to %d\n", timerval);
  752. /* may be unnecessary */
  753. devctl(p->epin, "timeout 5000");
  754. devctl(p->epout, "timeout 5000");
  755. /* 0xb is the mask for lines. plug dependant? */
  756. ftdiwrite(p, BMMPSSE|0x0b, 0, FTSETBITMODE);
  757. }
  758. incref(ser->dev);
  759. threadcreate(statusreader, p, 8*1024);
  760. return 0;
  761. }
  762. static int
  763. ftsetbreak(Serialport *p, int val)
  764. {
  765. return ftdiwrite(p, (val != 0? FTSETBREAK: 0), 0, FTSETDATA);
  766. }
  767. static int
  768. ftclearpipes(Serialport *p)
  769. {
  770. /* maybe can be done in one... */
  771. ftdiwrite(p, FTRESETCTLVALPURGETX, 0, FTRESET);
  772. ftdiwrite(p, FTRESETCTLVALPURGERX, 0, FTRESET);
  773. return 0;
  774. }
  775. static int
  776. setctlline(Serialport *p, uchar val)
  777. {
  778. return ftdiwrite(p, val | (val << 8), 0, FTSETMODEMCTRL);
  779. }
  780. static void
  781. updatectlst(Serialport *p, int val)
  782. {
  783. if(p->rts)
  784. p->ctlstate |= val;
  785. else
  786. p->ctlstate &= ~val;
  787. }
  788. static int
  789. setctl(Serialport *p)
  790. {
  791. int res;
  792. Serial *ser;
  793. ser = p->s;
  794. if(ser->dev->usb->vid == FTVid && ser->dev->usb->did == FTHETIRA1Did){
  795. fprint(2, "serial: cannot set lines for this device\n");
  796. updatectlst(p, CtlRTS|CtlDTR);
  797. p->rts = p->dtr = 1;
  798. return -1;
  799. }
  800. /* NB: you can not set DTR and RTS with one control message */
  801. updatectlst(p, CtlRTS);
  802. res = setctlline(p, (CtlRTS<<8)|p->ctlstate);
  803. if(res < 0)
  804. return res;
  805. updatectlst(p, CtlDTR);
  806. res = setctlline(p, (CtlDTR<<8)|p->ctlstate);
  807. if(res < 0)
  808. return res;
  809. return 0;
  810. }
  811. static int
  812. ftsendlines(Serialport *p)
  813. {
  814. int res;
  815. dsprint(2, "serial: sendlines: %#2.2x\n", p->ctlstate);
  816. res = setctl(p);
  817. dsprint(2, "serial: sendlines res: %d\n", res);
  818. return 0;
  819. }
  820. static int
  821. ftseteps(Serialport *p)
  822. {
  823. char *s;
  824. Serial *ser;
  825. ser = p->s;
  826. s = smprint("maxpkt %d", ser->maxrtrans);
  827. devctl(p->epin, s);
  828. free(s);
  829. s = smprint("maxpkt %d", ser->maxwtrans);
  830. devctl(p->epout, s);
  831. free(s);
  832. return 0;
  833. }
  834. Serialops ftops = {
  835. .init = ftinit,
  836. .seteps = ftseteps,
  837. .setparam = ftsetparam,
  838. .clearpipes = ftclearpipes,
  839. .reset = ftreset,
  840. .sendlines = ftsendlines,
  841. .modemctl = ftmodemctl,
  842. .setbreak = ftsetbreak,
  843. .wait4data = wait4data,
  844. .wait4write = wait4write,
  845. };