mos7840.c 73 KB

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