mos7840.c 78 KB

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