mos7840.c 70 KB

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