mos7840.c 77 KB

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