mos7840.c 69 KB

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