mos7840.c 77 KB

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