mos7840.c 76 KB

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