dgnc_tty.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993
  1. /*
  2. * Copyright 2003 Digi International (www.digi.com)
  3. * Scott H Kilau <Scott_Kilau at digi dot com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
  12. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. * PURPOSE. See the GNU General Public License for more details.
  14. */
  15. /************************************************************************
  16. *
  17. * This file implements the tty driver functionality for the
  18. * Neo and ClassicBoard PCI based product lines.
  19. *
  20. ************************************************************************
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h> /* For jiffies, task states */
  25. #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
  26. #include <linux/module.h>
  27. #include <linux/ctype.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/types.h>
  31. #include <linux/serial_reg.h>
  32. #include <linux/slab.h>
  33. #include <linux/delay.h> /* For udelay */
  34. #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
  35. #include <linux/pci.h>
  36. #include "dgnc_driver.h"
  37. #include "dgnc_tty.h"
  38. #include "dgnc_neo.h"
  39. #include "dgnc_cls.h"
  40. #include "dgnc_sysfs.h"
  41. #include "dgnc_utils.h"
  42. /*
  43. * internal variables
  44. */
  45. static struct dgnc_board *dgnc_BoardsByMajor[256];
  46. static unsigned char *dgnc_TmpWriteBuf;
  47. /*
  48. * Default transparent print information.
  49. */
  50. static struct digi_t dgnc_digi_init = {
  51. .digi_flags = DIGI_COOK, /* Flags */
  52. .digi_maxcps = 100, /* Max CPS */
  53. .digi_maxchar = 50, /* Max chars in print queue */
  54. .digi_bufsize = 100, /* Printer buffer size */
  55. .digi_onlen = 4, /* size of printer on string */
  56. .digi_offlen = 4, /* size of printer off string */
  57. .digi_onstr = "\033[5i", /* ANSI printer on string ] */
  58. .digi_offstr = "\033[4i", /* ANSI printer off string ] */
  59. .digi_term = "ansi" /* default terminal type */
  60. };
  61. /*
  62. * Define a local default termios struct. All ports will be created
  63. * with this termios initially.
  64. *
  65. * This defines a raw port at 9600 baud, 8 data bits, no parity,
  66. * 1 stop bit.
  67. */
  68. static struct ktermios DgncDefaultTermios = {
  69. .c_iflag = (DEFAULT_IFLAGS), /* iflags */
  70. .c_oflag = (DEFAULT_OFLAGS), /* oflags */
  71. .c_cflag = (DEFAULT_CFLAGS), /* cflags */
  72. .c_lflag = (DEFAULT_LFLAGS), /* lflags */
  73. .c_cc = INIT_C_CC,
  74. .c_line = 0,
  75. };
  76. /* Our function prototypes */
  77. static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
  78. static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
  79. static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
  80. struct channel_t *ch);
  81. static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  82. unsigned long arg);
  83. static int dgnc_tty_digigeta(struct tty_struct *tty,
  84. struct digi_t __user *retinfo);
  85. static int dgnc_tty_digiseta(struct tty_struct *tty,
  86. struct digi_t __user *new_info);
  87. static int dgnc_tty_write_room(struct tty_struct *tty);
  88. static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
  89. static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
  90. static void dgnc_tty_start(struct tty_struct *tty);
  91. static void dgnc_tty_stop(struct tty_struct *tty);
  92. static void dgnc_tty_throttle(struct tty_struct *tty);
  93. static void dgnc_tty_unthrottle(struct tty_struct *tty);
  94. static void dgnc_tty_flush_chars(struct tty_struct *tty);
  95. static void dgnc_tty_flush_buffer(struct tty_struct *tty);
  96. static void dgnc_tty_hangup(struct tty_struct *tty);
  97. static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command,
  98. unsigned int __user *value);
  99. static int dgnc_get_modem_info(struct channel_t *ch,
  100. unsigned int __user *value);
  101. static int dgnc_tty_tiocmget(struct tty_struct *tty);
  102. static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
  103. unsigned int clear);
  104. static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
  105. static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
  106. static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
  107. int count);
  108. static void dgnc_tty_set_termios(struct tty_struct *tty,
  109. struct ktermios *old_termios);
  110. static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
  111. static const struct tty_operations dgnc_tty_ops = {
  112. .open = dgnc_tty_open,
  113. .close = dgnc_tty_close,
  114. .write = dgnc_tty_write,
  115. .write_room = dgnc_tty_write_room,
  116. .flush_buffer = dgnc_tty_flush_buffer,
  117. .chars_in_buffer = dgnc_tty_chars_in_buffer,
  118. .flush_chars = dgnc_tty_flush_chars,
  119. .ioctl = dgnc_tty_ioctl,
  120. .set_termios = dgnc_tty_set_termios,
  121. .stop = dgnc_tty_stop,
  122. .start = dgnc_tty_start,
  123. .throttle = dgnc_tty_throttle,
  124. .unthrottle = dgnc_tty_unthrottle,
  125. .hangup = dgnc_tty_hangup,
  126. .put_char = dgnc_tty_put_char,
  127. .tiocmget = dgnc_tty_tiocmget,
  128. .tiocmset = dgnc_tty_tiocmset,
  129. .break_ctl = dgnc_tty_send_break,
  130. .wait_until_sent = dgnc_tty_wait_until_sent,
  131. .send_xchar = dgnc_tty_send_xchar
  132. };
  133. /************************************************************************
  134. *
  135. * TTY Initialization/Cleanup Functions
  136. *
  137. ************************************************************************/
  138. /*
  139. * dgnc_tty_preinit()
  140. *
  141. * Initialize any global tty related data before we download any boards.
  142. */
  143. int dgnc_tty_preinit(void)
  144. {
  145. /*
  146. * Allocate a buffer for doing the copy from user space to
  147. * kernel space in dgnc_write(). We only use one buffer and
  148. * control access to it with a semaphore. If we are paging, we
  149. * are already in trouble so one buffer won't hurt much anyway.
  150. *
  151. * We are okay to sleep in the malloc, as this routine
  152. * is only called during module load, (not in interrupt context),
  153. * and with no locks held.
  154. */
  155. dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
  156. if (!dgnc_TmpWriteBuf)
  157. return -ENOMEM;
  158. return 0;
  159. }
  160. /*
  161. * dgnc_tty_register()
  162. *
  163. * Init the tty subsystem for this board.
  164. */
  165. int dgnc_tty_register(struct dgnc_board *brd)
  166. {
  167. int rc = 0;
  168. brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
  169. snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
  170. brd->SerialDriver.name = brd->SerialName;
  171. brd->SerialDriver.name_base = 0;
  172. brd->SerialDriver.major = 0;
  173. brd->SerialDriver.minor_start = 0;
  174. brd->SerialDriver.num = brd->maxports;
  175. brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
  176. brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
  177. brd->SerialDriver.init_termios = DgncDefaultTermios;
  178. brd->SerialDriver.driver_name = DRVSTR;
  179. brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW |
  180. TTY_DRIVER_DYNAMIC_DEV |
  181. TTY_DRIVER_HARDWARE_BREAK);
  182. /*
  183. * The kernel wants space to store pointers to
  184. * tty_struct's and termios's.
  185. */
  186. brd->SerialDriver.ttys = kcalloc(brd->maxports,
  187. sizeof(*brd->SerialDriver.ttys),
  188. GFP_KERNEL);
  189. if (!brd->SerialDriver.ttys)
  190. return -ENOMEM;
  191. kref_init(&brd->SerialDriver.kref);
  192. brd->SerialDriver.termios = kcalloc(brd->maxports,
  193. sizeof(*brd->SerialDriver.termios),
  194. GFP_KERNEL);
  195. if (!brd->SerialDriver.termios)
  196. return -ENOMEM;
  197. /*
  198. * Entry points for driver. Called by the kernel from
  199. * tty_io.c and n_tty.c.
  200. */
  201. tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
  202. if (!brd->dgnc_Major_Serial_Registered) {
  203. /* Register tty devices */
  204. rc = tty_register_driver(&brd->SerialDriver);
  205. if (rc < 0) {
  206. dev_dbg(&brd->pdev->dev,
  207. "Can't register tty device (%d)\n", rc);
  208. return rc;
  209. }
  210. brd->dgnc_Major_Serial_Registered = true;
  211. }
  212. /*
  213. * If we're doing transparent print, we have to do all of the above
  214. * again, separately so we don't get the LD confused about what major
  215. * we are when we get into the dgnc_tty_open() routine.
  216. */
  217. brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
  218. snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
  219. brd->PrintDriver.name = brd->PrintName;
  220. brd->PrintDriver.name_base = 0;
  221. brd->PrintDriver.major = brd->SerialDriver.major;
  222. brd->PrintDriver.minor_start = 0x80;
  223. brd->PrintDriver.num = brd->maxports;
  224. brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
  225. brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
  226. brd->PrintDriver.init_termios = DgncDefaultTermios;
  227. brd->PrintDriver.driver_name = DRVSTR;
  228. brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW |
  229. TTY_DRIVER_DYNAMIC_DEV |
  230. TTY_DRIVER_HARDWARE_BREAK);
  231. /*
  232. * The kernel wants space to store pointers to
  233. * tty_struct's and termios's. Must be separated from
  234. * the Serial Driver so we don't get confused
  235. */
  236. brd->PrintDriver.ttys = kcalloc(brd->maxports,
  237. sizeof(*brd->PrintDriver.ttys),
  238. GFP_KERNEL);
  239. if (!brd->PrintDriver.ttys)
  240. return -ENOMEM;
  241. kref_init(&brd->PrintDriver.kref);
  242. brd->PrintDriver.termios = kcalloc(brd->maxports,
  243. sizeof(*brd->PrintDriver.termios),
  244. GFP_KERNEL);
  245. if (!brd->PrintDriver.termios)
  246. return -ENOMEM;
  247. /*
  248. * Entry points for driver. Called by the kernel from
  249. * tty_io.c and n_tty.c.
  250. */
  251. tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
  252. if (!brd->dgnc_Major_TransparentPrint_Registered) {
  253. /* Register Transparent Print devices */
  254. rc = tty_register_driver(&brd->PrintDriver);
  255. if (rc < 0) {
  256. dev_dbg(&brd->pdev->dev,
  257. "Can't register Transparent Print device(%d)\n",
  258. rc);
  259. return rc;
  260. }
  261. brd->dgnc_Major_TransparentPrint_Registered = true;
  262. }
  263. dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
  264. brd->dgnc_Serial_Major = brd->SerialDriver.major;
  265. brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
  266. return rc;
  267. }
  268. /*
  269. * dgnc_tty_init()
  270. *
  271. * Init the tty subsystem. Called once per board after board has been
  272. * downloaded and init'ed.
  273. */
  274. int dgnc_tty_init(struct dgnc_board *brd)
  275. {
  276. int i;
  277. void __iomem *vaddr;
  278. struct channel_t *ch;
  279. if (!brd)
  280. return -ENXIO;
  281. /*
  282. * Initialize board structure elements.
  283. */
  284. vaddr = brd->re_map_membase;
  285. brd->nasync = brd->maxports;
  286. for (i = 0; i < brd->nasync; i++) {
  287. /*
  288. * Okay to malloc with GFP_KERNEL, we are not at
  289. * interrupt context, and there are no locks held.
  290. */
  291. brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
  292. GFP_KERNEL);
  293. if (!brd->channels[i])
  294. goto err_free_channels;
  295. }
  296. ch = brd->channels[0];
  297. vaddr = brd->re_map_membase;
  298. /* Set up channel variables */
  299. for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
  300. spin_lock_init(&ch->ch_lock);
  301. /* Store all our magic numbers */
  302. ch->magic = DGNC_CHANNEL_MAGIC;
  303. ch->ch_tun.magic = DGNC_UNIT_MAGIC;
  304. ch->ch_tun.un_ch = ch;
  305. ch->ch_tun.un_type = DGNC_SERIAL;
  306. ch->ch_tun.un_dev = i;
  307. ch->ch_pun.magic = DGNC_UNIT_MAGIC;
  308. ch->ch_pun.un_ch = ch;
  309. ch->ch_pun.un_type = DGNC_PRINT;
  310. ch->ch_pun.un_dev = i + 128;
  311. if (brd->bd_uart_offset == 0x200)
  312. ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
  313. else
  314. ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
  315. ch->ch_bd = brd;
  316. ch->ch_portnum = i;
  317. ch->ch_digi = dgnc_digi_init;
  318. /* .25 second delay */
  319. ch->ch_close_delay = 250;
  320. init_waitqueue_head(&ch->ch_flags_wait);
  321. init_waitqueue_head(&ch->ch_tun.un_flags_wait);
  322. init_waitqueue_head(&ch->ch_pun.un_flags_wait);
  323. {
  324. struct device *classp;
  325. classp = tty_register_device(&brd->SerialDriver, i,
  326. &ch->ch_bd->pdev->dev);
  327. ch->ch_tun.un_sysfs = classp;
  328. dgnc_create_tty_sysfs(&ch->ch_tun, classp);
  329. classp = tty_register_device(&brd->PrintDriver, i,
  330. &ch->ch_bd->pdev->dev);
  331. ch->ch_pun.un_sysfs = classp;
  332. dgnc_create_tty_sysfs(&ch->ch_pun, classp);
  333. }
  334. }
  335. return 0;
  336. err_free_channels:
  337. for (i = i - 1; i >= 0; --i) {
  338. kfree(brd->channels[i]);
  339. brd->channels[i] = NULL;
  340. }
  341. return -ENOMEM;
  342. }
  343. /*
  344. * dgnc_tty_post_uninit()
  345. *
  346. * UnInitialize any global tty related data.
  347. */
  348. void dgnc_tty_post_uninit(void)
  349. {
  350. kfree(dgnc_TmpWriteBuf);
  351. dgnc_TmpWriteBuf = NULL;
  352. }
  353. /*
  354. * dgnc_tty_uninit()
  355. *
  356. * Uninitialize the TTY portion of this driver. Free all memory and
  357. * resources.
  358. */
  359. void dgnc_tty_uninit(struct dgnc_board *brd)
  360. {
  361. int i = 0;
  362. if (brd->dgnc_Major_Serial_Registered) {
  363. dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
  364. brd->dgnc_Serial_Major = 0;
  365. for (i = 0; i < brd->nasync; i++) {
  366. if (brd->channels[i])
  367. dgnc_remove_tty_sysfs(brd->channels[i]->
  368. ch_tun.un_sysfs);
  369. tty_unregister_device(&brd->SerialDriver, i);
  370. }
  371. tty_unregister_driver(&brd->SerialDriver);
  372. brd->dgnc_Major_Serial_Registered = false;
  373. }
  374. if (brd->dgnc_Major_TransparentPrint_Registered) {
  375. dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
  376. brd->dgnc_TransparentPrint_Major = 0;
  377. for (i = 0; i < brd->nasync; i++) {
  378. if (brd->channels[i])
  379. dgnc_remove_tty_sysfs(brd->channels[i]->
  380. ch_pun.un_sysfs);
  381. tty_unregister_device(&brd->PrintDriver, i);
  382. }
  383. tty_unregister_driver(&brd->PrintDriver);
  384. brd->dgnc_Major_TransparentPrint_Registered = false;
  385. }
  386. kfree(brd->SerialDriver.ttys);
  387. brd->SerialDriver.ttys = NULL;
  388. kfree(brd->SerialDriver.termios);
  389. brd->SerialDriver.termios = NULL;
  390. kfree(brd->PrintDriver.ttys);
  391. brd->PrintDriver.ttys = NULL;
  392. kfree(brd->PrintDriver.termios);
  393. brd->PrintDriver.termios = NULL;
  394. }
  395. /*=======================================================================
  396. *
  397. * dgnc_wmove - Write data to transmit queue.
  398. *
  399. * ch - Pointer to channel structure.
  400. * buf - Poiter to characters to be moved.
  401. * n - Number of characters to move.
  402. *
  403. *=======================================================================*/
  404. static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
  405. {
  406. int remain;
  407. uint head;
  408. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  409. return;
  410. head = ch->ch_w_head & WQUEUEMASK;
  411. /*
  412. * If the write wraps over the top of the circular buffer,
  413. * move the portion up to the wrap point, and reset the
  414. * pointers to the bottom.
  415. */
  416. remain = WQUEUESIZE - head;
  417. if (n >= remain) {
  418. n -= remain;
  419. memcpy(ch->ch_wqueue + head, buf, remain);
  420. head = 0;
  421. buf += remain;
  422. }
  423. if (n > 0) {
  424. /*
  425. * Move rest of data.
  426. */
  427. remain = n;
  428. memcpy(ch->ch_wqueue + head, buf, remain);
  429. head += remain;
  430. }
  431. head &= WQUEUEMASK;
  432. ch->ch_w_head = head;
  433. }
  434. /*=======================================================================
  435. *
  436. * dgnc_input - Process received data.
  437. *
  438. * ch - Pointer to channel structure.
  439. *
  440. *=======================================================================*/
  441. void dgnc_input(struct channel_t *ch)
  442. {
  443. struct dgnc_board *bd;
  444. struct tty_struct *tp;
  445. struct tty_ldisc *ld = NULL;
  446. uint rmask;
  447. ushort head;
  448. ushort tail;
  449. int data_len;
  450. unsigned long flags;
  451. int flip_len;
  452. int len = 0;
  453. int n = 0;
  454. int s = 0;
  455. int i = 0;
  456. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  457. return;
  458. tp = ch->ch_tun.un_tty;
  459. bd = ch->ch_bd;
  460. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  461. return;
  462. spin_lock_irqsave(&ch->ch_lock, flags);
  463. /*
  464. * Figure the number of characters in the buffer.
  465. * Exit immediately if none.
  466. */
  467. rmask = RQUEUEMASK;
  468. head = ch->ch_r_head & rmask;
  469. tail = ch->ch_r_tail & rmask;
  470. data_len = (head - tail) & rmask;
  471. if (data_len == 0)
  472. goto exit_unlock;
  473. /*
  474. * If the device is not open, or CREAD is off,
  475. * flush input data and return immediately.
  476. */
  477. if (!tp || (tp->magic != TTY_MAGIC) ||
  478. !(ch->ch_tun.un_flags & UN_ISOPEN) ||
  479. !(tp->termios.c_cflag & CREAD) ||
  480. (ch->ch_tun.un_flags & UN_CLOSING)) {
  481. ch->ch_r_head = tail;
  482. /* Force queue flow control to be released, if needed */
  483. dgnc_check_queue_flow_control(ch);
  484. goto exit_unlock;
  485. }
  486. /*
  487. * If we are throttled, simply don't read any data.
  488. */
  489. if (ch->ch_flags & CH_FORCED_STOPI)
  490. goto exit_unlock;
  491. flip_len = TTY_FLIPBUF_SIZE;
  492. /* Chop down the length, if needed */
  493. len = min(data_len, flip_len);
  494. len = min(len, (N_TTY_BUF_SIZE - 1));
  495. ld = tty_ldisc_ref(tp);
  496. /*
  497. * If we were unable to get a reference to the ld,
  498. * don't flush our buffer, and act like the ld doesn't
  499. * have any space to put the data right now.
  500. */
  501. if (!ld) {
  502. len = 0;
  503. } else {
  504. /*
  505. * If ld doesn't have a pointer to a receive_buf function,
  506. * flush the data, then act like the ld doesn't have any
  507. * space to put the data right now.
  508. */
  509. if (!ld->ops->receive_buf) {
  510. ch->ch_r_head = ch->ch_r_tail;
  511. len = 0;
  512. }
  513. }
  514. if (len <= 0)
  515. goto exit_unlock;
  516. /*
  517. * The tty layer in the kernel has changed in 2.6.16+.
  518. *
  519. * The flip buffers in the tty structure are no longer exposed,
  520. * and probably will be going away eventually.
  521. *
  522. * If we are completely raw, we don't need to go through a lot
  523. * of the tty layers that exist.
  524. * In this case, we take the shortest and fastest route we
  525. * can to relay the data to the user.
  526. *
  527. * On the other hand, if we are not raw, we need to go through
  528. * the new 2.6.16+ tty layer, which has its API more well defined.
  529. */
  530. len = tty_buffer_request_room(tp->port, len);
  531. n = len;
  532. /*
  533. * n now contains the most amount of data we can copy,
  534. * bounded either by how much the Linux tty layer can handle,
  535. * or the amount of data the card actually has pending...
  536. */
  537. while (n) {
  538. s = ((head >= tail) ? head : RQUEUESIZE) - tail;
  539. s = min(s, n);
  540. if (s <= 0)
  541. break;
  542. /*
  543. * If conditions are such that ld needs to see all
  544. * UART errors, we will have to walk each character
  545. * and error byte and send them to the buffer one at
  546. * a time.
  547. */
  548. if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
  549. for (i = 0; i < s; i++) {
  550. if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
  551. tty_insert_flip_char(tp->port,
  552. *(ch->ch_rqueue + tail + i),
  553. TTY_BREAK);
  554. else if (*(ch->ch_equeue + tail + i) &
  555. UART_LSR_PE)
  556. tty_insert_flip_char(tp->port,
  557. *(ch->ch_rqueue + tail + i),
  558. TTY_PARITY);
  559. else if (*(ch->ch_equeue + tail + i) &
  560. UART_LSR_FE)
  561. tty_insert_flip_char(tp->port,
  562. *(ch->ch_rqueue + tail + i),
  563. TTY_FRAME);
  564. else
  565. tty_insert_flip_char(tp->port,
  566. *(ch->ch_rqueue + tail + i),
  567. TTY_NORMAL);
  568. }
  569. } else {
  570. tty_insert_flip_string(tp->port,
  571. ch->ch_rqueue + tail,
  572. s);
  573. }
  574. tail += s;
  575. n -= s;
  576. /* Flip queue if needed */
  577. tail &= rmask;
  578. }
  579. ch->ch_r_tail = tail & rmask;
  580. ch->ch_e_tail = tail & rmask;
  581. dgnc_check_queue_flow_control(ch);
  582. spin_unlock_irqrestore(&ch->ch_lock, flags);
  583. /* Tell the tty layer its okay to "eat" the data now */
  584. tty_flip_buffer_push(tp->port);
  585. if (ld)
  586. tty_ldisc_deref(ld);
  587. return;
  588. exit_unlock:
  589. spin_unlock_irqrestore(&ch->ch_lock, flags);
  590. if (ld)
  591. tty_ldisc_deref(ld);
  592. }
  593. /************************************************************************
  594. * Determines when CARRIER changes state and takes appropriate
  595. * action.
  596. ************************************************************************/
  597. void dgnc_carrier(struct channel_t *ch)
  598. {
  599. struct dgnc_board *bd;
  600. int virt_carrier = 0;
  601. int phys_carrier = 0;
  602. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  603. return;
  604. bd = ch->ch_bd;
  605. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  606. return;
  607. if (ch->ch_mistat & UART_MSR_DCD)
  608. phys_carrier = 1;
  609. if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
  610. virt_carrier = 1;
  611. if (ch->ch_c_cflag & CLOCAL)
  612. virt_carrier = 1;
  613. /*
  614. * Test for a VIRTUAL carrier transition to HIGH.
  615. */
  616. if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
  617. /*
  618. * When carrier rises, wake any threads waiting
  619. * for carrier in the open routine.
  620. */
  621. if (waitqueue_active(&ch->ch_flags_wait))
  622. wake_up_interruptible(&ch->ch_flags_wait);
  623. }
  624. /*
  625. * Test for a PHYSICAL carrier transition to HIGH.
  626. */
  627. if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
  628. /*
  629. * When carrier rises, wake any threads waiting
  630. * for carrier in the open routine.
  631. */
  632. if (waitqueue_active(&ch->ch_flags_wait))
  633. wake_up_interruptible(&ch->ch_flags_wait);
  634. }
  635. /*
  636. * Test for a PHYSICAL transition to low, so long as we aren't
  637. * currently ignoring physical transitions (which is what "virtual
  638. * carrier" indicates).
  639. *
  640. * The transition of the virtual carrier to low really doesn't
  641. * matter... it really only means "ignore carrier state", not
  642. * "make pretend that carrier is there".
  643. */
  644. if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
  645. (phys_carrier == 0)) {
  646. /*
  647. * When carrier drops:
  648. *
  649. * Drop carrier on all open units.
  650. *
  651. * Flush queues, waking up any task waiting in the
  652. * line discipline.
  653. *
  654. * Send a hangup to the control terminal.
  655. *
  656. * Enable all select calls.
  657. */
  658. if (waitqueue_active(&ch->ch_flags_wait))
  659. wake_up_interruptible(&ch->ch_flags_wait);
  660. if (ch->ch_tun.un_open_count > 0)
  661. tty_hangup(ch->ch_tun.un_tty);
  662. if (ch->ch_pun.un_open_count > 0)
  663. tty_hangup(ch->ch_pun.un_tty);
  664. }
  665. /*
  666. * Make sure that our cached values reflect the current reality.
  667. */
  668. if (virt_carrier == 1)
  669. ch->ch_flags |= CH_FCAR;
  670. else
  671. ch->ch_flags &= ~CH_FCAR;
  672. if (phys_carrier == 1)
  673. ch->ch_flags |= CH_CD;
  674. else
  675. ch->ch_flags &= ~CH_CD;
  676. }
  677. /*
  678. * Assign the custom baud rate to the channel structure
  679. */
  680. static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
  681. {
  682. int testdiv;
  683. int testrate_high;
  684. int testrate_low;
  685. int deltahigh;
  686. int deltalow;
  687. if (newrate <= 0) {
  688. ch->ch_custom_speed = 0;
  689. return;
  690. }
  691. /*
  692. * Since the divisor is stored in a 16-bit integer, we make sure
  693. * we don't allow any rates smaller than a 16-bit integer would allow.
  694. * And of course, rates above the dividend won't fly.
  695. */
  696. if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
  697. newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
  698. if (newrate && newrate > ch->ch_bd->bd_dividend)
  699. newrate = ch->ch_bd->bd_dividend;
  700. if (newrate > 0) {
  701. testdiv = ch->ch_bd->bd_dividend / newrate;
  702. /*
  703. * If we try to figure out what rate the board would use
  704. * with the test divisor, it will be either equal or higher
  705. * than the requested baud rate. If we then determine the
  706. * rate with a divisor one higher, we will get the next lower
  707. * supported rate below the requested.
  708. */
  709. testrate_high = ch->ch_bd->bd_dividend / testdiv;
  710. testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
  711. /*
  712. * If the rate for the requested divisor is correct, just
  713. * use it and be done.
  714. */
  715. if (testrate_high != newrate) {
  716. /*
  717. * Otherwise, pick the rate that is closer
  718. * (i.e. whichever rate has a smaller delta).
  719. */
  720. deltahigh = testrate_high - newrate;
  721. deltalow = newrate - testrate_low;
  722. if (deltahigh < deltalow)
  723. newrate = testrate_high;
  724. else
  725. newrate = testrate_low;
  726. }
  727. }
  728. ch->ch_custom_speed = newrate;
  729. }
  730. void dgnc_check_queue_flow_control(struct channel_t *ch)
  731. {
  732. int qleft;
  733. /* Store how much space we have left in the queue */
  734. qleft = ch->ch_r_tail - ch->ch_r_head - 1;
  735. if (qleft < 0)
  736. qleft += RQUEUEMASK + 1;
  737. /*
  738. * Check to see if we should enforce flow control on our queue because
  739. * the ld (or user) isn't reading data out of our queue fast enuf.
  740. *
  741. * NOTE: This is done based on what the current flow control of the
  742. * port is set for.
  743. *
  744. * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
  745. * This will cause the UART's FIFO to back up, and force
  746. * the RTS signal to be dropped.
  747. * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
  748. * the other side, in hopes it will stop sending data to us.
  749. * 3) NONE - Nothing we can do. We will simply drop any extra data
  750. * that gets sent into us when the queue fills up.
  751. */
  752. if (qleft < 256) {
  753. /* HWFLOW */
  754. if (ch->ch_digi.digi_flags & CTSPACE ||
  755. ch->ch_c_cflag & CRTSCTS) {
  756. if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
  757. ch->ch_bd->bd_ops->disable_receiver(ch);
  758. ch->ch_flags |= (CH_RECEIVER_OFF);
  759. }
  760. }
  761. /* SWFLOW */
  762. else if (ch->ch_c_iflag & IXOFF) {
  763. if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
  764. ch->ch_bd->bd_ops->send_stop_character(ch);
  765. ch->ch_stops_sent++;
  766. }
  767. }
  768. }
  769. /*
  770. * Check to see if we should unenforce flow control because
  771. * ld (or user) finally read enuf data out of our queue.
  772. *
  773. * NOTE: This is done based on what the current flow control of the
  774. * port is set for.
  775. *
  776. * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
  777. * This will cause the UART's FIFO to raise RTS back up,
  778. * which will allow the other side to start sending data again.
  779. * 2) SWFLOW (IXOFF) - Send a start character to
  780. * the other side, so it will start sending data to us again.
  781. * 3) NONE - Do nothing. Since we didn't do anything to turn off the
  782. * other side, we don't need to do anything now.
  783. */
  784. if (qleft > (RQUEUESIZE / 2)) {
  785. /* HWFLOW */
  786. if (ch->ch_digi.digi_flags & RTSPACE ||
  787. ch->ch_c_cflag & CRTSCTS) {
  788. if (ch->ch_flags & CH_RECEIVER_OFF) {
  789. ch->ch_bd->bd_ops->enable_receiver(ch);
  790. ch->ch_flags &= ~(CH_RECEIVER_OFF);
  791. }
  792. }
  793. /* SWFLOW */
  794. else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
  795. ch->ch_stops_sent = 0;
  796. ch->ch_bd->bd_ops->send_start_character(ch);
  797. }
  798. }
  799. }
  800. void dgnc_wakeup_writes(struct channel_t *ch)
  801. {
  802. int qlen = 0;
  803. unsigned long flags;
  804. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  805. return;
  806. spin_lock_irqsave(&ch->ch_lock, flags);
  807. /*
  808. * If channel now has space, wake up anyone waiting on the condition.
  809. */
  810. qlen = ch->ch_w_head - ch->ch_w_tail;
  811. if (qlen < 0)
  812. qlen += WQUEUESIZE;
  813. if (qlen >= (WQUEUESIZE - 256)) {
  814. spin_unlock_irqrestore(&ch->ch_lock, flags);
  815. return;
  816. }
  817. if (ch->ch_tun.un_flags & UN_ISOPEN) {
  818. if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  819. ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
  820. spin_unlock_irqrestore(&ch->ch_lock, flags);
  821. ch->ch_tun.un_tty->ldisc->ops->write_wakeup(ch->ch_tun.un_tty);
  822. spin_lock_irqsave(&ch->ch_lock, flags);
  823. }
  824. wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
  825. /*
  826. * If unit is set to wait until empty, check to make sure
  827. * the queue AND FIFO are both empty.
  828. */
  829. if (ch->ch_tun.un_flags & UN_EMPTY) {
  830. if ((qlen == 0) &&
  831. (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
  832. ch->ch_tun.un_flags &= ~(UN_EMPTY);
  833. /*
  834. * If RTS Toggle mode is on, whenever
  835. * the queue and UART is empty, keep RTS low.
  836. */
  837. if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
  838. ch->ch_mostat &= ~(UART_MCR_RTS);
  839. ch->ch_bd->bd_ops->assert_modem_signals(ch);
  840. }
  841. /*
  842. * If DTR Toggle mode is on, whenever
  843. * the queue and UART is empty, keep DTR low.
  844. */
  845. if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
  846. ch->ch_mostat &= ~(UART_MCR_DTR);
  847. ch->ch_bd->bd_ops->assert_modem_signals(ch);
  848. }
  849. }
  850. }
  851. wake_up_interruptible(&ch->ch_tun.un_flags_wait);
  852. }
  853. if (ch->ch_pun.un_flags & UN_ISOPEN) {
  854. if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  855. ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
  856. spin_unlock_irqrestore(&ch->ch_lock, flags);
  857. ch->ch_pun.un_tty->ldisc->ops->write_wakeup(ch->ch_pun.un_tty);
  858. spin_lock_irqsave(&ch->ch_lock, flags);
  859. }
  860. wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
  861. /*
  862. * If unit is set to wait until empty, check to make sure
  863. * the queue AND FIFO are both empty.
  864. */
  865. if (ch->ch_pun.un_flags & UN_EMPTY) {
  866. if ((qlen == 0) &&
  867. (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
  868. ch->ch_pun.un_flags &= ~(UN_EMPTY);
  869. }
  870. wake_up_interruptible(&ch->ch_pun.un_flags_wait);
  871. }
  872. spin_unlock_irqrestore(&ch->ch_lock, flags);
  873. }
  874. /************************************************************************
  875. *
  876. * TTY Entry points and helper functions
  877. *
  878. ************************************************************************/
  879. /*
  880. * dgnc_tty_open()
  881. *
  882. */
  883. static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
  884. {
  885. struct dgnc_board *brd;
  886. struct channel_t *ch;
  887. struct un_t *un;
  888. uint major = 0;
  889. uint minor = 0;
  890. int rc = 0;
  891. unsigned long flags;
  892. rc = 0;
  893. major = MAJOR(tty_devnum(tty));
  894. minor = MINOR(tty_devnum(tty));
  895. if (major > 255)
  896. return -ENXIO;
  897. /* Get board pointer from our array of majors we have allocated */
  898. brd = dgnc_BoardsByMajor[major];
  899. if (!brd)
  900. return -ENXIO;
  901. /*
  902. * If board is not yet up to a state of READY, go to
  903. * sleep waiting for it to happen or they cancel the open.
  904. */
  905. rc = wait_event_interruptible(brd->state_wait,
  906. (brd->state & BOARD_READY));
  907. if (rc)
  908. return rc;
  909. spin_lock_irqsave(&brd->bd_lock, flags);
  910. /* If opened device is greater than our number of ports, bail. */
  911. if (PORT_NUM(minor) >= brd->nasync) {
  912. spin_unlock_irqrestore(&brd->bd_lock, flags);
  913. return -ENXIO;
  914. }
  915. ch = brd->channels[PORT_NUM(minor)];
  916. if (!ch) {
  917. spin_unlock_irqrestore(&brd->bd_lock, flags);
  918. return -ENXIO;
  919. }
  920. /* Drop board lock */
  921. spin_unlock_irqrestore(&brd->bd_lock, flags);
  922. /* Grab channel lock */
  923. spin_lock_irqsave(&ch->ch_lock, flags);
  924. /* Figure out our type */
  925. if (!IS_PRINT(minor)) {
  926. un = &brd->channels[PORT_NUM(minor)]->ch_tun;
  927. un->un_type = DGNC_SERIAL;
  928. } else if (IS_PRINT(minor)) {
  929. un = &brd->channels[PORT_NUM(minor)]->ch_pun;
  930. un->un_type = DGNC_PRINT;
  931. } else {
  932. spin_unlock_irqrestore(&ch->ch_lock, flags);
  933. return -ENXIO;
  934. }
  935. /*
  936. * If the port is still in a previous open, and in a state
  937. * where we simply cannot safely keep going, wait until the
  938. * state clears.
  939. */
  940. spin_unlock_irqrestore(&ch->ch_lock, flags);
  941. rc = wait_event_interruptible(ch->ch_flags_wait,
  942. ((ch->ch_flags & CH_OPENING) == 0));
  943. /* If ret is non-zero, user ctrl-c'ed us */
  944. if (rc)
  945. return -EINTR;
  946. /*
  947. * If either unit is in the middle of the fragile part of close,
  948. * we just cannot touch the channel safely.
  949. * Go to sleep, knowing that when the channel can be
  950. * touched safely, the close routine will signal the
  951. * ch_flags_wait to wake us back up.
  952. */
  953. rc = wait_event_interruptible(ch->ch_flags_wait,
  954. (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
  955. UN_CLOSING) == 0));
  956. /* If ret is non-zero, user ctrl-c'ed us */
  957. if (rc)
  958. return -EINTR;
  959. spin_lock_irqsave(&ch->ch_lock, flags);
  960. /* Store our unit into driver_data, so we always have it available. */
  961. tty->driver_data = un;
  962. /*
  963. * Initialize tty's
  964. */
  965. if (!(un->un_flags & UN_ISOPEN)) {
  966. /* Store important variables. */
  967. un->un_tty = tty;
  968. /* Maybe do something here to the TTY struct as well? */
  969. }
  970. /*
  971. * Allocate channel buffers for read/write/error.
  972. * Set flag, so we don't get trounced on.
  973. */
  974. ch->ch_flags |= (CH_OPENING);
  975. /* Drop locks, as malloc with GFP_KERNEL can sleep */
  976. spin_unlock_irqrestore(&ch->ch_lock, flags);
  977. if (!ch->ch_rqueue)
  978. ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
  979. if (!ch->ch_equeue)
  980. ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
  981. if (!ch->ch_wqueue)
  982. ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
  983. spin_lock_irqsave(&ch->ch_lock, flags);
  984. ch->ch_flags &= ~(CH_OPENING);
  985. wake_up_interruptible(&ch->ch_flags_wait);
  986. /*
  987. * Initialize if neither terminal or printer is open.
  988. */
  989. if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
  990. /*
  991. * Flush input queues.
  992. */
  993. ch->ch_r_head = 0;
  994. ch->ch_r_tail = 0;
  995. ch->ch_e_head = 0;
  996. ch->ch_e_tail = 0;
  997. ch->ch_w_head = 0;
  998. ch->ch_w_tail = 0;
  999. brd->bd_ops->flush_uart_write(ch);
  1000. brd->bd_ops->flush_uart_read(ch);
  1001. ch->ch_flags = 0;
  1002. ch->ch_cached_lsr = 0;
  1003. ch->ch_stop_sending_break = 0;
  1004. ch->ch_stops_sent = 0;
  1005. ch->ch_c_cflag = tty->termios.c_cflag;
  1006. ch->ch_c_iflag = tty->termios.c_iflag;
  1007. ch->ch_c_oflag = tty->termios.c_oflag;
  1008. ch->ch_c_lflag = tty->termios.c_lflag;
  1009. ch->ch_startc = tty->termios.c_cc[VSTART];
  1010. ch->ch_stopc = tty->termios.c_cc[VSTOP];
  1011. /*
  1012. * Bring up RTS and DTR...
  1013. * Also handle RTS or DTR toggle if set.
  1014. */
  1015. if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
  1016. ch->ch_mostat |= (UART_MCR_RTS);
  1017. if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
  1018. ch->ch_mostat |= (UART_MCR_DTR);
  1019. /* Tell UART to init itself */
  1020. brd->bd_ops->uart_init(ch);
  1021. }
  1022. /*
  1023. * Run param in case we changed anything
  1024. */
  1025. brd->bd_ops->param(tty);
  1026. dgnc_carrier(ch);
  1027. /*
  1028. * follow protocol for opening port
  1029. */
  1030. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1031. rc = dgnc_block_til_ready(tty, file, ch);
  1032. /* No going back now, increment our unit and channel counters */
  1033. spin_lock_irqsave(&ch->ch_lock, flags);
  1034. ch->ch_open_count++;
  1035. un->un_open_count++;
  1036. un->un_flags |= (UN_ISOPEN);
  1037. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1038. return rc;
  1039. }
  1040. /*
  1041. * dgnc_block_til_ready()
  1042. *
  1043. * Wait for DCD, if needed.
  1044. */
  1045. static int dgnc_block_til_ready(struct tty_struct *tty,
  1046. struct file *file,
  1047. struct channel_t *ch)
  1048. {
  1049. int retval = 0;
  1050. struct un_t *un = NULL;
  1051. unsigned long flags;
  1052. uint old_flags = 0;
  1053. int sleep_on_un_flags = 0;
  1054. if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
  1055. ch->magic != DGNC_CHANNEL_MAGIC)
  1056. return -ENXIO;
  1057. un = tty->driver_data;
  1058. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1059. return -ENXIO;
  1060. spin_lock_irqsave(&ch->ch_lock, flags);
  1061. ch->ch_wopen++;
  1062. /* Loop forever */
  1063. while (1) {
  1064. sleep_on_un_flags = 0;
  1065. /*
  1066. * If board has failed somehow during our sleep,
  1067. * bail with error.
  1068. */
  1069. if (ch->ch_bd->state == BOARD_FAILED) {
  1070. retval = -ENXIO;
  1071. break;
  1072. }
  1073. /* If tty was hung up, break out of loop and set error. */
  1074. if (tty_hung_up_p(file)) {
  1075. retval = -EAGAIN;
  1076. break;
  1077. }
  1078. /*
  1079. * If either unit is in the middle of the fragile part of close,
  1080. * we just cannot touch the channel safely.
  1081. * Go back to sleep, knowing that when the channel can be
  1082. * touched safely, the close routine will signal the
  1083. * ch_wait_flags to wake us back up.
  1084. */
  1085. if (!((ch->ch_tun.un_flags |
  1086. ch->ch_pun.un_flags) &
  1087. UN_CLOSING)) {
  1088. /*
  1089. * Our conditions to leave cleanly and happily:
  1090. * 1) NONBLOCKING on the tty is set.
  1091. * 2) CLOCAL is set.
  1092. * 3) DCD (fake or real) is active.
  1093. */
  1094. if (file->f_flags & O_NONBLOCK)
  1095. break;
  1096. if (tty->flags & (1 << TTY_IO_ERROR)) {
  1097. retval = -EIO;
  1098. break;
  1099. }
  1100. if (ch->ch_flags & CH_CD)
  1101. break;
  1102. if (ch->ch_flags & CH_FCAR)
  1103. break;
  1104. } else {
  1105. sleep_on_un_flags = 1;
  1106. }
  1107. /*
  1108. * If there is a signal pending, the user probably
  1109. * interrupted (ctrl-c) us.
  1110. * Leave loop with error set.
  1111. */
  1112. if (signal_pending(current)) {
  1113. retval = -ERESTARTSYS;
  1114. break;
  1115. }
  1116. /*
  1117. * Store the flags before we let go of channel lock
  1118. */
  1119. if (sleep_on_un_flags)
  1120. old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
  1121. else
  1122. old_flags = ch->ch_flags;
  1123. /*
  1124. * Let go of channel lock before calling schedule.
  1125. * Our poller will get any FEP events and wake us up when DCD
  1126. * eventually goes active.
  1127. */
  1128. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1129. /*
  1130. * Wait for something in the flags to change
  1131. * from the current value.
  1132. */
  1133. if (sleep_on_un_flags)
  1134. retval = wait_event_interruptible(un->un_flags_wait,
  1135. (old_flags != (ch->ch_tun.un_flags |
  1136. ch->ch_pun.un_flags)));
  1137. else
  1138. retval = wait_event_interruptible(ch->ch_flags_wait,
  1139. (old_flags != ch->ch_flags));
  1140. /*
  1141. * We got woken up for some reason.
  1142. * Before looping around, grab our channel lock.
  1143. */
  1144. spin_lock_irqsave(&ch->ch_lock, flags);
  1145. }
  1146. ch->ch_wopen--;
  1147. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1148. return retval;
  1149. }
  1150. /*
  1151. * dgnc_tty_hangup()
  1152. *
  1153. * Hangup the port. Like a close, but don't wait for output to drain.
  1154. */
  1155. static void dgnc_tty_hangup(struct tty_struct *tty)
  1156. {
  1157. struct un_t *un;
  1158. if (!tty || tty->magic != TTY_MAGIC)
  1159. return;
  1160. un = tty->driver_data;
  1161. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1162. return;
  1163. /* flush the transmit queues */
  1164. dgnc_tty_flush_buffer(tty);
  1165. }
  1166. /*
  1167. * dgnc_tty_close()
  1168. *
  1169. */
  1170. static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
  1171. {
  1172. struct dgnc_board *bd;
  1173. struct channel_t *ch;
  1174. struct un_t *un;
  1175. unsigned long flags;
  1176. if (!tty || tty->magic != TTY_MAGIC)
  1177. return;
  1178. un = tty->driver_data;
  1179. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1180. return;
  1181. ch = un->un_ch;
  1182. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1183. return;
  1184. bd = ch->ch_bd;
  1185. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1186. return;
  1187. spin_lock_irqsave(&ch->ch_lock, flags);
  1188. /*
  1189. * Determine if this is the last close or not - and if we agree about
  1190. * which type of close it is with the Line Discipline
  1191. */
  1192. if ((tty->count == 1) && (un->un_open_count != 1)) {
  1193. /*
  1194. * Uh, oh. tty->count is 1, which means that the tty
  1195. * structure will be freed. un_open_count should always
  1196. * be one in these conditions. If it's greater than
  1197. * one, we've got real problems, since it means the
  1198. * serial port won't be shutdown.
  1199. */
  1200. dev_dbg(tty->dev,
  1201. "tty->count is 1, un open count is %d\n",
  1202. un->un_open_count);
  1203. un->un_open_count = 1;
  1204. }
  1205. if (un->un_open_count)
  1206. un->un_open_count--;
  1207. else
  1208. dev_dbg(tty->dev,
  1209. "bad serial port open count of %d\n",
  1210. un->un_open_count);
  1211. ch->ch_open_count--;
  1212. if (ch->ch_open_count && un->un_open_count) {
  1213. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1214. return;
  1215. }
  1216. /* OK, its the last close on the unit */
  1217. un->un_flags |= UN_CLOSING;
  1218. tty->closing = 1;
  1219. /*
  1220. * Only officially close channel if count is 0 and
  1221. * DIGI_PRINTER bit is not set.
  1222. */
  1223. if ((ch->ch_open_count == 0) &&
  1224. !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
  1225. ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
  1226. /*
  1227. * turn off print device when closing print device.
  1228. */
  1229. if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
  1230. dgnc_wmove(ch, ch->ch_digi.digi_offstr,
  1231. (int)ch->ch_digi.digi_offlen);
  1232. ch->ch_flags &= ~CH_PRON;
  1233. }
  1234. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1235. /* wait for output to drain */
  1236. /* This will also return if we take an interrupt */
  1237. bd->bd_ops->drain(tty, 0);
  1238. dgnc_tty_flush_buffer(tty);
  1239. tty_ldisc_flush(tty);
  1240. spin_lock_irqsave(&ch->ch_lock, flags);
  1241. tty->closing = 0;
  1242. /*
  1243. * If we have HUPCL set, lower DTR and RTS
  1244. */
  1245. if (ch->ch_c_cflag & HUPCL) {
  1246. /* Drop RTS/DTR */
  1247. ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
  1248. bd->bd_ops->assert_modem_signals(ch);
  1249. /*
  1250. * Go to sleep to ensure RTS/DTR
  1251. * have been dropped for modems to see it.
  1252. */
  1253. if (ch->ch_close_delay) {
  1254. spin_unlock_irqrestore(&ch->ch_lock,
  1255. flags);
  1256. dgnc_ms_sleep(ch->ch_close_delay);
  1257. spin_lock_irqsave(&ch->ch_lock, flags);
  1258. }
  1259. }
  1260. ch->ch_old_baud = 0;
  1261. /* Turn off UART interrupts for this port */
  1262. ch->ch_bd->bd_ops->uart_off(ch);
  1263. } else {
  1264. /*
  1265. * turn off print device when closing print device.
  1266. */
  1267. if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
  1268. dgnc_wmove(ch, ch->ch_digi.digi_offstr,
  1269. (int)ch->ch_digi.digi_offlen);
  1270. ch->ch_flags &= ~CH_PRON;
  1271. }
  1272. }
  1273. un->un_tty = NULL;
  1274. un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
  1275. wake_up_interruptible(&ch->ch_flags_wait);
  1276. wake_up_interruptible(&un->un_flags_wait);
  1277. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1278. }
  1279. /*
  1280. * dgnc_tty_chars_in_buffer()
  1281. *
  1282. * Return number of characters that have not been transmitted yet.
  1283. *
  1284. * This routine is used by the line discipline to determine if there
  1285. * is data waiting to be transmitted/drained/flushed or not.
  1286. */
  1287. static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
  1288. {
  1289. struct channel_t *ch = NULL;
  1290. struct un_t *un = NULL;
  1291. ushort thead;
  1292. ushort ttail;
  1293. uint tmask;
  1294. uint chars = 0;
  1295. unsigned long flags;
  1296. if (!tty)
  1297. return 0;
  1298. un = tty->driver_data;
  1299. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1300. return 0;
  1301. ch = un->un_ch;
  1302. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1303. return 0;
  1304. spin_lock_irqsave(&ch->ch_lock, flags);
  1305. tmask = WQUEUEMASK;
  1306. thead = ch->ch_w_head & tmask;
  1307. ttail = ch->ch_w_tail & tmask;
  1308. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1309. if (ttail == thead) {
  1310. chars = 0;
  1311. } else {
  1312. if (thead >= ttail)
  1313. chars = thead - ttail;
  1314. else
  1315. chars = thead - ttail + WQUEUESIZE;
  1316. }
  1317. return chars;
  1318. }
  1319. /*
  1320. * dgnc_maxcps_room
  1321. *
  1322. * Reduces bytes_available to the max number of characters
  1323. * that can be sent currently given the maxcps value, and
  1324. * returns the new bytes_available. This only affects printer
  1325. * output.
  1326. */
  1327. static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
  1328. {
  1329. struct channel_t *ch = NULL;
  1330. struct un_t *un = NULL;
  1331. if (!tty)
  1332. return bytes_available;
  1333. un = tty->driver_data;
  1334. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1335. return bytes_available;
  1336. ch = un->un_ch;
  1337. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1338. return bytes_available;
  1339. /*
  1340. * If its not the Transparent print device, return
  1341. * the full data amount.
  1342. */
  1343. if (un->un_type != DGNC_PRINT)
  1344. return bytes_available;
  1345. if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
  1346. int cps_limit = 0;
  1347. unsigned long current_time = jiffies;
  1348. unsigned long buffer_time = current_time +
  1349. (HZ * ch->ch_digi.digi_bufsize) /
  1350. ch->ch_digi.digi_maxcps;
  1351. if (ch->ch_cpstime < current_time) {
  1352. /* buffer is empty */
  1353. ch->ch_cpstime = current_time; /* reset ch_cpstime */
  1354. cps_limit = ch->ch_digi.digi_bufsize;
  1355. } else if (ch->ch_cpstime < buffer_time) {
  1356. /* still room in the buffer */
  1357. cps_limit = ((buffer_time - ch->ch_cpstime) *
  1358. ch->ch_digi.digi_maxcps) / HZ;
  1359. } else {
  1360. /* no room in the buffer */
  1361. cps_limit = 0;
  1362. }
  1363. bytes_available = min(cps_limit, bytes_available);
  1364. }
  1365. return bytes_available;
  1366. }
  1367. /*
  1368. * dgnc_tty_write_room()
  1369. *
  1370. * Return space available in Tx buffer
  1371. */
  1372. static int dgnc_tty_write_room(struct tty_struct *tty)
  1373. {
  1374. struct channel_t *ch = NULL;
  1375. struct un_t *un = NULL;
  1376. ushort head;
  1377. ushort tail;
  1378. ushort tmask;
  1379. int ret = 0;
  1380. unsigned long flags;
  1381. if (!tty || !dgnc_TmpWriteBuf)
  1382. return 0;
  1383. un = tty->driver_data;
  1384. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1385. return 0;
  1386. ch = un->un_ch;
  1387. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1388. return 0;
  1389. spin_lock_irqsave(&ch->ch_lock, flags);
  1390. tmask = WQUEUEMASK;
  1391. head = (ch->ch_w_head) & tmask;
  1392. tail = (ch->ch_w_tail) & tmask;
  1393. ret = tail - head - 1;
  1394. if (ret < 0)
  1395. ret += WQUEUESIZE;
  1396. /* Limit printer to maxcps */
  1397. ret = dgnc_maxcps_room(tty, ret);
  1398. /*
  1399. * If we are printer device, leave space for
  1400. * possibly both the on and off strings.
  1401. */
  1402. if (un->un_type == DGNC_PRINT) {
  1403. if (!(ch->ch_flags & CH_PRON))
  1404. ret -= ch->ch_digi.digi_onlen;
  1405. ret -= ch->ch_digi.digi_offlen;
  1406. } else {
  1407. if (ch->ch_flags & CH_PRON)
  1408. ret -= ch->ch_digi.digi_offlen;
  1409. }
  1410. if (ret < 0)
  1411. ret = 0;
  1412. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1413. return ret;
  1414. }
  1415. /*
  1416. * dgnc_tty_put_char()
  1417. *
  1418. * Put a character into ch->ch_buf
  1419. *
  1420. * - used by the line discipline for OPOST processing
  1421. */
  1422. static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
  1423. {
  1424. /*
  1425. * Simply call tty_write.
  1426. */
  1427. dgnc_tty_write(tty, &c, 1);
  1428. return 1;
  1429. }
  1430. /*
  1431. * dgnc_tty_write()
  1432. *
  1433. * Take data from the user or kernel and send it out to the FEP.
  1434. * In here exists all the Transparent Print magic as well.
  1435. */
  1436. static int dgnc_tty_write(struct tty_struct *tty,
  1437. const unsigned char *buf, int count)
  1438. {
  1439. struct channel_t *ch = NULL;
  1440. struct un_t *un = NULL;
  1441. int bufcount = 0, n = 0;
  1442. unsigned long flags;
  1443. ushort head;
  1444. ushort tail;
  1445. ushort tmask;
  1446. uint remain;
  1447. if (!tty || !dgnc_TmpWriteBuf)
  1448. return 0;
  1449. un = tty->driver_data;
  1450. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1451. return 0;
  1452. ch = un->un_ch;
  1453. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1454. return 0;
  1455. if (!count)
  1456. return 0;
  1457. /*
  1458. * Store original amount of characters passed in.
  1459. * This helps to figure out if we should ask the FEP
  1460. * to send us an event when it has more space available.
  1461. */
  1462. spin_lock_irqsave(&ch->ch_lock, flags);
  1463. /* Get our space available for the channel from the board */
  1464. tmask = WQUEUEMASK;
  1465. head = (ch->ch_w_head) & tmask;
  1466. tail = (ch->ch_w_tail) & tmask;
  1467. bufcount = tail - head - 1;
  1468. if (bufcount < 0)
  1469. bufcount += WQUEUESIZE;
  1470. /*
  1471. * Limit printer output to maxcps overall, with bursts allowed
  1472. * up to bufsize characters.
  1473. */
  1474. bufcount = dgnc_maxcps_room(tty, bufcount);
  1475. /*
  1476. * Take minimum of what the user wants to send, and the
  1477. * space available in the FEP buffer.
  1478. */
  1479. count = min(count, bufcount);
  1480. /*
  1481. * Bail if no space left.
  1482. */
  1483. if (count <= 0)
  1484. goto exit_retry;
  1485. /*
  1486. * Output the printer ON string, if we are in terminal mode, but
  1487. * need to be in printer mode.
  1488. */
  1489. if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
  1490. dgnc_wmove(ch, ch->ch_digi.digi_onstr,
  1491. (int)ch->ch_digi.digi_onlen);
  1492. head = (ch->ch_w_head) & tmask;
  1493. ch->ch_flags |= CH_PRON;
  1494. }
  1495. /*
  1496. * On the other hand, output the printer OFF string, if we are
  1497. * currently in printer mode, but need to output to the terminal.
  1498. */
  1499. if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
  1500. dgnc_wmove(ch, ch->ch_digi.digi_offstr,
  1501. (int)ch->ch_digi.digi_offlen);
  1502. head = (ch->ch_w_head) & tmask;
  1503. ch->ch_flags &= ~CH_PRON;
  1504. }
  1505. n = count;
  1506. /*
  1507. * If the write wraps over the top of the circular buffer,
  1508. * move the portion up to the wrap point, and reset the
  1509. * pointers to the bottom.
  1510. */
  1511. remain = WQUEUESIZE - head;
  1512. if (n >= remain) {
  1513. n -= remain;
  1514. memcpy(ch->ch_wqueue + head, buf, remain);
  1515. head = 0;
  1516. buf += remain;
  1517. }
  1518. if (n > 0) {
  1519. /*
  1520. * Move rest of data.
  1521. */
  1522. remain = n;
  1523. memcpy(ch->ch_wqueue + head, buf, remain);
  1524. head += remain;
  1525. }
  1526. if (count) {
  1527. head &= tmask;
  1528. ch->ch_w_head = head;
  1529. }
  1530. /* Update printer buffer empty time. */
  1531. if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
  1532. && (ch->ch_digi.digi_bufsize > 0)) {
  1533. ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
  1534. }
  1535. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1536. if (count) {
  1537. /*
  1538. * Channel lock is grabbed and then released
  1539. * inside this routine.
  1540. */
  1541. ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
  1542. }
  1543. return count;
  1544. exit_retry:
  1545. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1546. return 0;
  1547. }
  1548. /*
  1549. * Return modem signals to ld.
  1550. */
  1551. static int dgnc_tty_tiocmget(struct tty_struct *tty)
  1552. {
  1553. struct channel_t *ch;
  1554. struct un_t *un;
  1555. int result = -EIO;
  1556. unsigned char mstat = 0;
  1557. unsigned long flags;
  1558. if (!tty || tty->magic != TTY_MAGIC)
  1559. return result;
  1560. un = tty->driver_data;
  1561. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1562. return result;
  1563. ch = un->un_ch;
  1564. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1565. return result;
  1566. spin_lock_irqsave(&ch->ch_lock, flags);
  1567. mstat = (ch->ch_mostat | ch->ch_mistat);
  1568. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1569. result = 0;
  1570. if (mstat & UART_MCR_DTR)
  1571. result |= TIOCM_DTR;
  1572. if (mstat & UART_MCR_RTS)
  1573. result |= TIOCM_RTS;
  1574. if (mstat & UART_MSR_CTS)
  1575. result |= TIOCM_CTS;
  1576. if (mstat & UART_MSR_DSR)
  1577. result |= TIOCM_DSR;
  1578. if (mstat & UART_MSR_RI)
  1579. result |= TIOCM_RI;
  1580. if (mstat & UART_MSR_DCD)
  1581. result |= TIOCM_CD;
  1582. return result;
  1583. }
  1584. /*
  1585. * dgnc_tty_tiocmset()
  1586. *
  1587. * Set modem signals, called by ld.
  1588. */
  1589. static int dgnc_tty_tiocmset(struct tty_struct *tty,
  1590. unsigned int set, unsigned int clear)
  1591. {
  1592. struct dgnc_board *bd;
  1593. struct channel_t *ch;
  1594. struct un_t *un;
  1595. int ret = -EIO;
  1596. unsigned long flags;
  1597. if (!tty || tty->magic != TTY_MAGIC)
  1598. return ret;
  1599. un = tty->driver_data;
  1600. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1601. return ret;
  1602. ch = un->un_ch;
  1603. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1604. return ret;
  1605. bd = ch->ch_bd;
  1606. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1607. return ret;
  1608. spin_lock_irqsave(&ch->ch_lock, flags);
  1609. if (set & TIOCM_RTS)
  1610. ch->ch_mostat |= UART_MCR_RTS;
  1611. if (set & TIOCM_DTR)
  1612. ch->ch_mostat |= UART_MCR_DTR;
  1613. if (clear & TIOCM_RTS)
  1614. ch->ch_mostat &= ~(UART_MCR_RTS);
  1615. if (clear & TIOCM_DTR)
  1616. ch->ch_mostat &= ~(UART_MCR_DTR);
  1617. ch->ch_bd->bd_ops->assert_modem_signals(ch);
  1618. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1619. return 0;
  1620. }
  1621. /*
  1622. * dgnc_tty_send_break()
  1623. *
  1624. * Send a Break, called by ld.
  1625. */
  1626. static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
  1627. {
  1628. struct dgnc_board *bd;
  1629. struct channel_t *ch;
  1630. struct un_t *un;
  1631. int ret = -EIO;
  1632. unsigned long flags;
  1633. if (!tty || tty->magic != TTY_MAGIC)
  1634. return ret;
  1635. un = tty->driver_data;
  1636. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1637. return ret;
  1638. ch = un->un_ch;
  1639. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1640. return ret;
  1641. bd = ch->ch_bd;
  1642. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1643. return ret;
  1644. switch (msec) {
  1645. case -1:
  1646. msec = 0xFFFF;
  1647. break;
  1648. case 0:
  1649. msec = 0;
  1650. break;
  1651. default:
  1652. break;
  1653. }
  1654. spin_lock_irqsave(&ch->ch_lock, flags);
  1655. ch->ch_bd->bd_ops->send_break(ch, msec);
  1656. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1657. return 0;
  1658. }
  1659. /*
  1660. * dgnc_tty_wait_until_sent()
  1661. *
  1662. * wait until data has been transmitted, called by ld.
  1663. */
  1664. static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
  1665. {
  1666. struct dgnc_board *bd;
  1667. struct channel_t *ch;
  1668. struct un_t *un;
  1669. if (!tty || tty->magic != TTY_MAGIC)
  1670. return;
  1671. un = tty->driver_data;
  1672. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1673. return;
  1674. ch = un->un_ch;
  1675. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1676. return;
  1677. bd = ch->ch_bd;
  1678. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1679. return;
  1680. bd->bd_ops->drain(tty, 0);
  1681. }
  1682. /*
  1683. * dgnc_send_xchar()
  1684. *
  1685. * send a high priority character, called by ld.
  1686. */
  1687. static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
  1688. {
  1689. struct dgnc_board *bd;
  1690. struct channel_t *ch;
  1691. struct un_t *un;
  1692. unsigned long flags;
  1693. if (!tty || tty->magic != TTY_MAGIC)
  1694. return;
  1695. un = tty->driver_data;
  1696. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1697. return;
  1698. ch = un->un_ch;
  1699. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1700. return;
  1701. bd = ch->ch_bd;
  1702. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1703. return;
  1704. dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
  1705. spin_lock_irqsave(&ch->ch_lock, flags);
  1706. bd->bd_ops->send_immediate_char(ch, c);
  1707. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1708. dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
  1709. }
  1710. /*
  1711. * Return modem signals to ld.
  1712. */
  1713. static inline int dgnc_get_mstat(struct channel_t *ch)
  1714. {
  1715. unsigned char mstat;
  1716. int result = -EIO;
  1717. unsigned long flags;
  1718. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1719. return -ENXIO;
  1720. spin_lock_irqsave(&ch->ch_lock, flags);
  1721. mstat = (ch->ch_mostat | ch->ch_mistat);
  1722. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1723. result = 0;
  1724. if (mstat & UART_MCR_DTR)
  1725. result |= TIOCM_DTR;
  1726. if (mstat & UART_MCR_RTS)
  1727. result |= TIOCM_RTS;
  1728. if (mstat & UART_MSR_CTS)
  1729. result |= TIOCM_CTS;
  1730. if (mstat & UART_MSR_DSR)
  1731. result |= TIOCM_DSR;
  1732. if (mstat & UART_MSR_RI)
  1733. result |= TIOCM_RI;
  1734. if (mstat & UART_MSR_DCD)
  1735. result |= TIOCM_CD;
  1736. return result;
  1737. }
  1738. /*
  1739. * Return modem signals to ld.
  1740. */
  1741. static int dgnc_get_modem_info(struct channel_t *ch,
  1742. unsigned int __user *value)
  1743. {
  1744. int result;
  1745. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1746. return -ENXIO;
  1747. result = dgnc_get_mstat(ch);
  1748. if (result < 0)
  1749. return -ENXIO;
  1750. return put_user(result, value);
  1751. }
  1752. /*
  1753. * dgnc_set_modem_info()
  1754. *
  1755. * Set modem signals, called by ld.
  1756. */
  1757. static int dgnc_set_modem_info(struct tty_struct *tty,
  1758. unsigned int command,
  1759. unsigned int __user *value)
  1760. {
  1761. struct dgnc_board *bd;
  1762. struct channel_t *ch;
  1763. struct un_t *un;
  1764. int ret = -ENXIO;
  1765. unsigned int arg = 0;
  1766. unsigned long flags;
  1767. if (!tty || tty->magic != TTY_MAGIC)
  1768. return ret;
  1769. un = tty->driver_data;
  1770. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1771. return ret;
  1772. ch = un->un_ch;
  1773. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1774. return ret;
  1775. bd = ch->ch_bd;
  1776. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1777. return ret;
  1778. ret = get_user(arg, value);
  1779. if (ret)
  1780. return ret;
  1781. switch (command) {
  1782. case TIOCMBIS:
  1783. if (arg & TIOCM_RTS)
  1784. ch->ch_mostat |= UART_MCR_RTS;
  1785. if (arg & TIOCM_DTR)
  1786. ch->ch_mostat |= UART_MCR_DTR;
  1787. break;
  1788. case TIOCMBIC:
  1789. if (arg & TIOCM_RTS)
  1790. ch->ch_mostat &= ~(UART_MCR_RTS);
  1791. if (arg & TIOCM_DTR)
  1792. ch->ch_mostat &= ~(UART_MCR_DTR);
  1793. break;
  1794. case TIOCMSET:
  1795. if (arg & TIOCM_RTS)
  1796. ch->ch_mostat |= UART_MCR_RTS;
  1797. else
  1798. ch->ch_mostat &= ~(UART_MCR_RTS);
  1799. if (arg & TIOCM_DTR)
  1800. ch->ch_mostat |= UART_MCR_DTR;
  1801. else
  1802. ch->ch_mostat &= ~(UART_MCR_DTR);
  1803. break;
  1804. default:
  1805. return -EINVAL;
  1806. }
  1807. spin_lock_irqsave(&ch->ch_lock, flags);
  1808. ch->ch_bd->bd_ops->assert_modem_signals(ch);
  1809. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1810. return 0;
  1811. }
  1812. /*
  1813. * dgnc_tty_digigeta()
  1814. *
  1815. * Ioctl to get the information for ditty.
  1816. *
  1817. *
  1818. *
  1819. */
  1820. static int dgnc_tty_digigeta(struct tty_struct *tty,
  1821. struct digi_t __user *retinfo)
  1822. {
  1823. struct channel_t *ch;
  1824. struct un_t *un;
  1825. struct digi_t tmp;
  1826. unsigned long flags;
  1827. if (!retinfo)
  1828. return -EFAULT;
  1829. if (!tty || tty->magic != TTY_MAGIC)
  1830. return -EFAULT;
  1831. un = tty->driver_data;
  1832. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1833. return -EFAULT;
  1834. ch = un->un_ch;
  1835. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1836. return -EFAULT;
  1837. memset(&tmp, 0, sizeof(tmp));
  1838. spin_lock_irqsave(&ch->ch_lock, flags);
  1839. memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
  1840. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1841. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1842. return -EFAULT;
  1843. return 0;
  1844. }
  1845. /*
  1846. * dgnc_tty_digiseta()
  1847. *
  1848. * Ioctl to set the information for ditty.
  1849. *
  1850. *
  1851. *
  1852. */
  1853. static int dgnc_tty_digiseta(struct tty_struct *tty,
  1854. struct digi_t __user *new_info)
  1855. {
  1856. struct dgnc_board *bd;
  1857. struct channel_t *ch;
  1858. struct un_t *un;
  1859. struct digi_t new_digi;
  1860. unsigned long flags;
  1861. if (!tty || tty->magic != TTY_MAGIC)
  1862. return -EFAULT;
  1863. un = tty->driver_data;
  1864. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1865. return -EFAULT;
  1866. ch = un->un_ch;
  1867. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1868. return -EFAULT;
  1869. bd = ch->ch_bd;
  1870. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1871. return -EFAULT;
  1872. if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
  1873. return -EFAULT;
  1874. spin_lock_irqsave(&ch->ch_lock, flags);
  1875. /*
  1876. * Handle transistions to and from RTS Toggle.
  1877. */
  1878. if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
  1879. (new_digi.digi_flags & DIGI_RTS_TOGGLE))
  1880. ch->ch_mostat &= ~(UART_MCR_RTS);
  1881. if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
  1882. !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
  1883. ch->ch_mostat |= (UART_MCR_RTS);
  1884. /*
  1885. * Handle transistions to and from DTR Toggle.
  1886. */
  1887. if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
  1888. (new_digi.digi_flags & DIGI_DTR_TOGGLE))
  1889. ch->ch_mostat &= ~(UART_MCR_DTR);
  1890. if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
  1891. !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
  1892. ch->ch_mostat |= (UART_MCR_DTR);
  1893. memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
  1894. if (ch->ch_digi.digi_maxcps < 1)
  1895. ch->ch_digi.digi_maxcps = 1;
  1896. if (ch->ch_digi.digi_maxcps > 10000)
  1897. ch->ch_digi.digi_maxcps = 10000;
  1898. if (ch->ch_digi.digi_bufsize < 10)
  1899. ch->ch_digi.digi_bufsize = 10;
  1900. if (ch->ch_digi.digi_maxchar < 1)
  1901. ch->ch_digi.digi_maxchar = 1;
  1902. if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
  1903. ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
  1904. if (ch->ch_digi.digi_onlen > DIGI_PLEN)
  1905. ch->ch_digi.digi_onlen = DIGI_PLEN;
  1906. if (ch->ch_digi.digi_offlen > DIGI_PLEN)
  1907. ch->ch_digi.digi_offlen = DIGI_PLEN;
  1908. ch->ch_bd->bd_ops->param(tty);
  1909. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1910. return 0;
  1911. }
  1912. /*
  1913. * dgnc_set_termios()
  1914. */
  1915. static void dgnc_tty_set_termios(struct tty_struct *tty,
  1916. struct ktermios *old_termios)
  1917. {
  1918. struct dgnc_board *bd;
  1919. struct channel_t *ch;
  1920. struct un_t *un;
  1921. unsigned long flags;
  1922. if (!tty || tty->magic != TTY_MAGIC)
  1923. return;
  1924. un = tty->driver_data;
  1925. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1926. return;
  1927. ch = un->un_ch;
  1928. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1929. return;
  1930. bd = ch->ch_bd;
  1931. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1932. return;
  1933. spin_lock_irqsave(&ch->ch_lock, flags);
  1934. ch->ch_c_cflag = tty->termios.c_cflag;
  1935. ch->ch_c_iflag = tty->termios.c_iflag;
  1936. ch->ch_c_oflag = tty->termios.c_oflag;
  1937. ch->ch_c_lflag = tty->termios.c_lflag;
  1938. ch->ch_startc = tty->termios.c_cc[VSTART];
  1939. ch->ch_stopc = tty->termios.c_cc[VSTOP];
  1940. ch->ch_bd->bd_ops->param(tty);
  1941. dgnc_carrier(ch);
  1942. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1943. }
  1944. static void dgnc_tty_throttle(struct tty_struct *tty)
  1945. {
  1946. struct channel_t *ch;
  1947. struct un_t *un;
  1948. unsigned long flags;
  1949. if (!tty || tty->magic != TTY_MAGIC)
  1950. return;
  1951. un = tty->driver_data;
  1952. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1953. return;
  1954. ch = un->un_ch;
  1955. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1956. return;
  1957. spin_lock_irqsave(&ch->ch_lock, flags);
  1958. ch->ch_flags |= (CH_FORCED_STOPI);
  1959. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1960. }
  1961. static void dgnc_tty_unthrottle(struct tty_struct *tty)
  1962. {
  1963. struct channel_t *ch;
  1964. struct un_t *un;
  1965. unsigned long flags;
  1966. if (!tty || tty->magic != TTY_MAGIC)
  1967. return;
  1968. un = tty->driver_data;
  1969. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1970. return;
  1971. ch = un->un_ch;
  1972. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1973. return;
  1974. spin_lock_irqsave(&ch->ch_lock, flags);
  1975. ch->ch_flags &= ~(CH_FORCED_STOPI);
  1976. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1977. }
  1978. static void dgnc_tty_start(struct tty_struct *tty)
  1979. {
  1980. struct dgnc_board *bd;
  1981. struct channel_t *ch;
  1982. struct un_t *un;
  1983. unsigned long flags;
  1984. if (!tty || tty->magic != TTY_MAGIC)
  1985. return;
  1986. un = tty->driver_data;
  1987. if (!un || un->magic != DGNC_UNIT_MAGIC)
  1988. return;
  1989. ch = un->un_ch;
  1990. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  1991. return;
  1992. bd = ch->ch_bd;
  1993. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  1994. return;
  1995. spin_lock_irqsave(&ch->ch_lock, flags);
  1996. ch->ch_flags &= ~(CH_FORCED_STOP);
  1997. spin_unlock_irqrestore(&ch->ch_lock, flags);
  1998. }
  1999. static void dgnc_tty_stop(struct tty_struct *tty)
  2000. {
  2001. struct dgnc_board *bd;
  2002. struct channel_t *ch;
  2003. struct un_t *un;
  2004. unsigned long flags;
  2005. if (!tty || tty->magic != TTY_MAGIC)
  2006. return;
  2007. un = tty->driver_data;
  2008. if (!un || un->magic != DGNC_UNIT_MAGIC)
  2009. return;
  2010. ch = un->un_ch;
  2011. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  2012. return;
  2013. bd = ch->ch_bd;
  2014. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  2015. return;
  2016. spin_lock_irqsave(&ch->ch_lock, flags);
  2017. ch->ch_flags |= (CH_FORCED_STOP);
  2018. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2019. }
  2020. /*
  2021. * dgnc_tty_flush_chars()
  2022. *
  2023. * Flush the cook buffer
  2024. *
  2025. * Note to self, and any other poor souls who venture here:
  2026. *
  2027. * flush in this case DOES NOT mean dispose of the data.
  2028. * instead, it means "stop buffering and send it if you
  2029. * haven't already." Just guess how I figured that out... SRW 2-Jun-98
  2030. *
  2031. * It is also always called in interrupt context - JAR 8-Sept-99
  2032. */
  2033. static void dgnc_tty_flush_chars(struct tty_struct *tty)
  2034. {
  2035. struct dgnc_board *bd;
  2036. struct channel_t *ch;
  2037. struct un_t *un;
  2038. unsigned long flags;
  2039. if (!tty || tty->magic != TTY_MAGIC)
  2040. return;
  2041. un = tty->driver_data;
  2042. if (!un || un->magic != DGNC_UNIT_MAGIC)
  2043. return;
  2044. ch = un->un_ch;
  2045. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  2046. return;
  2047. bd = ch->ch_bd;
  2048. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  2049. return;
  2050. spin_lock_irqsave(&ch->ch_lock, flags);
  2051. /* Do something maybe here */
  2052. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2053. }
  2054. /*
  2055. * dgnc_tty_flush_buffer()
  2056. *
  2057. * Flush Tx buffer (make in == out)
  2058. */
  2059. static void dgnc_tty_flush_buffer(struct tty_struct *tty)
  2060. {
  2061. struct channel_t *ch;
  2062. struct un_t *un;
  2063. unsigned long flags;
  2064. if (!tty || tty->magic != TTY_MAGIC)
  2065. return;
  2066. un = tty->driver_data;
  2067. if (!un || un->magic != DGNC_UNIT_MAGIC)
  2068. return;
  2069. ch = un->un_ch;
  2070. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  2071. return;
  2072. spin_lock_irqsave(&ch->ch_lock, flags);
  2073. ch->ch_flags &= ~CH_STOP;
  2074. /* Flush our write queue */
  2075. ch->ch_w_head = ch->ch_w_tail;
  2076. /* Flush UARTs transmit FIFO */
  2077. ch->ch_bd->bd_ops->flush_uart_write(ch);
  2078. if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
  2079. ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
  2080. wake_up_interruptible(&ch->ch_tun.un_flags_wait);
  2081. }
  2082. if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
  2083. ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
  2084. wake_up_interruptible(&ch->ch_pun.un_flags_wait);
  2085. }
  2086. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2087. }
  2088. /*****************************************************************************
  2089. *
  2090. * The IOCTL function and all of its helpers
  2091. *
  2092. *****************************************************************************/
  2093. /*
  2094. * dgnc_tty_ioctl()
  2095. *
  2096. * The usual assortment of ioctl's
  2097. */
  2098. static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  2099. unsigned long arg)
  2100. {
  2101. struct dgnc_board *bd;
  2102. struct channel_t *ch;
  2103. struct un_t *un;
  2104. int rc;
  2105. unsigned long flags;
  2106. void __user *uarg = (void __user *)arg;
  2107. if (!tty || tty->magic != TTY_MAGIC)
  2108. return -ENODEV;
  2109. un = tty->driver_data;
  2110. if (!un || un->magic != DGNC_UNIT_MAGIC)
  2111. return -ENODEV;
  2112. ch = un->un_ch;
  2113. if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
  2114. return -ENODEV;
  2115. bd = ch->ch_bd;
  2116. if (!bd || bd->magic != DGNC_BOARD_MAGIC)
  2117. return -ENODEV;
  2118. spin_lock_irqsave(&ch->ch_lock, flags);
  2119. if (un->un_open_count <= 0) {
  2120. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2121. return -EIO;
  2122. }
  2123. switch (cmd) {
  2124. /* Here are all the standard ioctl's that we MUST implement */
  2125. case TCSBRK:
  2126. /*
  2127. * TCSBRK is SVID version: non-zero arg --> no break
  2128. * this behaviour is exploited by tcdrain().
  2129. *
  2130. * According to POSIX.1 spec (7.2.2.1.2) breaks should be
  2131. * between 0.25 and 0.5 seconds so we'll ask for something
  2132. * in the middle: 0.375 seconds.
  2133. */
  2134. rc = tty_check_change(tty);
  2135. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2136. if (rc)
  2137. return rc;
  2138. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2139. if (rc)
  2140. return -EINTR;
  2141. spin_lock_irqsave(&ch->ch_lock, flags);
  2142. if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
  2143. ch->ch_bd->bd_ops->send_break(ch, 250);
  2144. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2145. return 0;
  2146. case TCSBRKP:
  2147. /* support for POSIX tcsendbreak()
  2148. * According to POSIX.1 spec (7.2.2.1.2) breaks should be
  2149. * between 0.25 and 0.5 seconds so we'll ask for something
  2150. * in the middle: 0.375 seconds.
  2151. */
  2152. rc = tty_check_change(tty);
  2153. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2154. if (rc)
  2155. return rc;
  2156. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2157. if (rc)
  2158. return -EINTR;
  2159. spin_lock_irqsave(&ch->ch_lock, flags);
  2160. ch->ch_bd->bd_ops->send_break(ch, 250);
  2161. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2162. return 0;
  2163. case TIOCSBRK:
  2164. rc = tty_check_change(tty);
  2165. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2166. if (rc)
  2167. return rc;
  2168. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2169. if (rc)
  2170. return -EINTR;
  2171. spin_lock_irqsave(&ch->ch_lock, flags);
  2172. ch->ch_bd->bd_ops->send_break(ch, 250);
  2173. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2174. return 0;
  2175. case TIOCCBRK:
  2176. /* Do Nothing */
  2177. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2178. return 0;
  2179. case TIOCGSOFTCAR:
  2180. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2181. rc = put_user(C_CLOCAL(tty) ? 1 : 0,
  2182. (unsigned long __user *)arg);
  2183. return rc;
  2184. case TIOCSSOFTCAR:
  2185. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2186. rc = get_user(arg, (unsigned long __user *)arg);
  2187. if (rc)
  2188. return rc;
  2189. spin_lock_irqsave(&ch->ch_lock, flags);
  2190. tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
  2191. (arg ? CLOCAL : 0));
  2192. ch->ch_bd->bd_ops->param(tty);
  2193. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2194. return 0;
  2195. case TIOCMGET:
  2196. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2197. return dgnc_get_modem_info(ch, uarg);
  2198. case TIOCMBIS:
  2199. case TIOCMBIC:
  2200. case TIOCMSET:
  2201. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2202. return dgnc_set_modem_info(tty, cmd, uarg);
  2203. /*
  2204. * Here are any additional ioctl's that we want to implement
  2205. */
  2206. case TCFLSH:
  2207. /*
  2208. * The linux tty driver doesn't have a flush
  2209. * input routine for the driver, assuming all backed
  2210. * up data is in the line disc. buffers. However,
  2211. * we all know that's not the case. Here, we
  2212. * act on the ioctl, but then lie and say we didn't
  2213. * so the line discipline will process the flush
  2214. * also.
  2215. */
  2216. rc = tty_check_change(tty);
  2217. if (rc) {
  2218. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2219. return rc;
  2220. }
  2221. if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
  2222. ch->ch_r_head = ch->ch_r_tail;
  2223. ch->ch_bd->bd_ops->flush_uart_read(ch);
  2224. /* Force queue flow control to be released, if needed */
  2225. dgnc_check_queue_flow_control(ch);
  2226. }
  2227. if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
  2228. if (!(un->un_type == DGNC_PRINT)) {
  2229. ch->ch_w_head = ch->ch_w_tail;
  2230. ch->ch_bd->bd_ops->flush_uart_write(ch);
  2231. if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
  2232. ch->ch_tun.un_flags &=
  2233. ~(UN_LOW|UN_EMPTY);
  2234. wake_up_interruptible(&ch->ch_tun.un_flags_wait);
  2235. }
  2236. if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
  2237. ch->ch_pun.un_flags &=
  2238. ~(UN_LOW|UN_EMPTY);
  2239. wake_up_interruptible(&ch->ch_pun.un_flags_wait);
  2240. }
  2241. }
  2242. }
  2243. /* pretend we didn't recognize this IOCTL */
  2244. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2245. return -ENOIOCTLCMD;
  2246. case TCSETSF:
  2247. case TCSETSW:
  2248. /*
  2249. * The linux tty driver doesn't have a flush
  2250. * input routine for the driver, assuming all backed
  2251. * up data is in the line disc. buffers. However,
  2252. * we all know that's not the case. Here, we
  2253. * act on the ioctl, but then lie and say we didn't
  2254. * so the line discipline will process the flush
  2255. * also.
  2256. */
  2257. if (cmd == TCSETSF) {
  2258. /* flush rx */
  2259. ch->ch_flags &= ~CH_STOP;
  2260. ch->ch_r_head = ch->ch_r_tail;
  2261. ch->ch_bd->bd_ops->flush_uart_read(ch);
  2262. /* Force queue flow control to be released, if needed */
  2263. dgnc_check_queue_flow_control(ch);
  2264. }
  2265. /* now wait for all the output to drain */
  2266. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2267. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2268. if (rc)
  2269. return -EINTR;
  2270. /* pretend we didn't recognize this */
  2271. return -ENOIOCTLCMD;
  2272. case TCSETAW:
  2273. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2274. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2275. if (rc)
  2276. return -EINTR;
  2277. /* pretend we didn't recognize this */
  2278. return -ENOIOCTLCMD;
  2279. case TCXONC:
  2280. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2281. /* Make the ld do it */
  2282. return -ENOIOCTLCMD;
  2283. case DIGI_GETA:
  2284. /* get information for ditty */
  2285. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2286. return dgnc_tty_digigeta(tty, uarg);
  2287. case DIGI_SETAW:
  2288. case DIGI_SETAF:
  2289. /* set information for ditty */
  2290. if (cmd == (DIGI_SETAW)) {
  2291. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2292. rc = ch->ch_bd->bd_ops->drain(tty, 0);
  2293. if (rc)
  2294. return -EINTR;
  2295. spin_lock_irqsave(&ch->ch_lock, flags);
  2296. } else {
  2297. tty_ldisc_flush(tty);
  2298. }
  2299. /* fall thru */
  2300. case DIGI_SETA:
  2301. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2302. return dgnc_tty_digiseta(tty, uarg);
  2303. case DIGI_LOOPBACK:
  2304. {
  2305. uint loopback = 0;
  2306. /* Let go of locks when accessing user space,
  2307. * could sleep
  2308. */
  2309. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2310. rc = get_user(loopback, (unsigned int __user *)arg);
  2311. if (rc)
  2312. return rc;
  2313. spin_lock_irqsave(&ch->ch_lock, flags);
  2314. /* Enable/disable internal loopback for this port */
  2315. if (loopback)
  2316. ch->ch_flags |= CH_LOOPBACK;
  2317. else
  2318. ch->ch_flags &= ~(CH_LOOPBACK);
  2319. ch->ch_bd->bd_ops->param(tty);
  2320. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2321. return 0;
  2322. }
  2323. case DIGI_GETCUSTOMBAUD:
  2324. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2325. rc = put_user(ch->ch_custom_speed, (unsigned int __user *)arg);
  2326. return rc;
  2327. case DIGI_SETCUSTOMBAUD:
  2328. {
  2329. int new_rate;
  2330. /* Let go of locks when accessing user space, could sleep */
  2331. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2332. rc = get_user(new_rate, (int __user *)arg);
  2333. if (rc)
  2334. return rc;
  2335. spin_lock_irqsave(&ch->ch_lock, flags);
  2336. dgnc_set_custom_speed(ch, new_rate);
  2337. ch->ch_bd->bd_ops->param(tty);
  2338. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2339. return 0;
  2340. }
  2341. /*
  2342. * This ioctl allows insertion of a character into the front
  2343. * of any pending data to be transmitted.
  2344. *
  2345. * This ioctl is to satify the "Send Character Immediate"
  2346. * call that the RealPort protocol spec requires.
  2347. */
  2348. case DIGI_REALPORT_SENDIMMEDIATE:
  2349. {
  2350. unsigned char c;
  2351. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2352. rc = get_user(c, (unsigned char __user *)arg);
  2353. if (rc)
  2354. return rc;
  2355. spin_lock_irqsave(&ch->ch_lock, flags);
  2356. ch->ch_bd->bd_ops->send_immediate_char(ch, c);
  2357. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2358. return 0;
  2359. }
  2360. /*
  2361. * This ioctl returns all the current counts for the port.
  2362. *
  2363. * This ioctl is to satify the "Line Error Counters"
  2364. * call that the RealPort protocol spec requires.
  2365. */
  2366. case DIGI_REALPORT_GETCOUNTERS:
  2367. {
  2368. struct digi_getcounter buf;
  2369. buf.norun = ch->ch_err_overrun;
  2370. buf.noflow = 0; /* The driver doesn't keep this stat */
  2371. buf.nframe = ch->ch_err_frame;
  2372. buf.nparity = ch->ch_err_parity;
  2373. buf.nbreak = ch->ch_err_break;
  2374. buf.rbytes = ch->ch_rxcount;
  2375. buf.tbytes = ch->ch_txcount;
  2376. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2377. if (copy_to_user(uarg, &buf, sizeof(buf)))
  2378. return -EFAULT;
  2379. return 0;
  2380. }
  2381. /*
  2382. * This ioctl returns all current events.
  2383. *
  2384. * This ioctl is to satify the "Event Reporting"
  2385. * call that the RealPort protocol spec requires.
  2386. */
  2387. case DIGI_REALPORT_GETEVENTS:
  2388. {
  2389. unsigned int events = 0;
  2390. /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
  2391. if (ch->ch_flags & CH_BREAK_SENDING)
  2392. events |= EV_TXB;
  2393. if ((ch->ch_flags & CH_STOP) ||
  2394. (ch->ch_flags & CH_FORCED_STOP))
  2395. events |= (EV_OPU | EV_OPS);
  2396. if ((ch->ch_flags & CH_STOPI) ||
  2397. (ch->ch_flags & CH_FORCED_STOPI))
  2398. events |= (EV_IPU | EV_IPS);
  2399. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2400. rc = put_user(events, (unsigned int __user *)arg);
  2401. return rc;
  2402. }
  2403. /*
  2404. * This ioctl returns TOUT and TIN counters based
  2405. * upon the values passed in by the RealPort Server.
  2406. * It also passes back whether the UART Transmitter is
  2407. * empty as well.
  2408. */
  2409. case DIGI_REALPORT_GETBUFFERS:
  2410. {
  2411. struct digi_getbuffer buf;
  2412. int tdist;
  2413. int count;
  2414. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2415. /*
  2416. * Get data from user first.
  2417. */
  2418. if (copy_from_user(&buf, uarg, sizeof(buf)))
  2419. return -EFAULT;
  2420. spin_lock_irqsave(&ch->ch_lock, flags);
  2421. /*
  2422. * Figure out how much data is in our RX and TX queues.
  2423. */
  2424. buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
  2425. buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
  2426. /*
  2427. * Is the UART empty? Add that value to whats in our TX queue.
  2428. */
  2429. count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
  2430. /*
  2431. * Figure out how much data the RealPort Server believes should
  2432. * be in our TX queue.
  2433. */
  2434. tdist = (buf.tIn - buf.tOut) & 0xffff;
  2435. /*
  2436. * If we have more data than the RealPort Server believes we
  2437. * should have, reduce our count to its amount.
  2438. *
  2439. * This count difference CAN happen because the Linux LD can
  2440. * insert more characters into our queue for OPOST processing
  2441. * that the RealPort Server doesn't know about.
  2442. */
  2443. if (buf.txbuf > tdist)
  2444. buf.txbuf = tdist;
  2445. /*
  2446. * Report whether our queue and UART TX are completely empty.
  2447. */
  2448. if (count)
  2449. buf.txdone = 0;
  2450. else
  2451. buf.txdone = 1;
  2452. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2453. if (copy_to_user(uarg, &buf, sizeof(buf)))
  2454. return -EFAULT;
  2455. return 0;
  2456. }
  2457. default:
  2458. spin_unlock_irqrestore(&ch->ch_lock, flags);
  2459. return -ENOIOCTLCMD;
  2460. }
  2461. }