mos7840.c 76 KB

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