mos7840.c 78 KB

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