mos7840.c 75 KB

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