mos7840.c 75 KB

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