mos7840.c 75 KB

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