mos7840.c 77 KB

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