mos7840.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Clean ups from Moschip version and a few ioctl implementations by:
  17. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/serial.h>
  35. #include <asm/uaccess.h>
  36. /*
  37. * Version Information
  38. */
  39. #define DRIVER_VERSION "1.3.1"
  40. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  41. /*
  42. * 16C50 UART register defines
  43. */
  44. #define LCR_BITS_5 0x00 /* 5 bits/char */
  45. #define LCR_BITS_6 0x01 /* 6 bits/char */
  46. #define LCR_BITS_7 0x02 /* 7 bits/char */
  47. #define LCR_BITS_8 0x03 /* 8 bits/char */
  48. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  49. #define LCR_STOP_1 0x00 /* 1 stop bit */
  50. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  51. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  52. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  53. #define LCR_PAR_NONE 0x00 /* No parity */
  54. #define LCR_PAR_ODD 0x08 /* Odd parity */
  55. #define LCR_PAR_EVEN 0x18 /* Even parity */
  56. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  57. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  58. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  59. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  60. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  61. #define MCR_DTR 0x01 /* Assert DTR */
  62. #define MCR_RTS 0x02 /* Assert RTS */
  63. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  64. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  65. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  66. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  67. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  68. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  69. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  70. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  71. /*
  72. * Defines used for sending commands to port
  73. */
  74. #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
  75. #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
  76. #define MOS_PORT1 0x0200
  77. #define MOS_PORT2 0x0300
  78. #define MOS_VENREG 0x0000
  79. #define MOS_MAX_PORT 0x02
  80. #define MOS_WRITE 0x0E
  81. #define MOS_READ 0x0D
  82. /* Requests */
  83. #define MCS_RD_RTYPE 0xC0
  84. #define MCS_WR_RTYPE 0x40
  85. #define MCS_RDREQ 0x0D
  86. #define MCS_WRREQ 0x0E
  87. #define MCS_CTRL_TIMEOUT 500
  88. #define VENDOR_READ_LENGTH (0x01)
  89. #define MAX_NAME_LEN 64
  90. #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
  91. #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
  92. /* For higher baud Rates use TIOCEXBAUD */
  93. #define TIOCEXBAUD 0x5462
  94. /* vendor id and device id defines */
  95. #define USB_VENDOR_ID_MOSCHIP 0x9710
  96. #define MOSCHIP_DEVICE_ID_7840 0x7840
  97. #define MOSCHIP_DEVICE_ID_7820 0x7820
  98. /* Interrupt Rotinue Defines */
  99. #define SERIAL_IIR_RLS 0x06
  100. #define SERIAL_IIR_MS 0x00
  101. /*
  102. * Emulation of the bit mask on the LINE STATUS REGISTER.
  103. */
  104. #define SERIAL_LSR_DR 0x0001
  105. #define SERIAL_LSR_OE 0x0002
  106. #define SERIAL_LSR_PE 0x0004
  107. #define SERIAL_LSR_FE 0x0008
  108. #define SERIAL_LSR_BI 0x0010
  109. #define MOS_MSR_DELTA_CTS 0x10
  110. #define MOS_MSR_DELTA_DSR 0x20
  111. #define MOS_MSR_DELTA_RI 0x40
  112. #define MOS_MSR_DELTA_CD 0x80
  113. // Serial Port register Address
  114. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  115. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  116. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  117. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  118. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  119. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  120. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  121. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  122. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  123. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  124. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  125. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  126. /*
  127. * URB POOL related defines
  128. */
  129. #define NUM_URBS 16 /* URB Count */
  130. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  131. static struct usb_device_id moschip_port_id_table[] = {
  132. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  133. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  134. {} /* terminating entry */
  135. };
  136. static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
  137. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  138. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  139. {} /* terminating entry */
  140. };
  141. MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
  142. /* This structure holds all of the local port information */
  143. struct moschip_port {
  144. int port_num; /*Actual port number in the device(1,2,etc) */
  145. struct urb *write_urb; /* write URB for this port */
  146. struct urb *read_urb; /* read URB for this port */
  147. __u8 shadowLCR; /* last LCR value received */
  148. __u8 shadowMCR; /* last MCR value received */
  149. char open;
  150. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  151. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  152. int delta_msr_cond;
  153. struct async_icount icount;
  154. struct usb_serial_port *port; /* loop back to the owner of this object */
  155. /*Offsets */
  156. __u8 SpRegOffset;
  157. __u8 ControlRegOffset;
  158. __u8 DcrRegOffset;
  159. //for processing control URBS in interrupt context
  160. struct urb *control_urb;
  161. char *ctrl_buf;
  162. int MsrLsr;
  163. struct urb *write_urb_pool[NUM_URBS];
  164. };
  165. static int debug;
  166. static int mos7840_num_ports; //this says the number of ports in the device
  167. static int mos7840_num_open_ports;
  168. /*
  169. * mos7840_set_reg_sync
  170. * To set the Control register by calling usb_fill_control_urb function
  171. * by passing usb_sndctrlpipe function as parameter.
  172. */
  173. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  174. __u16 val)
  175. {
  176. struct usb_device *dev = port->serial->dev;
  177. val = val & 0x00ff;
  178. dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  179. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  180. MCS_WR_RTYPE, val, reg, NULL, 0,
  181. MOS_WDR_TIMEOUT);
  182. }
  183. /*
  184. * mos7840_get_reg_sync
  185. * To set the Uart register by calling usb_fill_control_urb function by
  186. * passing usb_rcvctrlpipe function as parameter.
  187. */
  188. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  189. __u16 * val)
  190. {
  191. struct usb_device *dev = port->serial->dev;
  192. int ret = 0;
  193. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  194. MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
  195. MOS_WDR_TIMEOUT);
  196. dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
  197. *val = (*val) & 0x00ff;
  198. return ret;
  199. }
  200. /*
  201. * mos7840_set_uart_reg
  202. * To set the Uart register by calling usb_fill_control_urb function by
  203. * passing usb_sndctrlpipe function as parameter.
  204. */
  205. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  206. __u16 val)
  207. {
  208. struct usb_device *dev = port->serial->dev;
  209. val = val & 0x00ff;
  210. // For the UART control registers, the application number need to be Or'ed
  211. if (mos7840_num_ports == 4) {
  212. val |=
  213. (((__u16) port->number - (__u16) (port->serial->minor)) +
  214. 1) << 8;
  215. dbg("mos7840_set_uart_reg application number is %x\n", val);
  216. } else {
  217. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  218. val |=
  219. (((__u16) port->number -
  220. (__u16) (port->serial->minor)) + 1) << 8;
  221. dbg("mos7840_set_uart_reg application number is %x\n",
  222. val);
  223. } else {
  224. val |=
  225. (((__u16) port->number -
  226. (__u16) (port->serial->minor)) + 2) << 8;
  227. dbg("mos7840_set_uart_reg application number is %x\n",
  228. val);
  229. }
  230. }
  231. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  232. MCS_WR_RTYPE, val, reg, NULL, 0,
  233. MOS_WDR_TIMEOUT);
  234. }
  235. /*
  236. * mos7840_get_uart_reg
  237. * To set the Control register by calling usb_fill_control_urb function
  238. * by passing usb_rcvctrlpipe function as parameter.
  239. */
  240. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  241. __u16 * val)
  242. {
  243. struct usb_device *dev = port->serial->dev;
  244. int ret = 0;
  245. __u16 Wval;
  246. //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
  247. /*Wval is same as application number */
  248. if (mos7840_num_ports == 4) {
  249. Wval =
  250. (((__u16) port->number - (__u16) (port->serial->minor)) +
  251. 1) << 8;
  252. dbg("mos7840_get_uart_reg application number is %x\n", Wval);
  253. } else {
  254. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  255. Wval =
  256. (((__u16) port->number -
  257. (__u16) (port->serial->minor)) + 1) << 8;
  258. dbg("mos7840_get_uart_reg application number is %x\n",
  259. Wval);
  260. } else {
  261. Wval =
  262. (((__u16) port->number -
  263. (__u16) (port->serial->minor)) + 2) << 8;
  264. dbg("mos7840_get_uart_reg application number is %x\n",
  265. Wval);
  266. }
  267. }
  268. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  269. MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
  270. MOS_WDR_TIMEOUT);
  271. *val = (*val) & 0x00ff;
  272. return ret;
  273. }
  274. static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
  275. {
  276. dbg("***************************************\n");
  277. dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  278. dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
  279. dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
  280. dbg("***************************************\n");
  281. }
  282. /************************************************************************/
  283. /************************************************************************/
  284. /* I N T E R F A C E F U N C T I O N S */
  285. /* I N T E R F A C E F U N C T I O N S */
  286. /************************************************************************/
  287. /************************************************************************/
  288. static inline void mos7840_set_port_private(struct usb_serial_port *port,
  289. struct moschip_port *data)
  290. {
  291. usb_set_serial_port_data(port, (void *)data);
  292. }
  293. static inline struct moschip_port *mos7840_get_port_private(struct
  294. usb_serial_port
  295. *port)
  296. {
  297. return (struct moschip_port *)usb_get_serial_port_data(port);
  298. }
  299. static int mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
  300. {
  301. struct moschip_port *mos7840_port;
  302. struct async_icount *icount;
  303. mos7840_port = port;
  304. icount = &mos7840_port->icount;
  305. if (new_msr &
  306. (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
  307. MOS_MSR_DELTA_CD)) {
  308. icount = &mos7840_port->icount;
  309. /* update input line counters */
  310. if (new_msr & MOS_MSR_DELTA_CTS) {
  311. icount->cts++;
  312. }
  313. if (new_msr & MOS_MSR_DELTA_DSR) {
  314. icount->dsr++;
  315. }
  316. if (new_msr & MOS_MSR_DELTA_CD) {
  317. icount->dcd++;
  318. }
  319. if (new_msr & MOS_MSR_DELTA_RI) {
  320. icount->rng++;
  321. }
  322. }
  323. return 0;
  324. }
  325. static int mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
  326. {
  327. struct async_icount *icount;
  328. dbg("%s - %02x", __FUNCTION__, new_lsr);
  329. if (new_lsr & SERIAL_LSR_BI) {
  330. //
  331. // Parity and Framing errors only count if they
  332. // occur exclusive of a break being
  333. // received.
  334. //
  335. new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
  336. }
  337. /* update input line counters */
  338. icount = &port->icount;
  339. if (new_lsr & SERIAL_LSR_BI) {
  340. icount->brk++;
  341. }
  342. if (new_lsr & SERIAL_LSR_OE) {
  343. icount->overrun++;
  344. }
  345. if (new_lsr & SERIAL_LSR_PE) {
  346. icount->parity++;
  347. }
  348. if (new_lsr & SERIAL_LSR_FE) {
  349. icount->frame++;
  350. }
  351. return 0;
  352. }
  353. /************************************************************************/
  354. /************************************************************************/
  355. /* U S B C A L L B A C K F U N C T I O N S */
  356. /* U S B C A L L B A C K F U N C T I O N S */
  357. /************************************************************************/
  358. /************************************************************************/
  359. static void mos7840_control_callback(struct urb *urb)
  360. {
  361. unsigned char *data;
  362. struct moschip_port *mos7840_port;
  363. __u8 regval = 0x0;
  364. if (!urb) {
  365. dbg("%s", "Invalid Pointer !!!!:\n");
  366. return;
  367. }
  368. switch (urb->status) {
  369. case 0:
  370. /* success */
  371. break;
  372. case -ECONNRESET:
  373. case -ENOENT:
  374. case -ESHUTDOWN:
  375. /* this urb is terminated, clean up */
  376. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  377. urb->status);
  378. return;
  379. default:
  380. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  381. urb->status);
  382. goto exit;
  383. }
  384. mos7840_port = (struct moschip_port *)urb->context;
  385. dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
  386. dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
  387. mos7840_port->MsrLsr, mos7840_port->port_num);
  388. data = urb->transfer_buffer;
  389. regval = (__u8) data[0];
  390. dbg("%s data is %x\n", __FUNCTION__, regval);
  391. if (mos7840_port->MsrLsr == 0)
  392. mos7840_handle_new_msr(mos7840_port, regval);
  393. else if (mos7840_port->MsrLsr == 1)
  394. mos7840_handle_new_lsr(mos7840_port, regval);
  395. exit:
  396. return;
  397. }
  398. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  399. __u16 * val)
  400. {
  401. struct usb_device *dev = mcs->port->serial->dev;
  402. struct usb_ctrlrequest *dr = NULL;
  403. unsigned char *buffer = NULL;
  404. int ret = 0;
  405. buffer = (__u8 *) mcs->ctrl_buf;
  406. // dr=(struct usb_ctrlrequest *)(buffer);
  407. dr = (void *)(buffer + 2);
  408. dr->bRequestType = MCS_RD_RTYPE;
  409. dr->bRequest = MCS_RDREQ;
  410. dr->wValue = cpu_to_le16(Wval); //0;
  411. dr->wIndex = cpu_to_le16(reg);
  412. dr->wLength = cpu_to_le16(2);
  413. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  414. (unsigned char *)dr, buffer, 2,
  415. mos7840_control_callback, mcs);
  416. mcs->control_urb->transfer_buffer_length = 2;
  417. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  418. return ret;
  419. }
  420. /*****************************************************************************
  421. * mos7840_interrupt_callback
  422. * this is the callback function for when we have received data on the
  423. * interrupt endpoint.
  424. *****************************************************************************/
  425. static void mos7840_interrupt_callback(struct urb *urb)
  426. {
  427. int result;
  428. int length;
  429. struct moschip_port *mos7840_port;
  430. struct usb_serial *serial;
  431. __u16 Data;
  432. unsigned char *data;
  433. __u8 sp[5], st;
  434. int i;
  435. __u16 wval;
  436. dbg("%s", " : Entering\n");
  437. if (!urb) {
  438. dbg("%s", "Invalid Pointer !!!!:\n");
  439. return;
  440. }
  441. switch (urb->status) {
  442. case 0:
  443. /* success */
  444. break;
  445. case -ECONNRESET:
  446. case -ENOENT:
  447. case -ESHUTDOWN:
  448. /* this urb is terminated, clean up */
  449. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  450. urb->status);
  451. return;
  452. default:
  453. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  454. urb->status);
  455. goto exit;
  456. }
  457. length = urb->actual_length;
  458. data = urb->transfer_buffer;
  459. serial = (struct usb_serial *)urb->context;
  460. /* Moschip get 5 bytes
  461. * Byte 1 IIR Port 1 (port.number is 0)
  462. * Byte 2 IIR Port 2 (port.number is 1)
  463. * Byte 3 IIR Port 3 (port.number is 2)
  464. * Byte 4 IIR Port 4 (port.number is 3)
  465. * Byte 5 FIFO status for both */
  466. if (length && length > 5) {
  467. dbg("%s \n", "Wrong data !!!");
  468. return;
  469. }
  470. sp[0] = (__u8) data[0];
  471. sp[1] = (__u8) data[1];
  472. sp[2] = (__u8) data[2];
  473. sp[3] = (__u8) data[3];
  474. st = (__u8) data[4];
  475. for (i = 0; i < serial->num_ports; i++) {
  476. mos7840_port = mos7840_get_port_private(serial->port[i]);
  477. wval =
  478. (((__u16) serial->port[i]->number -
  479. (__u16) (serial->minor)) + 1) << 8;
  480. if (mos7840_port->open) {
  481. if (sp[i] & 0x01) {
  482. dbg("SP%d No Interrupt !!!\n", i);
  483. } else {
  484. switch (sp[i] & 0x0f) {
  485. case SERIAL_IIR_RLS:
  486. dbg("Serial Port %d: Receiver status error or ", i);
  487. dbg("address bit detected in 9-bit mode\n");
  488. mos7840_port->MsrLsr = 1;
  489. mos7840_get_reg(mos7840_port, wval,
  490. LINE_STATUS_REGISTER,
  491. &Data);
  492. break;
  493. case SERIAL_IIR_MS:
  494. dbg("Serial Port %d: Modem status change\n", i);
  495. mos7840_port->MsrLsr = 0;
  496. mos7840_get_reg(mos7840_port, wval,
  497. MODEM_STATUS_REGISTER,
  498. &Data);
  499. break;
  500. }
  501. }
  502. }
  503. }
  504. exit:
  505. result = usb_submit_urb(urb, GFP_ATOMIC);
  506. if (result) {
  507. dev_err(&urb->dev->dev,
  508. "%s - Error %d submitting interrupt urb\n",
  509. __FUNCTION__, result);
  510. }
  511. return;
  512. }
  513. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  514. const char *function)
  515. {
  516. if (!port) {
  517. dbg("%s - port == NULL", function);
  518. return -1;
  519. }
  520. if (!port->serial) {
  521. dbg("%s - port->serial == NULL", function);
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. /* Inline functions to check the sanity of a pointer that is passed to us */
  527. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  528. const char *function)
  529. {
  530. if (!serial) {
  531. dbg("%s - serial == NULL", function);
  532. return -1;
  533. }
  534. if (!serial->type) {
  535. dbg("%s - serial->type == NULL!", function);
  536. return -1;
  537. }
  538. return 0;
  539. }
  540. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  541. const char *function)
  542. {
  543. /* if no port was specified, or it fails a paranoia check */
  544. if (!port ||
  545. mos7840_port_paranoia_check(port, function) ||
  546. mos7840_serial_paranoia_check(port->serial, function)) {
  547. /* then say that we don't have a valid usb_serial thing, which will * end up genrating -ENODEV return values */
  548. return NULL;
  549. }
  550. return port->serial;
  551. }
  552. /*****************************************************************************
  553. * mos7840_bulk_in_callback
  554. * this is the callback function for when we have received data on the
  555. * bulk in endpoint.
  556. *****************************************************************************/
  557. static void mos7840_bulk_in_callback(struct urb *urb)
  558. {
  559. int status;
  560. unsigned char *data;
  561. struct usb_serial *serial;
  562. struct usb_serial_port *port;
  563. struct moschip_port *mos7840_port;
  564. struct tty_struct *tty;
  565. if (!urb) {
  566. dbg("%s", "Invalid Pointer !!!!:\n");
  567. return;
  568. }
  569. if (urb->status) {
  570. dbg("nonzero read bulk status received: %d", urb->status);
  571. return;
  572. }
  573. mos7840_port = (struct moschip_port *)urb->context;
  574. if (!mos7840_port) {
  575. dbg("%s", "NULL mos7840_port pointer \n");
  576. return;
  577. }
  578. port = (struct usb_serial_port *)mos7840_port->port;
  579. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  580. dbg("%s", "Port Paranoia failed \n");
  581. return;
  582. }
  583. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  584. if (!serial) {
  585. dbg("%s\n", "Bad serial pointer ");
  586. return;
  587. }
  588. dbg("%s\n", "Entering... \n");
  589. data = urb->transfer_buffer;
  590. dbg("%s", "Entering ........... \n");
  591. if (urb->actual_length) {
  592. tty = mos7840_port->port->tty;
  593. if (tty) {
  594. tty_buffer_request_room(tty, urb->actual_length);
  595. tty_insert_flip_string(tty, data, urb->actual_length);
  596. dbg(" %s \n", data);
  597. tty_flip_buffer_push(tty);
  598. }
  599. mos7840_port->icount.rx += urb->actual_length;
  600. dbg("mos7840_port->icount.rx is %d:\n",
  601. mos7840_port->icount.rx);
  602. }
  603. if (!mos7840_port->read_urb) {
  604. dbg("%s", "URB KILLED !!!\n");
  605. return;
  606. }
  607. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  608. mos7840_port->read_urb->dev = serial->dev;
  609. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  610. if (status) {
  611. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  612. status);
  613. }
  614. }
  615. }
  616. /*****************************************************************************
  617. * mos7840_bulk_out_data_callback
  618. * this is the callback function for when we have finished sending serial data
  619. * on the bulk out endpoint.
  620. *****************************************************************************/
  621. static void mos7840_bulk_out_data_callback(struct urb *urb)
  622. {
  623. struct moschip_port *mos7840_port;
  624. struct tty_struct *tty;
  625. if (!urb) {
  626. dbg("%s", "Invalid Pointer !!!!:\n");
  627. return;
  628. }
  629. if (urb->status) {
  630. dbg("nonzero write bulk status received:%d\n", urb->status);
  631. return;
  632. }
  633. mos7840_port = (struct moschip_port *)urb->context;
  634. if (!mos7840_port) {
  635. dbg("%s", "NULL mos7840_port pointer \n");
  636. return;
  637. }
  638. if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
  639. dbg("%s", "Port Paranoia failed \n");
  640. return;
  641. }
  642. dbg("%s \n", "Entering .........");
  643. tty = mos7840_port->port->tty;
  644. if (tty && mos7840_port->open) {
  645. /* let the tty driver wakeup if it has a special *
  646. * write_wakeup function */
  647. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
  648. && tty->ldisc.write_wakeup) {
  649. (tty->ldisc.write_wakeup) (tty);
  650. }
  651. /* tell the tty driver that something has changed */
  652. wake_up_interruptible(&tty->write_wait);
  653. }
  654. }
  655. /************************************************************************/
  656. /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
  657. /************************************************************************/
  658. #ifdef MCSSerialProbe
  659. static int mos7840_serial_probe(struct usb_serial *serial,
  660. const struct usb_device_id *id)
  661. {
  662. /*need to implement the mode_reg reading and updating\
  663. structures usb_serial_ device_type\
  664. (i.e num_ports, num_bulkin,bulkout etc) */
  665. /* Also we can update the changes attach */
  666. return 1;
  667. }
  668. #endif
  669. /*****************************************************************************
  670. * mos7840_open
  671. * this function is called by the tty driver when a port is opened
  672. * If successful, we return 0
  673. * Otherwise we return a negative error number.
  674. *****************************************************************************/
  675. static int mos7840_open(struct usb_serial_port *port, struct file *filp)
  676. {
  677. int response;
  678. int j;
  679. struct usb_serial *serial;
  680. struct urb *urb;
  681. __u16 Data;
  682. int status;
  683. struct moschip_port *mos7840_port;
  684. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  685. dbg("%s", "Port Paranoia failed \n");
  686. return -ENODEV;
  687. }
  688. mos7840_num_open_ports++;
  689. serial = port->serial;
  690. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  691. dbg("%s", "Serial Paranoia failed \n");
  692. return -ENODEV;
  693. }
  694. mos7840_port = mos7840_get_port_private(port);
  695. if (mos7840_port == NULL)
  696. return -ENODEV;
  697. usb_clear_halt(serial->dev, port->write_urb->pipe);
  698. usb_clear_halt(serial->dev, port->read_urb->pipe);
  699. /* Initialising the write urb pool */
  700. for (j = 0; j < NUM_URBS; ++j) {
  701. urb = usb_alloc_urb(0, SLAB_ATOMIC);
  702. mos7840_port->write_urb_pool[j] = urb;
  703. if (urb == NULL) {
  704. err("No more urbs???");
  705. continue;
  706. }
  707. urb->transfer_buffer = NULL;
  708. urb->transfer_buffer =
  709. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  710. if (!urb->transfer_buffer) {
  711. err("%s-out of memory for urb buffers.", __FUNCTION__);
  712. continue;
  713. }
  714. }
  715. /*****************************************************************************
  716. * Initialize MCS7840 -- Write Init values to corresponding Registers
  717. *
  718. * Register Index
  719. * 1 : IER
  720. * 2 : FCR
  721. * 3 : LCR
  722. * 4 : MCR
  723. *
  724. * 0x08 : SP1/2 Control Reg
  725. *****************************************************************************/
  726. //NEED to check the following Block
  727. status = 0;
  728. Data = 0x0;
  729. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  730. if (status < 0) {
  731. dbg("Reading Spreg failed\n");
  732. return -1;
  733. }
  734. Data |= 0x80;
  735. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  736. if (status < 0) {
  737. dbg("writing Spreg failed\n");
  738. return -1;
  739. }
  740. Data &= ~0x80;
  741. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  742. if (status < 0) {
  743. dbg("writing Spreg failed\n");
  744. return -1;
  745. }
  746. //End of block to be checked
  747. status = 0;
  748. Data = 0x0;
  749. status =
  750. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  751. if (status < 0) {
  752. dbg("Reading Controlreg failed\n");
  753. return -1;
  754. }
  755. Data |= 0x08; //Driver done bit
  756. Data |= 0x20; //rx_disable
  757. status = 0;
  758. status =
  759. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  760. if (status < 0) {
  761. dbg("writing Controlreg failed\n");
  762. return -1;
  763. }
  764. //do register settings here
  765. // Set all regs to the device default values.
  766. ////////////////////////////////////
  767. // First Disable all interrupts.
  768. ////////////////////////////////////
  769. Data = 0x00;
  770. status = 0;
  771. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  772. if (status < 0) {
  773. dbg("disableing interrupts failed\n");
  774. return -1;
  775. }
  776. // Set FIFO_CONTROL_REGISTER to the default value
  777. Data = 0x00;
  778. status = 0;
  779. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  780. if (status < 0) {
  781. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  782. return -1;
  783. }
  784. Data = 0xcf;
  785. status = 0;
  786. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  787. if (status < 0) {
  788. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  789. return -1;
  790. }
  791. Data = 0x03;
  792. status = 0;
  793. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  794. mos7840_port->shadowLCR = Data;
  795. Data = 0x0b;
  796. status = 0;
  797. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  798. mos7840_port->shadowMCR = Data;
  799. Data = 0x00;
  800. status = 0;
  801. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  802. mos7840_port->shadowLCR = Data;
  803. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  804. status = 0;
  805. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  806. Data = 0x0c;
  807. status = 0;
  808. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  809. Data = 0x0;
  810. status = 0;
  811. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  812. Data = 0x00;
  813. status = 0;
  814. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  815. Data = Data & ~SERIAL_LCR_DLAB;
  816. status = 0;
  817. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  818. mos7840_port->shadowLCR = Data;
  819. //clearing Bulkin and Bulkout Fifo
  820. Data = 0x0;
  821. status = 0;
  822. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  823. Data = Data | 0x0c;
  824. status = 0;
  825. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  826. Data = Data & ~0x0c;
  827. status = 0;
  828. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  829. //Finally enable all interrupts
  830. Data = 0x0;
  831. Data = 0x0c;
  832. status = 0;
  833. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  834. //clearing rx_disable
  835. Data = 0x0;
  836. status = 0;
  837. status =
  838. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  839. Data = Data & ~0x20;
  840. status = 0;
  841. status =
  842. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  843. // rx_negate
  844. Data = 0x0;
  845. status = 0;
  846. status =
  847. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  848. Data = Data | 0x10;
  849. status = 0;
  850. status =
  851. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  852. /* force low_latency on so that our tty_push actually forces *
  853. * the data through,otherwise it is scheduled, and with *
  854. * high data rates (like with OHCI) data can get lost. */
  855. if (port->tty)
  856. port->tty->low_latency = 1;
  857. /* Check to see if we've set up our endpoint info yet *
  858. * (can't set it up in mos7840_startup as the structures *
  859. * were not set up at that time.) */
  860. if (mos7840_num_open_ports == 1) {
  861. if (serial->port[0]->interrupt_in_buffer == NULL) {
  862. /* set up interrupt urb */
  863. usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
  864. serial->dev,
  865. usb_rcvintpipe(serial->dev,
  866. serial->port[0]->
  867. interrupt_in_endpointAddress),
  868. serial->port[0]->interrupt_in_buffer,
  869. serial->port[0]->interrupt_in_urb->
  870. transfer_buffer_length,
  871. mos7840_interrupt_callback,
  872. serial,
  873. serial->port[0]->interrupt_in_urb->
  874. interval);
  875. /* start interrupt read for mos7840 *
  876. * will continue as long as mos7840 is connected */
  877. response =
  878. usb_submit_urb(serial->port[0]->interrupt_in_urb,
  879. GFP_KERNEL);
  880. if (response) {
  881. err("%s - Error %d submitting interrupt urb",
  882. __FUNCTION__, response);
  883. }
  884. }
  885. }
  886. /* see if we've set up our endpoint info yet *
  887. * (can't set it up in mos7840_startup as the *
  888. * structures were not set up at that time.) */
  889. dbg("port number is %d \n", port->number);
  890. dbg("serial number is %d \n", port->serial->minor);
  891. dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
  892. dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
  893. dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
  894. dbg("port's number in the device is %d\n", mos7840_port->port_num);
  895. mos7840_port->read_urb = port->read_urb;
  896. /* set up our bulk in urb */
  897. usb_fill_bulk_urb(mos7840_port->read_urb,
  898. serial->dev,
  899. usb_rcvbulkpipe(serial->dev,
  900. port->bulk_in_endpointAddress),
  901. port->bulk_in_buffer,
  902. mos7840_port->read_urb->transfer_buffer_length,
  903. mos7840_bulk_in_callback, mos7840_port);
  904. dbg("mos7840_open: bulkin endpoint is %d\n",
  905. port->bulk_in_endpointAddress);
  906. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  907. if (response) {
  908. err("%s - Error %d submitting control urb", __FUNCTION__,
  909. response);
  910. }
  911. /* initialize our wait queues */
  912. init_waitqueue_head(&mos7840_port->wait_chase);
  913. init_waitqueue_head(&mos7840_port->delta_msr_wait);
  914. /* initialize our icount structure */
  915. memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
  916. /* initialize our port settings */
  917. mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
  918. /* send a open port command */
  919. mos7840_port->open = 1;
  920. //mos7840_change_port_settings(mos7840_port,old_termios);
  921. mos7840_port->icount.tx = 0;
  922. mos7840_port->icount.rx = 0;
  923. dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
  924. return 0;
  925. }
  926. /*****************************************************************************
  927. * mos7840_chars_in_buffer
  928. * this function is called by the tty driver when it wants to know how many
  929. * bytes of data we currently have outstanding in the port (data that has
  930. * been written, but hasn't made it out the port yet)
  931. * If successful, we return the number of bytes left to be written in the
  932. * system,
  933. * Otherwise we return a negative error number.
  934. *****************************************************************************/
  935. static int mos7840_chars_in_buffer(struct usb_serial_port *port)
  936. {
  937. int i;
  938. int chars = 0;
  939. struct moschip_port *mos7840_port;
  940. dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
  941. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  942. dbg("%s", "Invalid port \n");
  943. return -1;
  944. }
  945. mos7840_port = mos7840_get_port_private(port);
  946. if (mos7840_port == NULL) {
  947. dbg("%s \n", "mos7840_break:leaving ...........");
  948. return -1;
  949. }
  950. for (i = 0; i < NUM_URBS; ++i) {
  951. if (mos7840_port->write_urb_pool[i]->status == -EINPROGRESS) {
  952. chars += URB_TRANSFER_BUFFER_SIZE;
  953. }
  954. }
  955. dbg("%s - returns %d", __FUNCTION__, chars);
  956. return (chars);
  957. }
  958. /************************************************************************
  959. *
  960. * mos7840_block_until_tx_empty
  961. *
  962. * This function will block the close until one of the following:
  963. * 1. TX count are 0
  964. * 2. The mos7840 has stopped
  965. * 3. A timout of 3 seconds without activity has expired
  966. *
  967. ************************************************************************/
  968. static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
  969. {
  970. int timeout = HZ / 10;
  971. int wait = 30;
  972. int count;
  973. while (1) {
  974. count = mos7840_chars_in_buffer(mos7840_port->port);
  975. /* Check for Buffer status */
  976. if (count <= 0) {
  977. return;
  978. }
  979. /* Block the thread for a while */
  980. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  981. timeout);
  982. /* No activity.. count down section */
  983. wait--;
  984. if (wait == 0) {
  985. dbg("%s - TIMEOUT", __FUNCTION__);
  986. return;
  987. } else {
  988. /* Reset timout value back to seconds */
  989. wait = 30;
  990. }
  991. }
  992. }
  993. /*****************************************************************************
  994. * mos7840_close
  995. * this function is called by the tty driver when a port is closed
  996. *****************************************************************************/
  997. static void mos7840_close(struct usb_serial_port *port, struct file *filp)
  998. {
  999. struct usb_serial *serial;
  1000. struct moschip_port *mos7840_port;
  1001. int j;
  1002. __u16 Data;
  1003. dbg("%s\n", "mos7840_close:entering...");
  1004. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1005. dbg("%s", "Port Paranoia failed \n");
  1006. return;
  1007. }
  1008. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  1009. if (!serial) {
  1010. dbg("%s", "Serial Paranoia failed \n");
  1011. return;
  1012. }
  1013. mos7840_port = mos7840_get_port_private(port);
  1014. if (mos7840_port == NULL) {
  1015. return;
  1016. }
  1017. for (j = 0; j < NUM_URBS; ++j)
  1018. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  1019. /* Freeing Write URBs */
  1020. for (j = 0; j < NUM_URBS; ++j) {
  1021. if (mos7840_port->write_urb_pool[j]) {
  1022. if (mos7840_port->write_urb_pool[j]->transfer_buffer)
  1023. kfree(mos7840_port->write_urb_pool[j]->
  1024. transfer_buffer);
  1025. usb_free_urb(mos7840_port->write_urb_pool[j]);
  1026. }
  1027. }
  1028. if (serial->dev) {
  1029. /* flush and block until tx is empty */
  1030. mos7840_block_until_tx_empty(mos7840_port);
  1031. }
  1032. /* While closing port, shutdown all bulk read, write *
  1033. * and interrupt read if they exists */
  1034. if (serial->dev) {
  1035. if (mos7840_port->write_urb) {
  1036. dbg("%s", "Shutdown bulk write\n");
  1037. usb_kill_urb(mos7840_port->write_urb);
  1038. }
  1039. if (mos7840_port->read_urb) {
  1040. dbg("%s", "Shutdown bulk read\n");
  1041. usb_kill_urb(mos7840_port->read_urb);
  1042. }
  1043. if ((&mos7840_port->control_urb)) {
  1044. dbg("%s", "Shutdown control read\n");
  1045. // usb_kill_urb (mos7840_port->control_urb);
  1046. }
  1047. }
  1048. // if(mos7840_port->ctrl_buf != NULL)
  1049. // kfree(mos7840_port->ctrl_buf);
  1050. mos7840_num_open_ports--;
  1051. dbg("mos7840_num_open_ports in close%d:in port%d\n",
  1052. mos7840_num_open_ports, port->number);
  1053. if (mos7840_num_open_ports == 0) {
  1054. if (serial->port[0]->interrupt_in_urb) {
  1055. dbg("%s", "Shutdown interrupt_in_urb\n");
  1056. }
  1057. }
  1058. if (mos7840_port->write_urb) {
  1059. /* if this urb had a transfer buffer already (old tx) free it */
  1060. if (mos7840_port->write_urb->transfer_buffer != NULL) {
  1061. kfree(mos7840_port->write_urb->transfer_buffer);
  1062. }
  1063. usb_free_urb(mos7840_port->write_urb);
  1064. }
  1065. Data = 0x0;
  1066. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1067. Data = 0x00;
  1068. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1069. mos7840_port->open = 0;
  1070. dbg("%s \n", "Leaving ............");
  1071. }
  1072. /************************************************************************
  1073. *
  1074. * mos7840_block_until_chase_response
  1075. *
  1076. * This function will block the close until one of the following:
  1077. * 1. Response to our Chase comes from mos7840
  1078. * 2. A timout of 10 seconds without activity has expired
  1079. * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
  1080. *
  1081. ************************************************************************/
  1082. static void mos7840_block_until_chase_response(struct moschip_port
  1083. *mos7840_port)
  1084. {
  1085. int timeout = 1 * HZ;
  1086. int wait = 10;
  1087. int count;
  1088. while (1) {
  1089. count = mos7840_chars_in_buffer(mos7840_port->port);
  1090. /* Check for Buffer status */
  1091. if (count <= 0) {
  1092. return;
  1093. }
  1094. /* Block the thread for a while */
  1095. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  1096. timeout);
  1097. /* No activity.. count down section */
  1098. wait--;
  1099. if (wait == 0) {
  1100. dbg("%s - TIMEOUT", __FUNCTION__);
  1101. return;
  1102. } else {
  1103. /* Reset timout value back to seconds */
  1104. wait = 10;
  1105. }
  1106. }
  1107. }
  1108. /*****************************************************************************
  1109. * mos7840_break
  1110. * this function sends a break to the port
  1111. *****************************************************************************/
  1112. static void mos7840_break(struct usb_serial_port *port, int break_state)
  1113. {
  1114. unsigned char data;
  1115. struct usb_serial *serial;
  1116. struct moschip_port *mos7840_port;
  1117. dbg("%s \n", "Entering ...........");
  1118. dbg("mos7840_break: Start\n");
  1119. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1120. dbg("%s", "Port Paranoia failed \n");
  1121. return;
  1122. }
  1123. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  1124. if (!serial) {
  1125. dbg("%s", "Serial Paranoia failed \n");
  1126. return;
  1127. }
  1128. mos7840_port = mos7840_get_port_private(port);
  1129. if (mos7840_port == NULL) {
  1130. return;
  1131. }
  1132. if (serial->dev) {
  1133. /* flush and block until tx is empty */
  1134. mos7840_block_until_chase_response(mos7840_port);
  1135. }
  1136. if (break_state == -1) {
  1137. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  1138. } else {
  1139. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  1140. }
  1141. mos7840_port->shadowLCR = data;
  1142. dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
  1143. mos7840_port->shadowLCR);
  1144. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  1145. mos7840_port->shadowLCR);
  1146. return;
  1147. }
  1148. /*****************************************************************************
  1149. * mos7840_write_room
  1150. * this function is called by the tty driver when it wants to know how many
  1151. * bytes of data we can accept for a specific port.
  1152. * If successful, we return the amount of room that we have for this port
  1153. * Otherwise we return a negative error number.
  1154. *****************************************************************************/
  1155. static int mos7840_write_room(struct usb_serial_port *port)
  1156. {
  1157. int i;
  1158. int room = 0;
  1159. struct moschip_port *mos7840_port;
  1160. dbg("%s \n", " mos7840_write_room:entering ...........");
  1161. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1162. dbg("%s", "Invalid port \n");
  1163. dbg("%s \n", " mos7840_write_room:leaving ...........");
  1164. return -1;
  1165. }
  1166. mos7840_port = mos7840_get_port_private(port);
  1167. if (mos7840_port == NULL) {
  1168. dbg("%s \n", "mos7840_break:leaving ...........");
  1169. return -1;
  1170. }
  1171. for (i = 0; i < NUM_URBS; ++i) {
  1172. if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1173. room += URB_TRANSFER_BUFFER_SIZE;
  1174. }
  1175. }
  1176. dbg("%s - returns %d", __FUNCTION__, room);
  1177. return (room);
  1178. }
  1179. /*****************************************************************************
  1180. * mos7840_write
  1181. * this function is called by the tty driver when data should be written to
  1182. * the port.
  1183. * If successful, we return the number of bytes written, otherwise we
  1184. * return a negative error number.
  1185. *****************************************************************************/
  1186. static int mos7840_write(struct usb_serial_port *port,
  1187. const unsigned char *data, int count)
  1188. {
  1189. int status;
  1190. int i;
  1191. int bytes_sent = 0;
  1192. int transfer_size;
  1193. struct moschip_port *mos7840_port;
  1194. struct usb_serial *serial;
  1195. struct urb *urb;
  1196. //__u16 Data;
  1197. const unsigned char *current_position = data;
  1198. unsigned char *data1;
  1199. dbg("%s \n", "entering ...........");
  1200. //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
  1201. #ifdef NOTMOS7840
  1202. Data = 0x00;
  1203. status = 0;
  1204. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  1205. mos7840_port->shadowLCR = Data;
  1206. dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
  1207. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1208. mos7840_port->shadowLCR);
  1209. //Data = 0x03;
  1210. //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
  1211. //mos7840_port->shadowLCR=Data;//Need to add later
  1212. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  1213. status = 0;
  1214. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1215. //Data = 0x0c;
  1216. //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
  1217. Data = 0x00;
  1218. status = 0;
  1219. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
  1220. dbg("mos7840_write:DLL value is %x\n", Data);
  1221. Data = 0x0;
  1222. status = 0;
  1223. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
  1224. dbg("mos7840_write:DLM value is %x\n", Data);
  1225. Data = Data & ~SERIAL_LCR_DLAB;
  1226. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1227. mos7840_port->shadowLCR);
  1228. status = 0;
  1229. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1230. #endif
  1231. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1232. dbg("%s", "Port Paranoia failed \n");
  1233. return -1;
  1234. }
  1235. serial = port->serial;
  1236. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  1237. dbg("%s", "Serial Paranoia failed \n");
  1238. return -1;
  1239. }
  1240. mos7840_port = mos7840_get_port_private(port);
  1241. if (mos7840_port == NULL) {
  1242. dbg("%s", "mos7840_port is NULL\n");
  1243. return -1;
  1244. }
  1245. /* try to find a free urb in the list */
  1246. urb = NULL;
  1247. for (i = 0; i < NUM_URBS; ++i) {
  1248. if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1249. urb = mos7840_port->write_urb_pool[i];
  1250. dbg("\nURB:%d", i);
  1251. break;
  1252. }
  1253. }
  1254. if (urb == NULL) {
  1255. dbg("%s - no more free urbs", __FUNCTION__);
  1256. goto exit;
  1257. }
  1258. if (urb->transfer_buffer == NULL) {
  1259. urb->transfer_buffer =
  1260. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  1261. if (urb->transfer_buffer == NULL) {
  1262. err("%s no more kernel memory...", __FUNCTION__);
  1263. goto exit;
  1264. }
  1265. }
  1266. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1267. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1268. /* fill urb with data and submit */
  1269. usb_fill_bulk_urb(urb,
  1270. serial->dev,
  1271. usb_sndbulkpipe(serial->dev,
  1272. port->bulk_out_endpointAddress),
  1273. urb->transfer_buffer,
  1274. transfer_size,
  1275. mos7840_bulk_out_data_callback, mos7840_port);
  1276. data1 = urb->transfer_buffer;
  1277. dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
  1278. /* send it down the pipe */
  1279. status = usb_submit_urb(urb, GFP_ATOMIC);
  1280. if (status) {
  1281. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  1282. __FUNCTION__, status);
  1283. bytes_sent = status;
  1284. goto exit;
  1285. }
  1286. bytes_sent = transfer_size;
  1287. mos7840_port->icount.tx += transfer_size;
  1288. dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
  1289. exit:
  1290. return bytes_sent;
  1291. }
  1292. /*****************************************************************************
  1293. * mos7840_throttle
  1294. * this function is called by the tty driver when it wants to stop the data
  1295. * being read from the port.
  1296. *****************************************************************************/
  1297. static void mos7840_throttle(struct usb_serial_port *port)
  1298. {
  1299. struct moschip_port *mos7840_port;
  1300. struct tty_struct *tty;
  1301. int status;
  1302. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1303. dbg("%s", "Invalid port \n");
  1304. return;
  1305. }
  1306. dbg("- port %d\n", port->number);
  1307. mos7840_port = mos7840_get_port_private(port);
  1308. if (mos7840_port == NULL)
  1309. return;
  1310. if (!mos7840_port->open) {
  1311. dbg("%s\n", "port not opened");
  1312. return;
  1313. }
  1314. dbg("%s", "Entering .......... \n");
  1315. tty = port->tty;
  1316. if (!tty) {
  1317. dbg("%s - no tty available", __FUNCTION__);
  1318. return;
  1319. }
  1320. /* if we are implementing XON/XOFF, send the stop character */
  1321. if (I_IXOFF(tty)) {
  1322. unsigned char stop_char = STOP_CHAR(tty);
  1323. status = mos7840_write(port, &stop_char, 1);
  1324. if (status <= 0) {
  1325. return;
  1326. }
  1327. }
  1328. /* if we are implementing RTS/CTS, toggle that line */
  1329. if (tty->termios->c_cflag & CRTSCTS) {
  1330. mos7840_port->shadowMCR &= ~MCR_RTS;
  1331. status = 0;
  1332. status =
  1333. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1334. mos7840_port->shadowMCR);
  1335. if (status < 0) {
  1336. return;
  1337. }
  1338. }
  1339. return;
  1340. }
  1341. /*****************************************************************************
  1342. * mos7840_unthrottle
  1343. * this function is called by the tty driver when it wants to resume the data
  1344. * being read from the port (called after SerialThrottle is called)
  1345. *****************************************************************************/
  1346. static void mos7840_unthrottle(struct usb_serial_port *port)
  1347. {
  1348. struct tty_struct *tty;
  1349. int status;
  1350. struct moschip_port *mos7840_port = mos7840_get_port_private(port);
  1351. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1352. dbg("%s", "Invalid port \n");
  1353. return;
  1354. }
  1355. if (mos7840_port == NULL)
  1356. return;
  1357. if (!mos7840_port->open) {
  1358. dbg("%s - port not opened", __FUNCTION__);
  1359. return;
  1360. }
  1361. dbg("%s", "Entering .......... \n");
  1362. tty = port->tty;
  1363. if (!tty) {
  1364. dbg("%s - no tty available", __FUNCTION__);
  1365. return;
  1366. }
  1367. /* if we are implementing XON/XOFF, send the start character */
  1368. if (I_IXOFF(tty)) {
  1369. unsigned char start_char = START_CHAR(tty);
  1370. status = mos7840_write(port, &start_char, 1);
  1371. if (status <= 0) {
  1372. return;
  1373. }
  1374. }
  1375. /* if we are implementing RTS/CTS, toggle that line */
  1376. if (tty->termios->c_cflag & CRTSCTS) {
  1377. mos7840_port->shadowMCR |= MCR_RTS;
  1378. status = 0;
  1379. status =
  1380. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1381. mos7840_port->shadowMCR);
  1382. if (status < 0) {
  1383. return;
  1384. }
  1385. }
  1386. return;
  1387. }
  1388. static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
  1389. {
  1390. struct moschip_port *mos7840_port;
  1391. unsigned int result;
  1392. __u16 msr;
  1393. __u16 mcr;
  1394. int status = 0;
  1395. mos7840_port = mos7840_get_port_private(port);
  1396. dbg("%s - port %d", __FUNCTION__, port->number);
  1397. if (mos7840_port == NULL)
  1398. return -ENODEV;
  1399. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  1400. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1401. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  1402. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  1403. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  1404. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  1405. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  1406. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  1407. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  1408. dbg("%s - 0x%04X", __FUNCTION__, result);
  1409. return result;
  1410. }
  1411. static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
  1412. unsigned int set, unsigned int clear)
  1413. {
  1414. struct moschip_port *mos7840_port;
  1415. unsigned int mcr;
  1416. unsigned int status;
  1417. dbg("%s - port %d", __FUNCTION__, port->number);
  1418. mos7840_port = mos7840_get_port_private(port);
  1419. if (mos7840_port == NULL)
  1420. return -ENODEV;
  1421. mcr = mos7840_port->shadowMCR;
  1422. if (clear & TIOCM_RTS)
  1423. mcr &= ~MCR_RTS;
  1424. if (clear & TIOCM_DTR)
  1425. mcr &= ~MCR_DTR;
  1426. if (clear & TIOCM_LOOP)
  1427. mcr &= ~MCR_LOOPBACK;
  1428. if (set & TIOCM_RTS)
  1429. mcr |= MCR_RTS;
  1430. if (set & TIOCM_DTR)
  1431. mcr |= MCR_DTR;
  1432. if (set & TIOCM_LOOP)
  1433. mcr |= MCR_LOOPBACK;
  1434. mos7840_port->shadowMCR = mcr;
  1435. status = 0;
  1436. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  1437. if (status < 0) {
  1438. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1439. return -1;
  1440. }
  1441. return 0;
  1442. }
  1443. /*****************************************************************************
  1444. * mos7840_calc_baud_rate_divisor
  1445. * this function calculates the proper baud rate divisor for the specified
  1446. * baud rate.
  1447. *****************************************************************************/
  1448. static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
  1449. __u16 * clk_sel_val)
  1450. {
  1451. dbg("%s - %d", __FUNCTION__, baudRate);
  1452. if (baudRate <= 115200) {
  1453. *divisor = 115200 / baudRate;
  1454. *clk_sel_val = 0x0;
  1455. }
  1456. if ((baudRate > 115200) && (baudRate <= 230400)) {
  1457. *divisor = 230400 / baudRate;
  1458. *clk_sel_val = 0x10;
  1459. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  1460. *divisor = 403200 / baudRate;
  1461. *clk_sel_val = 0x20;
  1462. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  1463. *divisor = 460800 / baudRate;
  1464. *clk_sel_val = 0x30;
  1465. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  1466. *divisor = 806400 / baudRate;
  1467. *clk_sel_val = 0x40;
  1468. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  1469. *divisor = 921600 / baudRate;
  1470. *clk_sel_val = 0x50;
  1471. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  1472. *divisor = 1572864 / baudRate;
  1473. *clk_sel_val = 0x60;
  1474. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  1475. *divisor = 3145728 / baudRate;
  1476. *clk_sel_val = 0x70;
  1477. }
  1478. return 0;
  1479. #ifdef NOTMCS7840
  1480. for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
  1481. if (mos7840_divisor_table[i].BaudRate == baudrate) {
  1482. *divisor = mos7840_divisor_table[i].Divisor;
  1483. return 0;
  1484. }
  1485. }
  1486. /* After trying for all the standard baud rates *
  1487. * Try calculating the divisor for this baud rate */
  1488. if (baudrate > 75 && baudrate < 230400) {
  1489. /* get the divisor */
  1490. custom = (__u16) (230400L / baudrate);
  1491. /* Check for round off */
  1492. round1 = (__u16) (2304000L / baudrate);
  1493. round = (__u16) (round1 - (custom * 10));
  1494. if (round > 4) {
  1495. custom++;
  1496. }
  1497. *divisor = custom;
  1498. dbg(" Baud %d = %d\n", baudrate, custom);
  1499. return 0;
  1500. }
  1501. dbg("%s\n", " Baud calculation Failed...");
  1502. return -1;
  1503. #endif
  1504. }
  1505. /*****************************************************************************
  1506. * mos7840_send_cmd_write_baud_rate
  1507. * this function sends the proper command to change the baud rate of the
  1508. * specified port.
  1509. *****************************************************************************/
  1510. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  1511. int baudRate)
  1512. {
  1513. int divisor = 0;
  1514. int status;
  1515. __u16 Data;
  1516. unsigned char number;
  1517. __u16 clk_sel_val;
  1518. struct usb_serial_port *port;
  1519. if (mos7840_port == NULL)
  1520. return -1;
  1521. port = (struct usb_serial_port *)mos7840_port->port;
  1522. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1523. dbg("%s", "Invalid port \n");
  1524. return -1;
  1525. }
  1526. if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
  1527. dbg("%s", "Invalid Serial \n");
  1528. return -1;
  1529. }
  1530. dbg("%s", "Entering .......... \n");
  1531. number = mos7840_port->port->number - mos7840_port->port->serial->minor;
  1532. dbg("%s - port = %d, baud = %d", __FUNCTION__,
  1533. mos7840_port->port->number, baudRate);
  1534. //reset clk_uart_sel in spregOffset
  1535. if (baudRate > 115200) {
  1536. #ifdef HW_flow_control
  1537. //NOTE: need to see the pther register to modify
  1538. //setting h/w flow control bit to 1;
  1539. status = 0;
  1540. Data = 0x2b;
  1541. mos7840_port->shadowMCR = Data;
  1542. status =
  1543. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1544. if (status < 0) {
  1545. dbg("Writing spreg failed in set_serial_baud\n");
  1546. return -1;
  1547. }
  1548. #endif
  1549. } else {
  1550. #ifdef HW_flow_control
  1551. //setting h/w flow control bit to 0;
  1552. status = 0;
  1553. Data = 0xb;
  1554. mos7840_port->shadowMCR = Data;
  1555. status =
  1556. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1557. if (status < 0) {
  1558. dbg("Writing spreg failed in set_serial_baud\n");
  1559. return -1;
  1560. }
  1561. #endif
  1562. }
  1563. if (1) //baudRate <= 115200)
  1564. {
  1565. clk_sel_val = 0x0;
  1566. Data = 0x0;
  1567. status = 0;
  1568. status =
  1569. mos7840_calc_baud_rate_divisor(baudRate, &divisor,
  1570. &clk_sel_val);
  1571. status =
  1572. mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  1573. &Data);
  1574. if (status < 0) {
  1575. dbg("reading spreg failed in set_serial_baud\n");
  1576. return -1;
  1577. }
  1578. Data = (Data & 0x8f) | clk_sel_val;
  1579. status = 0;
  1580. status =
  1581. mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  1582. if (status < 0) {
  1583. dbg("Writing spreg failed in set_serial_baud\n");
  1584. return -1;
  1585. }
  1586. /* Calculate the Divisor */
  1587. if (status) {
  1588. err("%s - bad baud rate", __FUNCTION__);
  1589. dbg("%s\n", "bad baud rate");
  1590. return status;
  1591. }
  1592. /* Enable access to divisor latch */
  1593. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  1594. mos7840_port->shadowLCR = Data;
  1595. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1596. /* Write the divisor */
  1597. Data = (unsigned char)(divisor & 0xff);
  1598. dbg("set_serial_baud Value to write DLL is %x\n", Data);
  1599. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  1600. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1601. dbg("set_serial_baud Value to write DLM is %x\n", Data);
  1602. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1603. /* Disable access to divisor latch */
  1604. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1605. mos7840_port->shadowLCR = Data;
  1606. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1607. }
  1608. return status;
  1609. }
  1610. /*****************************************************************************
  1611. * mos7840_change_port_settings
  1612. * This routine is called to set the UART on the device to match
  1613. * the specified new settings.
  1614. *****************************************************************************/
  1615. static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
  1616. struct termios *old_termios)
  1617. {
  1618. struct tty_struct *tty;
  1619. int baud;
  1620. unsigned cflag;
  1621. unsigned iflag;
  1622. __u8 lData;
  1623. __u8 lParity;
  1624. __u8 lStop;
  1625. int status;
  1626. __u16 Data;
  1627. struct usb_serial_port *port;
  1628. struct usb_serial *serial;
  1629. if (mos7840_port == NULL)
  1630. return;
  1631. port = (struct usb_serial_port *)mos7840_port->port;
  1632. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1633. dbg("%s", "Invalid port \n");
  1634. return;
  1635. }
  1636. if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
  1637. dbg("%s", "Invalid Serial \n");
  1638. return;
  1639. }
  1640. serial = port->serial;
  1641. dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
  1642. if (!mos7840_port->open) {
  1643. dbg("%s - port not opened", __FUNCTION__);
  1644. return;
  1645. }
  1646. tty = mos7840_port->port->tty;
  1647. if ((!tty) || (!tty->termios)) {
  1648. dbg("%s - no tty structures", __FUNCTION__);
  1649. return;
  1650. }
  1651. dbg("%s", "Entering .......... \n");
  1652. lData = LCR_BITS_8;
  1653. lStop = LCR_STOP_1;
  1654. lParity = LCR_PAR_NONE;
  1655. cflag = tty->termios->c_cflag;
  1656. iflag = tty->termios->c_iflag;
  1657. /* Change the number of bits */
  1658. if (cflag & CSIZE) {
  1659. switch (cflag & CSIZE) {
  1660. case CS5:
  1661. lData = LCR_BITS_5;
  1662. break;
  1663. case CS6:
  1664. lData = LCR_BITS_6;
  1665. break;
  1666. case CS7:
  1667. lData = LCR_BITS_7;
  1668. break;
  1669. default:
  1670. case CS8:
  1671. lData = LCR_BITS_8;
  1672. break;
  1673. }
  1674. }
  1675. /* Change the Parity bit */
  1676. if (cflag & PARENB) {
  1677. if (cflag & PARODD) {
  1678. lParity = LCR_PAR_ODD;
  1679. dbg("%s - parity = odd", __FUNCTION__);
  1680. } else {
  1681. lParity = LCR_PAR_EVEN;
  1682. dbg("%s - parity = even", __FUNCTION__);
  1683. }
  1684. } else {
  1685. dbg("%s - parity = none", __FUNCTION__);
  1686. }
  1687. if (cflag & CMSPAR) {
  1688. lParity = lParity | 0x20;
  1689. }
  1690. /* Change the Stop bit */
  1691. if (cflag & CSTOPB) {
  1692. lStop = LCR_STOP_2;
  1693. dbg("%s - stop bits = 2", __FUNCTION__);
  1694. } else {
  1695. lStop = LCR_STOP_1;
  1696. dbg("%s - stop bits = 1", __FUNCTION__);
  1697. }
  1698. /* Update the LCR with the correct value */
  1699. mos7840_port->shadowLCR &=
  1700. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1701. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1702. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
  1703. mos7840_port->shadowLCR);
  1704. /* Disable Interrupts */
  1705. Data = 0x00;
  1706. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1707. Data = 0x00;
  1708. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1709. Data = 0xcf;
  1710. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1711. /* Send the updated LCR value to the mos7840 */
  1712. Data = mos7840_port->shadowLCR;
  1713. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1714. Data = 0x00b;
  1715. mos7840_port->shadowMCR = Data;
  1716. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1717. Data = 0x00b;
  1718. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1719. /* set up the MCR register and send it to the mos7840 */
  1720. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1721. if (cflag & CBAUD) {
  1722. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1723. }
  1724. if (cflag & CRTSCTS) {
  1725. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1726. } else {
  1727. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1728. }
  1729. Data = mos7840_port->shadowMCR;
  1730. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1731. /* Determine divisor based on baud rate */
  1732. baud = tty_get_baud_rate(tty);
  1733. if (!baud) {
  1734. /* pick a default, any default... */
  1735. dbg("%s\n", "Picked default baud...");
  1736. baud = 9600;
  1737. }
  1738. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  1739. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1740. /* Enable Interrupts */
  1741. Data = 0x0c;
  1742. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1743. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1744. mos7840_port->read_urb->dev = serial->dev;
  1745. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1746. if (status) {
  1747. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1748. status);
  1749. }
  1750. }
  1751. wake_up(&mos7840_port->delta_msr_wait);
  1752. mos7840_port->delta_msr_cond = 1;
  1753. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
  1754. mos7840_port->shadowLCR);
  1755. return;
  1756. }
  1757. /*****************************************************************************
  1758. * mos7840_set_termios
  1759. * this function is called by the tty driver when it wants to change
  1760. * the termios structure
  1761. *****************************************************************************/
  1762. static void mos7840_set_termios(struct usb_serial_port *port,
  1763. struct termios *old_termios)
  1764. {
  1765. int status;
  1766. unsigned int cflag;
  1767. struct usb_serial *serial;
  1768. struct moschip_port *mos7840_port;
  1769. struct tty_struct *tty;
  1770. dbg("mos7840_set_termios: START\n");
  1771. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1772. dbg("%s", "Invalid port \n");
  1773. return;
  1774. }
  1775. serial = port->serial;
  1776. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  1777. dbg("%s", "Invalid Serial \n");
  1778. return;
  1779. }
  1780. mos7840_port = mos7840_get_port_private(port);
  1781. if (mos7840_port == NULL)
  1782. return;
  1783. tty = port->tty;
  1784. if (!port->tty || !port->tty->termios) {
  1785. dbg("%s - no tty or termios", __FUNCTION__);
  1786. return;
  1787. }
  1788. if (!mos7840_port->open) {
  1789. dbg("%s - port not opened", __FUNCTION__);
  1790. return;
  1791. }
  1792. dbg("%s\n", "setting termios - ");
  1793. cflag = tty->termios->c_cflag;
  1794. if (!cflag) {
  1795. dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
  1796. return;
  1797. }
  1798. /* check that they really want us to change something */
  1799. if (old_termios) {
  1800. if ((cflag == old_termios->c_cflag) &&
  1801. (RELEVANT_IFLAG(tty->termios->c_iflag) ==
  1802. RELEVANT_IFLAG(old_termios->c_iflag))) {
  1803. dbg("%s\n", "Nothing to change");
  1804. return;
  1805. }
  1806. }
  1807. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1808. tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
  1809. if (old_termios) {
  1810. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1811. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1812. }
  1813. dbg("%s - port %d", __FUNCTION__, port->number);
  1814. /* change the port settings to the new ones specified */
  1815. mos7840_change_port_settings(mos7840_port, old_termios);
  1816. if (!mos7840_port->read_urb) {
  1817. dbg("%s", "URB KILLED !!!!!\n");
  1818. return;
  1819. }
  1820. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1821. mos7840_port->read_urb->dev = serial->dev;
  1822. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1823. if (status) {
  1824. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1825. status);
  1826. }
  1827. }
  1828. return;
  1829. }
  1830. /*****************************************************************************
  1831. * mos7840_get_lsr_info - get line status register info
  1832. *
  1833. * Purpose: Let user call ioctl() to get info when the UART physically
  1834. * is emptied. On bus types like RS485, the transmitter must
  1835. * release the bus after transmitting. This must be done when
  1836. * the transmit shift register is empty, not be done when the
  1837. * transmit holding register is empty. This functionality
  1838. * allows an RS485 driver to be written in user space.
  1839. *****************************************************************************/
  1840. static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
  1841. unsigned int __user *value)
  1842. {
  1843. int count;
  1844. unsigned int result = 0;
  1845. count = mos7840_chars_in_buffer(mos7840_port->port);
  1846. if (count == 0) {
  1847. dbg("%s -- Empty", __FUNCTION__);
  1848. result = TIOCSER_TEMT;
  1849. }
  1850. if (copy_to_user(value, &result, sizeof(int)))
  1851. return -EFAULT;
  1852. return 0;
  1853. }
  1854. /*****************************************************************************
  1855. * mos7840_get_bytes_avail - get number of bytes available
  1856. *
  1857. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1858. *****************************************************************************/
  1859. static int mos7840_get_bytes_avail(struct moschip_port *mos7840_port,
  1860. unsigned int __user *value)
  1861. {
  1862. unsigned int result = 0;
  1863. struct tty_struct *tty = mos7840_port->port->tty;
  1864. if (!tty)
  1865. return -ENOIOCTLCMD;
  1866. result = tty->read_cnt;
  1867. dbg("%s(%d) = %d", __FUNCTION__, mos7840_port->port->number, result);
  1868. if (copy_to_user(value, &result, sizeof(int)))
  1869. return -EFAULT;
  1870. return -ENOIOCTLCMD;
  1871. }
  1872. /*****************************************************************************
  1873. * mos7840_set_modem_info
  1874. * function to set modem info
  1875. *****************************************************************************/
  1876. static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
  1877. unsigned int cmd, unsigned int __user *value)
  1878. {
  1879. unsigned int mcr;
  1880. unsigned int arg;
  1881. __u16 Data;
  1882. int status;
  1883. struct usb_serial_port *port;
  1884. if (mos7840_port == NULL)
  1885. return -1;
  1886. port = (struct usb_serial_port *)mos7840_port->port;
  1887. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1888. dbg("%s", "Invalid port \n");
  1889. return -1;
  1890. }
  1891. mcr = mos7840_port->shadowMCR;
  1892. if (copy_from_user(&arg, value, sizeof(int)))
  1893. return -EFAULT;
  1894. switch (cmd) {
  1895. case TIOCMBIS:
  1896. if (arg & TIOCM_RTS)
  1897. mcr |= MCR_RTS;
  1898. if (arg & TIOCM_DTR)
  1899. mcr |= MCR_RTS;
  1900. if (arg & TIOCM_LOOP)
  1901. mcr |= MCR_LOOPBACK;
  1902. break;
  1903. case TIOCMBIC:
  1904. if (arg & TIOCM_RTS)
  1905. mcr &= ~MCR_RTS;
  1906. if (arg & TIOCM_DTR)
  1907. mcr &= ~MCR_RTS;
  1908. if (arg & TIOCM_LOOP)
  1909. mcr &= ~MCR_LOOPBACK;
  1910. break;
  1911. case TIOCMSET:
  1912. /* turn off the RTS and DTR and LOOPBACK
  1913. * and then only turn on what was asked to */
  1914. mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
  1915. mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
  1916. mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
  1917. mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
  1918. break;
  1919. }
  1920. mos7840_port->shadowMCR = mcr;
  1921. Data = mos7840_port->shadowMCR;
  1922. status = 0;
  1923. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1924. if (status < 0) {
  1925. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1926. return -1;
  1927. }
  1928. return 0;
  1929. }
  1930. /*****************************************************************************
  1931. * mos7840_get_modem_info
  1932. * function to get modem info
  1933. *****************************************************************************/
  1934. static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
  1935. unsigned int __user *value)
  1936. {
  1937. unsigned int result = 0;
  1938. __u16 msr;
  1939. unsigned int mcr = mos7840_port->shadowMCR;
  1940. int status = 0;
  1941. status =
  1942. mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
  1943. &msr);
  1944. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1945. |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1946. |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1947. |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
  1948. |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1949. |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1950. dbg("%s -- %x", __FUNCTION__, result);
  1951. if (copy_to_user(value, &result, sizeof(int)))
  1952. return -EFAULT;
  1953. return 0;
  1954. }
  1955. /*****************************************************************************
  1956. * mos7840_get_serial_info
  1957. * function to get information about serial port
  1958. *****************************************************************************/
  1959. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1960. struct serial_struct __user *retinfo)
  1961. {
  1962. struct serial_struct tmp;
  1963. if (mos7840_port == NULL)
  1964. return -1;
  1965. if (!retinfo)
  1966. return -EFAULT;
  1967. memset(&tmp, 0, sizeof(tmp));
  1968. tmp.type = PORT_16550A;
  1969. tmp.line = mos7840_port->port->serial->minor;
  1970. tmp.port = mos7840_port->port->number;
  1971. tmp.irq = 0;
  1972. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1973. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1974. tmp.baud_base = 9600;
  1975. tmp.close_delay = 5 * HZ;
  1976. tmp.closing_wait = 30 * HZ;
  1977. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1978. return -EFAULT;
  1979. return 0;
  1980. }
  1981. /*****************************************************************************
  1982. * SerialIoctl
  1983. * this function handles any ioctl calls to the driver
  1984. *****************************************************************************/
  1985. static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
  1986. unsigned int cmd, unsigned long arg)
  1987. {
  1988. void __user *argp = (void __user *)arg;
  1989. struct moschip_port *mos7840_port;
  1990. struct tty_struct *tty;
  1991. struct async_icount cnow;
  1992. struct async_icount cprev;
  1993. struct serial_icounter_struct icount;
  1994. int mosret = 0;
  1995. int retval;
  1996. struct tty_ldisc *ld;
  1997. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1998. dbg("%s", "Invalid port \n");
  1999. return -1;
  2000. }
  2001. mos7840_port = mos7840_get_port_private(port);
  2002. if (mos7840_port == NULL)
  2003. return -1;
  2004. tty = mos7840_port->port->tty;
  2005. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  2006. switch (cmd) {
  2007. /* return number of bytes available */
  2008. case TIOCINQ:
  2009. dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
  2010. return mos7840_get_bytes_avail(mos7840_port, argp);
  2011. case TIOCOUTQ:
  2012. dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
  2013. return put_user(tty->driver->chars_in_buffer ?
  2014. tty->driver->chars_in_buffer(tty) : 0,
  2015. (int __user *)arg);
  2016. case TCFLSH:
  2017. retval = tty_check_change(tty);
  2018. if (retval)
  2019. return retval;
  2020. ld = tty_ldisc_ref(tty);
  2021. switch (arg) {
  2022. case TCIFLUSH:
  2023. if (ld && ld->flush_buffer)
  2024. ld->flush_buffer(tty);
  2025. break;
  2026. case TCIOFLUSH:
  2027. if (ld && ld->flush_buffer)
  2028. ld->flush_buffer(tty);
  2029. /* fall through */
  2030. case TCOFLUSH:
  2031. if (tty->driver->flush_buffer)
  2032. tty->driver->flush_buffer(tty);
  2033. break;
  2034. default:
  2035. tty_ldisc_deref(ld);
  2036. return -EINVAL;
  2037. }
  2038. tty_ldisc_deref(ld);
  2039. return 0;
  2040. case TCGETS:
  2041. if (kernel_termios_to_user_termios
  2042. ((struct termios __user *)argp, tty->termios))
  2043. return -EFAULT;
  2044. return 0;
  2045. case TIOCSERGETLSR:
  2046. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  2047. return mos7840_get_lsr_info(mos7840_port, argp);
  2048. return 0;
  2049. case TIOCMBIS:
  2050. case TIOCMBIC:
  2051. case TIOCMSET:
  2052. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
  2053. port->number);
  2054. mosret =
  2055. mos7840_set_modem_info(mos7840_port, cmd, argp);
  2056. return mosret;
  2057. case TIOCMGET:
  2058. dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
  2059. return mos7840_get_modem_info(mos7840_port, argp);
  2060. case TIOCGSERIAL:
  2061. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  2062. return mos7840_get_serial_info(mos7840_port, argp);
  2063. case TIOCSSERIAL:
  2064. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  2065. break;
  2066. case TIOCMIWAIT:
  2067. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  2068. cprev = mos7840_port->icount;
  2069. while (1) {
  2070. //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
  2071. mos7840_port->delta_msr_cond = 0;
  2072. wait_event_interruptible(mos7840_port->delta_msr_wait,
  2073. (mos7840_port->
  2074. delta_msr_cond == 1));
  2075. /* see if a signal did it */
  2076. if (signal_pending(current))
  2077. return -ERESTARTSYS;
  2078. cnow = mos7840_port->icount;
  2079. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2080. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  2081. return -EIO; /* no change => error */
  2082. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  2083. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  2084. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  2085. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  2086. return 0;
  2087. }
  2088. cprev = cnow;
  2089. }
  2090. /* NOTREACHED */
  2091. break;
  2092. case TIOCGICOUNT:
  2093. cnow = mos7840_port->icount;
  2094. icount.cts = cnow.cts;
  2095. icount.dsr = cnow.dsr;
  2096. icount.rng = cnow.rng;
  2097. icount.dcd = cnow.dcd;
  2098. icount.rx = cnow.rx;
  2099. icount.tx = cnow.tx;
  2100. icount.frame = cnow.frame;
  2101. icount.overrun = cnow.overrun;
  2102. icount.parity = cnow.parity;
  2103. icount.brk = cnow.brk;
  2104. icount.buf_overrun = cnow.buf_overrun;
  2105. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  2106. port->number, icount.rx, icount.tx);
  2107. if (copy_to_user(argp, &icount, sizeof(icount)))
  2108. return -EFAULT;
  2109. return 0;
  2110. case TIOCEXBAUD:
  2111. return 0;
  2112. default:
  2113. break;
  2114. }
  2115. return -ENOIOCTLCMD;
  2116. }
  2117. static int mos7840_calc_num_ports(struct usb_serial *serial)
  2118. {
  2119. dbg("numberofendpoints: %d \n",
  2120. (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
  2121. dbg("numberofendpoints: %d \n",
  2122. (int)serial->interface->altsetting->desc.bNumEndpoints);
  2123. if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
  2124. mos7840_num_ports = 2;
  2125. serial->type->num_ports = 2;
  2126. } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
  2127. mos7840_num_ports = 4;
  2128. serial->type->num_bulk_in = 4;
  2129. serial->type->num_bulk_out = 4;
  2130. serial->type->num_ports = 4;
  2131. }
  2132. return mos7840_num_ports;
  2133. }
  2134. /****************************************************************************
  2135. * mos7840_startup
  2136. ****************************************************************************/
  2137. static int mos7840_startup(struct usb_serial *serial)
  2138. {
  2139. struct moschip_port *mos7840_port;
  2140. struct usb_device *dev;
  2141. int i, status;
  2142. __u16 Data;
  2143. dbg("%s \n", " mos7840_startup :entering..........");
  2144. if (!serial) {
  2145. dbg("%s\n", "Invalid Handler");
  2146. return -1;
  2147. }
  2148. dev = serial->dev;
  2149. dbg("%s\n", "Entering...");
  2150. /* we set up the pointers to the endpoints in the mos7840_open *
  2151. * function, as the structures aren't created yet. */
  2152. /* set up port private structures */
  2153. for (i = 0; i < serial->num_ports; ++i) {
  2154. mos7840_port = kmalloc(sizeof(struct moschip_port), GFP_KERNEL);
  2155. if (mos7840_port == NULL) {
  2156. err("%s - Out of memory", __FUNCTION__);
  2157. return -ENOMEM;
  2158. }
  2159. memset(mos7840_port, 0, sizeof(struct moschip_port));
  2160. /* Initialize all port interrupt end point to port 0 int endpoint *
  2161. * Our device has only one interrupt end point comman to all port */
  2162. mos7840_port->port = serial->port[i];
  2163. mos7840_set_port_private(serial->port[i], mos7840_port);
  2164. mos7840_port->port_num = ((serial->port[i]->number -
  2165. (serial->port[i]->serial->minor)) +
  2166. 1);
  2167. if (mos7840_port->port_num == 1) {
  2168. mos7840_port->SpRegOffset = 0x0;
  2169. mos7840_port->ControlRegOffset = 0x1;
  2170. mos7840_port->DcrRegOffset = 0x4;
  2171. } else if ((mos7840_port->port_num == 2)
  2172. && (mos7840_num_ports == 4)) {
  2173. mos7840_port->SpRegOffset = 0x8;
  2174. mos7840_port->ControlRegOffset = 0x9;
  2175. mos7840_port->DcrRegOffset = 0x16;
  2176. } else if ((mos7840_port->port_num == 2)
  2177. && (mos7840_num_ports == 2)) {
  2178. mos7840_port->SpRegOffset = 0xa;
  2179. mos7840_port->ControlRegOffset = 0xb;
  2180. mos7840_port->DcrRegOffset = 0x19;
  2181. } else if ((mos7840_port->port_num == 3)
  2182. && (mos7840_num_ports == 4)) {
  2183. mos7840_port->SpRegOffset = 0xa;
  2184. mos7840_port->ControlRegOffset = 0xb;
  2185. mos7840_port->DcrRegOffset = 0x19;
  2186. } else if ((mos7840_port->port_num == 4)
  2187. && (mos7840_num_ports == 4)) {
  2188. mos7840_port->SpRegOffset = 0xc;
  2189. mos7840_port->ControlRegOffset = 0xd;
  2190. mos7840_port->DcrRegOffset = 0x1c;
  2191. }
  2192. mos7840_dump_serial_port(mos7840_port);
  2193. mos7840_set_port_private(serial->port[i], mos7840_port);
  2194. //enable rx_disable bit in control register
  2195. status =
  2196. mos7840_get_reg_sync(serial->port[i],
  2197. mos7840_port->ControlRegOffset, &Data);
  2198. if (status < 0) {
  2199. dbg("Reading ControlReg failed status-0x%x\n", status);
  2200. break;
  2201. } else
  2202. dbg("ControlReg Reading success val is %x, status%d\n",
  2203. Data, status);
  2204. Data |= 0x08; //setting driver done bit
  2205. Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
  2206. //Data |= 0x20; //rx_disable bit
  2207. status = 0;
  2208. status =
  2209. mos7840_set_reg_sync(serial->port[i],
  2210. mos7840_port->ControlRegOffset, Data);
  2211. if (status < 0) {
  2212. dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  2213. break;
  2214. } else
  2215. dbg("ControlReg Writing success(rx_disable) status%d\n",
  2216. status);
  2217. //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
  2218. Data = 0x01;
  2219. status = 0;
  2220. status =
  2221. mos7840_set_reg_sync(serial->port[i],
  2222. (__u16) (mos7840_port->DcrRegOffset +
  2223. 0), Data);
  2224. if (status < 0) {
  2225. dbg("Writing DCR0 failed status-0x%x\n", status);
  2226. break;
  2227. } else
  2228. dbg("DCR0 Writing success status%d\n", status);
  2229. Data = 0x05;
  2230. status = 0;
  2231. status =
  2232. mos7840_set_reg_sync(serial->port[i],
  2233. (__u16) (mos7840_port->DcrRegOffset +
  2234. 1), Data);
  2235. if (status < 0) {
  2236. dbg("Writing DCR1 failed status-0x%x\n", status);
  2237. break;
  2238. } else
  2239. dbg("DCR1 Writing success status%d\n", status);
  2240. Data = 0x24;
  2241. status = 0;
  2242. status =
  2243. mos7840_set_reg_sync(serial->port[i],
  2244. (__u16) (mos7840_port->DcrRegOffset +
  2245. 2), Data);
  2246. if (status < 0) {
  2247. dbg("Writing DCR2 failed status-0x%x\n", status);
  2248. break;
  2249. } else
  2250. dbg("DCR2 Writing success status%d\n", status);
  2251. // write values in clkstart0x0 and clkmulti 0x20
  2252. Data = 0x0;
  2253. status = 0;
  2254. status =
  2255. mos7840_set_reg_sync(serial->port[i],
  2256. CLK_START_VALUE_REGISTER, Data);
  2257. if (status < 0) {
  2258. dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  2259. break;
  2260. } else
  2261. dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  2262. Data = 0x20;
  2263. status = 0;
  2264. status =
  2265. mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
  2266. Data);
  2267. if (status < 0) {
  2268. dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
  2269. status);
  2270. break;
  2271. } else
  2272. dbg("CLK_MULTI_REGISTER Writing success status%d\n",
  2273. status);
  2274. //write value 0x0 to scratchpad register
  2275. Data = 0x00;
  2276. status = 0;
  2277. status =
  2278. mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
  2279. Data);
  2280. if (status < 0) {
  2281. dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
  2282. status);
  2283. break;
  2284. } else
  2285. dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
  2286. status);
  2287. //Zero Length flag register
  2288. if ((mos7840_port->port_num != 1)
  2289. && (mos7840_num_ports == 2)) {
  2290. Data = 0xff;
  2291. status = 0;
  2292. status = mos7840_set_reg_sync(serial->port[i],
  2293. (__u16) (ZLP_REG1 +
  2294. ((__u16)
  2295. mos7840_port->
  2296. port_num)),
  2297. Data);
  2298. dbg("ZLIP offset%x\n",
  2299. (__u16) (ZLP_REG1 +
  2300. ((__u16) mos7840_port->port_num)));
  2301. if (status < 0) {
  2302. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2303. i + 2, status);
  2304. break;
  2305. } else
  2306. dbg("ZLP_REG%d Writing success status%d\n",
  2307. i + 2, status);
  2308. } else {
  2309. Data = 0xff;
  2310. status = 0;
  2311. status = mos7840_set_reg_sync(serial->port[i],
  2312. (__u16) (ZLP_REG1 +
  2313. ((__u16)
  2314. mos7840_port->
  2315. port_num) -
  2316. 0x1), Data);
  2317. dbg("ZLIP offset%x\n",
  2318. (__u16) (ZLP_REG1 +
  2319. ((__u16) mos7840_port->port_num) - 0x1));
  2320. if (status < 0) {
  2321. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2322. i + 1, status);
  2323. break;
  2324. } else
  2325. dbg("ZLP_REG%d Writing success status%d\n",
  2326. i + 1, status);
  2327. }
  2328. mos7840_port->control_urb = usb_alloc_urb(0, SLAB_ATOMIC);
  2329. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  2330. }
  2331. //Zero Length flag enable
  2332. Data = 0x0f;
  2333. status = 0;
  2334. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  2335. if (status < 0) {
  2336. dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
  2337. return -1;
  2338. } else
  2339. dbg("ZLP_REG5 Writing success status%d\n", status);
  2340. /* setting configuration feature to one */
  2341. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  2342. (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
  2343. return 0;
  2344. }
  2345. /****************************************************************************
  2346. * mos7840_shutdown
  2347. * This function is called whenever the device is removed from the usb bus.
  2348. ****************************************************************************/
  2349. static void mos7840_shutdown(struct usb_serial *serial)
  2350. {
  2351. int i;
  2352. struct moschip_port *mos7840_port;
  2353. dbg("%s \n", " shutdown :entering..........");
  2354. if (!serial) {
  2355. dbg("%s", "Invalid Handler \n");
  2356. return;
  2357. }
  2358. /* check for the ports to be closed,close the ports and disconnect */
  2359. /* free private structure allocated for serial port *
  2360. * stop reads and writes on all ports */
  2361. for (i = 0; i < serial->num_ports; ++i) {
  2362. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2363. kfree(mos7840_port->ctrl_buf);
  2364. usb_kill_urb(mos7840_port->control_urb);
  2365. kfree(mos7840_port);
  2366. mos7840_set_port_private(serial->port[i], NULL);
  2367. }
  2368. dbg("%s\n", "Thank u :: ");
  2369. }
  2370. static struct usb_serial_driver moschip7840_4port_device = {
  2371. .driver = {
  2372. .owner = THIS_MODULE,
  2373. .name = "mos7840",
  2374. },
  2375. .description = DRIVER_DESC,
  2376. .id_table = moschip_port_id_table,
  2377. .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
  2378. #ifdef check
  2379. .num_bulk_in = 4,
  2380. .num_bulk_out = 4,
  2381. .num_ports = 4,
  2382. #endif
  2383. .open = mos7840_open,
  2384. .close = mos7840_close,
  2385. .write = mos7840_write,
  2386. .write_room = mos7840_write_room,
  2387. .chars_in_buffer = mos7840_chars_in_buffer,
  2388. .throttle = mos7840_throttle,
  2389. .unthrottle = mos7840_unthrottle,
  2390. .calc_num_ports = mos7840_calc_num_ports,
  2391. #ifdef MCSSerialProbe
  2392. .probe = mos7840_serial_probe,
  2393. #endif
  2394. .ioctl = mos7840_ioctl,
  2395. .set_termios = mos7840_set_termios,
  2396. .break_ctl = mos7840_break,
  2397. .tiocmget = mos7840_tiocmget,
  2398. .tiocmset = mos7840_tiocmset,
  2399. .attach = mos7840_startup,
  2400. .shutdown = mos7840_shutdown,
  2401. .read_bulk_callback = mos7840_bulk_in_callback,
  2402. .read_int_callback = mos7840_interrupt_callback,
  2403. };
  2404. static struct usb_driver io_driver = {
  2405. .name = "mos7840",
  2406. .probe = usb_serial_probe,
  2407. .disconnect = usb_serial_disconnect,
  2408. .id_table = moschip_id_table_combined,
  2409. };
  2410. /****************************************************************************
  2411. * moschip7840_init
  2412. * This is called by the module subsystem, or on startup to initialize us
  2413. ****************************************************************************/
  2414. static int __init moschip7840_init(void)
  2415. {
  2416. int retval;
  2417. dbg("%s \n", " mos7840_init :entering..........");
  2418. /* Register with the usb serial */
  2419. retval = usb_serial_register(&moschip7840_4port_device);
  2420. if (retval)
  2421. goto failed_port_device_register;
  2422. dbg("%s\n", "Entring...");
  2423. info(DRIVER_DESC " " DRIVER_VERSION);
  2424. /* Register with the usb */
  2425. retval = usb_register(&io_driver);
  2426. if (retval)
  2427. goto failed_usb_register;
  2428. if (retval == 0) {
  2429. dbg("%s\n", "Leaving...");
  2430. return 0;
  2431. }
  2432. failed_usb_register:
  2433. usb_serial_deregister(&moschip7840_4port_device);
  2434. failed_port_device_register:
  2435. return retval;
  2436. }
  2437. /****************************************************************************
  2438. * moschip7840_exit
  2439. * Called when the driver is about to be unloaded.
  2440. ****************************************************************************/
  2441. static void __exit moschip7840_exit(void)
  2442. {
  2443. dbg("%s \n", " mos7840_exit :entering..........");
  2444. usb_deregister(&io_driver);
  2445. usb_serial_deregister(&moschip7840_4port_device);
  2446. dbg("%s\n", "Entring...");
  2447. }
  2448. module_init(moschip7840_init);
  2449. module_exit(moschip7840_exit);
  2450. /* Module information */
  2451. MODULE_DESCRIPTION(DRIVER_DESC);
  2452. MODULE_LICENSE("GPL");
  2453. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2454. MODULE_PARM_DESC(debug, "Debug enabled or not");