mos7840.c 69 KB

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