mos7840.c 79 KB

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