mos7840.c 78 KB

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