mos7840.c 68 KB

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