mos7840.c 77 KB

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