mos7840.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Clean ups from Moschip version and a few ioctl implementations by:
  4. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  5. *
  6. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  7. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  8. * Copyright (C) 2001-2002 Greg Kroah-Hartman <[email protected]>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/serial.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/uaccess.h>
  22. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  23. /*
  24. * 16C50 UART register defines
  25. */
  26. #define LCR_BITS_5 0x00 /* 5 bits/char */
  27. #define LCR_BITS_6 0x01 /* 6 bits/char */
  28. #define LCR_BITS_7 0x02 /* 7 bits/char */
  29. #define LCR_BITS_8 0x03 /* 8 bits/char */
  30. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  31. #define LCR_STOP_1 0x00 /* 1 stop bit */
  32. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  33. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  34. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  35. #define LCR_PAR_NONE 0x00 /* No parity */
  36. #define LCR_PAR_ODD 0x08 /* Odd parity */
  37. #define LCR_PAR_EVEN 0x18 /* Even parity */
  38. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  39. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  40. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  41. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  42. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  43. #define MCR_DTR 0x01 /* Assert DTR */
  44. #define MCR_RTS 0x02 /* Assert RTS */
  45. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  46. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  47. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  48. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  49. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  50. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  51. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  52. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  53. /*
  54. * Defines used for sending commands to port
  55. */
  56. #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
  57. #define MOS_PORT1 0x0200
  58. #define MOS_PORT2 0x0300
  59. #define MOS_VENREG 0x0000
  60. #define MOS_MAX_PORT 0x02
  61. #define MOS_WRITE 0x0E
  62. #define MOS_READ 0x0D
  63. /* Requests */
  64. #define MCS_RD_RTYPE 0xC0
  65. #define MCS_WR_RTYPE 0x40
  66. #define MCS_RDREQ 0x0D
  67. #define MCS_WRREQ 0x0E
  68. #define MCS_CTRL_TIMEOUT 500
  69. #define VENDOR_READ_LENGTH (0x01)
  70. #define MAX_NAME_LEN 64
  71. #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
  72. #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
  73. /* For higher baud Rates use TIOCEXBAUD */
  74. #define TIOCEXBAUD 0x5462
  75. /*
  76. * Vendor id and device id defines
  77. *
  78. * NOTE: Do not add new defines, add entries directly to the id_table instead.
  79. */
  80. #define USB_VENDOR_ID_BANDB 0x0856
  81. #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
  82. #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
  83. #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
  84. #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
  85. #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
  86. #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
  87. #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
  88. #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
  89. #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
  90. #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
  91. #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
  92. #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
  93. /* Interrupt Routine Defines */
  94. #define SERIAL_IIR_RLS 0x06
  95. #define SERIAL_IIR_MS 0x00
  96. /*
  97. * Emulation of the bit mask on the LINE STATUS REGISTER.
  98. */
  99. #define SERIAL_LSR_DR 0x0001
  100. #define SERIAL_LSR_OE 0x0002
  101. #define SERIAL_LSR_PE 0x0004
  102. #define SERIAL_LSR_FE 0x0008
  103. #define SERIAL_LSR_BI 0x0010
  104. #define MOS_MSR_DELTA_CTS 0x10
  105. #define MOS_MSR_DELTA_DSR 0x20
  106. #define MOS_MSR_DELTA_RI 0x40
  107. #define MOS_MSR_DELTA_CD 0x80
  108. /* Serial Port register Address */
  109. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  110. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  111. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  112. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  113. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  114. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  115. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  116. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  117. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  118. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  119. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  120. #define GPIO_REGISTER ((__u16)(0x07))
  121. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  122. /*
  123. * URB POOL related defines
  124. */
  125. #define NUM_URBS 16 /* URB Count */
  126. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  127. /* LED on/off milliseconds*/
  128. #define LED_ON_MS 500
  129. #define LED_OFF_MS 500
  130. enum mos7840_flag {
  131. MOS7840_FLAG_LED_BUSY,
  132. };
  133. #define MCS_PORT_MASK GENMASK(2, 0)
  134. #define MCS_PORTS(nr) ((nr) & MCS_PORT_MASK)
  135. #define MCS_LED BIT(3)
  136. #define MCS_DEVICE(vid, pid, flags) \
  137. USB_DEVICE((vid), (pid)), .driver_info = (flags)
  138. static const struct usb_device_id id_table[] = {
  139. { MCS_DEVICE(0x0557, 0x2011, MCS_PORTS(4)) }, /* ATEN UC2324 */
  140. { MCS_DEVICE(0x0557, 0x7820, MCS_PORTS(2)) }, /* ATEN UC2322 */
  141. { MCS_DEVICE(0x110a, 0x2210, MCS_PORTS(2)) }, /* Moxa UPort 2210 */
  142. { MCS_DEVICE(0x9710, 0x7810, MCS_PORTS(1) | MCS_LED) }, /* ASIX MCS7810 */
  143. { MCS_DEVICE(0x9710, 0x7820, MCS_PORTS(2)) }, /* MosChip MCS7820 */
  144. { MCS_DEVICE(0x9710, 0x7840, MCS_PORTS(4)) }, /* MosChip MCS7840 */
  145. { MCS_DEVICE(0x9710, 0x7843, MCS_PORTS(3)) }, /* ASIX MCS7840 3 port */
  146. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2) },
  147. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P) },
  148. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4) },
  149. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P) },
  150. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2) },
  151. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4) },
  152. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2) },
  153. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4) },
  154. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2) },
  155. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P) },
  156. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4) },
  157. { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P) },
  158. {} /* terminating entry */
  159. };
  160. MODULE_DEVICE_TABLE(usb, id_table);
  161. /* This structure holds all of the local port information */
  162. struct moschip_port {
  163. int port_num; /*Actual port number in the device(1,2,etc) */
  164. struct urb *read_urb; /* read URB for this port */
  165. __u8 shadowLCR; /* last LCR value received */
  166. __u8 shadowMCR; /* last MCR value received */
  167. struct usb_serial_port *port; /* loop back to the owner of this object */
  168. /* Offsets */
  169. __u8 SpRegOffset;
  170. __u8 ControlRegOffset;
  171. __u8 DcrRegOffset;
  172. spinlock_t pool_lock;
  173. struct urb *write_urb_pool[NUM_URBS];
  174. char busy[NUM_URBS];
  175. bool read_urb_busy;
  176. /* For device(s) with LED indicator */
  177. bool has_led;
  178. struct timer_list led_timer1; /* Timer for LED on */
  179. struct timer_list led_timer2; /* Timer for LED off */
  180. struct urb *led_urb;
  181. struct usb_ctrlrequest *led_dr;
  182. unsigned long flags;
  183. };
  184. /*
  185. * mos7840_set_reg_sync
  186. * To set the Control register by calling usb_fill_control_urb function
  187. * by passing usb_sndctrlpipe function as parameter.
  188. */
  189. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  190. __u16 val)
  191. {
  192. struct usb_device *dev = port->serial->dev;
  193. val = val & 0x00ff;
  194. dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  195. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  196. MCS_WR_RTYPE, val, reg, NULL, 0,
  197. MOS_WDR_TIMEOUT);
  198. }
  199. /*
  200. * mos7840_get_reg_sync
  201. * To set the Uart register by calling usb_fill_control_urb function by
  202. * passing usb_rcvctrlpipe function as parameter.
  203. */
  204. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  205. __u16 *val)
  206. {
  207. struct usb_device *dev = port->serial->dev;
  208. int ret = 0;
  209. u8 *buf;
  210. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  211. if (!buf)
  212. return -ENOMEM;
  213. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  214. MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
  215. MOS_WDR_TIMEOUT);
  216. if (ret < VENDOR_READ_LENGTH) {
  217. if (ret >= 0)
  218. ret = -EIO;
  219. goto out;
  220. }
  221. *val = buf[0];
  222. dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
  223. out:
  224. kfree(buf);
  225. return ret;
  226. }
  227. /*
  228. * mos7840_set_uart_reg
  229. * To set the Uart register by calling usb_fill_control_urb function by
  230. * passing usb_sndctrlpipe function as parameter.
  231. */
  232. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  233. __u16 val)
  234. {
  235. struct usb_device *dev = port->serial->dev;
  236. val = val & 0x00ff;
  237. /* For the UART control registers, the application number need
  238. to be Or'ed */
  239. if (port->serial->num_ports == 2 && port->port_number != 0)
  240. val |= ((__u16)port->port_number + 2) << 8;
  241. else
  242. val |= ((__u16)port->port_number + 1) << 8;
  243. dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
  244. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  245. MCS_WR_RTYPE, val, reg, NULL, 0,
  246. MOS_WDR_TIMEOUT);
  247. }
  248. /*
  249. * mos7840_get_uart_reg
  250. * To set the Control register by calling usb_fill_control_urb function
  251. * by passing usb_rcvctrlpipe function as parameter.
  252. */
  253. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  254. __u16 *val)
  255. {
  256. struct usb_device *dev = port->serial->dev;
  257. int ret = 0;
  258. __u16 Wval;
  259. u8 *buf;
  260. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  261. if (!buf)
  262. return -ENOMEM;
  263. /* Wval is same as application number */
  264. if (port->serial->num_ports == 2 && port->port_number != 0)
  265. Wval = ((__u16)port->port_number + 2) << 8;
  266. else
  267. Wval = ((__u16)port->port_number + 1) << 8;
  268. dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
  269. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  270. MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
  271. MOS_WDR_TIMEOUT);
  272. if (ret < VENDOR_READ_LENGTH) {
  273. if (ret >= 0)
  274. ret = -EIO;
  275. goto out;
  276. }
  277. *val = buf[0];
  278. out:
  279. kfree(buf);
  280. return ret;
  281. }
  282. static void mos7840_dump_serial_port(struct usb_serial_port *port,
  283. struct moschip_port *mos7840_port)
  284. {
  285. dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  286. dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
  287. dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
  288. }
  289. /************************************************************************/
  290. /************************************************************************/
  291. /* U S B C A L L B A C K F U N C T I O N S */
  292. /* U S B C A L L B A C K F U N C T I O N S */
  293. /************************************************************************/
  294. /************************************************************************/
  295. static void mos7840_set_led_callback(struct urb *urb)
  296. {
  297. switch (urb->status) {
  298. case 0:
  299. /* Success */
  300. break;
  301. case -ECONNRESET:
  302. case -ENOENT:
  303. case -ESHUTDOWN:
  304. /* This urb is terminated, clean up */
  305. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  306. __func__, urb->status);
  307. break;
  308. default:
  309. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  310. __func__, urb->status);
  311. }
  312. }
  313. static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
  314. __u16 reg)
  315. {
  316. struct usb_device *dev = mcs->port->serial->dev;
  317. struct usb_ctrlrequest *dr = mcs->led_dr;
  318. dr->bRequestType = MCS_WR_RTYPE;
  319. dr->bRequest = MCS_WRREQ;
  320. dr->wValue = cpu_to_le16(wval);
  321. dr->wIndex = cpu_to_le16(reg);
  322. dr->wLength = cpu_to_le16(0);
  323. usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
  324. (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
  325. usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
  326. }
  327. static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
  328. __u16 val)
  329. {
  330. struct usb_device *dev = port->serial->dev;
  331. usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
  332. val, reg, NULL, 0, MOS_WDR_TIMEOUT);
  333. }
  334. static void mos7840_led_off(struct timer_list *t)
  335. {
  336. struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
  337. /* Turn off LED */
  338. mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
  339. mod_timer(&mcs->led_timer2,
  340. jiffies + msecs_to_jiffies(LED_OFF_MS));
  341. }
  342. static void mos7840_led_flag_off(struct timer_list *t)
  343. {
  344. struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
  345. clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
  346. }
  347. static void mos7840_led_activity(struct usb_serial_port *port)
  348. {
  349. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  350. if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
  351. return;
  352. mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
  353. mod_timer(&mos7840_port->led_timer1,
  354. jiffies + msecs_to_jiffies(LED_ON_MS));
  355. }
  356. /*****************************************************************************
  357. * mos7840_bulk_in_callback
  358. * this is the callback function for when we have received data on the
  359. * bulk in endpoint.
  360. *****************************************************************************/
  361. static void mos7840_bulk_in_callback(struct urb *urb)
  362. {
  363. struct moschip_port *mos7840_port = urb->context;
  364. struct usb_serial_port *port = mos7840_port->port;
  365. int retval;
  366. unsigned char *data;
  367. int status = urb->status;
  368. if (status) {
  369. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  370. mos7840_port->read_urb_busy = false;
  371. return;
  372. }
  373. data = urb->transfer_buffer;
  374. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  375. if (urb->actual_length) {
  376. struct tty_port *tport = &mos7840_port->port->port;
  377. tty_insert_flip_string(tport, data, urb->actual_length);
  378. tty_flip_buffer_push(tport);
  379. port->icount.rx += urb->actual_length;
  380. dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
  381. }
  382. if (mos7840_port->has_led)
  383. mos7840_led_activity(port);
  384. mos7840_port->read_urb_busy = true;
  385. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  386. if (retval) {
  387. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  388. mos7840_port->read_urb_busy = false;
  389. }
  390. }
  391. /*****************************************************************************
  392. * mos7840_bulk_out_data_callback
  393. * this is the callback function for when we have finished sending
  394. * serial data on the bulk out endpoint.
  395. *****************************************************************************/
  396. static void mos7840_bulk_out_data_callback(struct urb *urb)
  397. {
  398. struct moschip_port *mos7840_port = urb->context;
  399. struct usb_serial_port *port = mos7840_port->port;
  400. int status = urb->status;
  401. unsigned long flags;
  402. int i;
  403. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  404. for (i = 0; i < NUM_URBS; i++) {
  405. if (urb == mos7840_port->write_urb_pool[i]) {
  406. mos7840_port->busy[i] = 0;
  407. break;
  408. }
  409. }
  410. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  411. if (status) {
  412. dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
  413. return;
  414. }
  415. tty_port_tty_wakeup(&port->port);
  416. }
  417. /************************************************************************/
  418. /* 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 */
  419. /************************************************************************/
  420. /*****************************************************************************
  421. * mos7840_open
  422. * this function is called by the tty driver when a port is opened
  423. * If successful, we return 0
  424. * Otherwise we return a negative error number.
  425. *****************************************************************************/
  426. static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
  427. {
  428. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  429. struct usb_serial *serial = port->serial;
  430. int response;
  431. int j;
  432. struct urb *urb;
  433. __u16 Data;
  434. int status;
  435. usb_clear_halt(serial->dev, port->write_urb->pipe);
  436. usb_clear_halt(serial->dev, port->read_urb->pipe);
  437. /* Initialising the write urb pool */
  438. for (j = 0; j < NUM_URBS; ++j) {
  439. urb = usb_alloc_urb(0, GFP_KERNEL);
  440. mos7840_port->write_urb_pool[j] = urb;
  441. if (!urb)
  442. continue;
  443. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  444. GFP_KERNEL);
  445. if (!urb->transfer_buffer) {
  446. usb_free_urb(urb);
  447. mos7840_port->write_urb_pool[j] = NULL;
  448. continue;
  449. }
  450. }
  451. /*****************************************************************************
  452. * Initialize MCS7840 -- Write Init values to corresponding Registers
  453. *
  454. * Register Index
  455. * 1 : IER
  456. * 2 : FCR
  457. * 3 : LCR
  458. * 4 : MCR
  459. *
  460. * 0x08 : SP1/2 Control Reg
  461. *****************************************************************************/
  462. /* NEED to check the following Block */
  463. Data = 0x0;
  464. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  465. if (status < 0) {
  466. dev_dbg(&port->dev, "Reading Spreg failed\n");
  467. goto err;
  468. }
  469. Data |= 0x80;
  470. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  471. if (status < 0) {
  472. dev_dbg(&port->dev, "writing Spreg failed\n");
  473. goto err;
  474. }
  475. Data &= ~0x80;
  476. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  477. if (status < 0) {
  478. dev_dbg(&port->dev, "writing Spreg failed\n");
  479. goto err;
  480. }
  481. /* End of block to be checked */
  482. Data = 0x0;
  483. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  484. &Data);
  485. if (status < 0) {
  486. dev_dbg(&port->dev, "Reading Controlreg failed\n");
  487. goto err;
  488. }
  489. Data |= 0x08; /* Driver done bit */
  490. Data |= 0x20; /* rx_disable */
  491. status = mos7840_set_reg_sync(port,
  492. mos7840_port->ControlRegOffset, Data);
  493. if (status < 0) {
  494. dev_dbg(&port->dev, "writing Controlreg failed\n");
  495. goto err;
  496. }
  497. /* do register settings here */
  498. /* Set all regs to the device default values. */
  499. /***********************************
  500. * First Disable all interrupts.
  501. ***********************************/
  502. Data = 0x00;
  503. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  504. if (status < 0) {
  505. dev_dbg(&port->dev, "disabling interrupts failed\n");
  506. goto err;
  507. }
  508. /* Set FIFO_CONTROL_REGISTER to the default value */
  509. Data = 0x00;
  510. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  511. if (status < 0) {
  512. dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
  513. goto err;
  514. }
  515. Data = 0xcf;
  516. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  517. if (status < 0) {
  518. dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
  519. goto err;
  520. }
  521. Data = 0x03;
  522. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  523. mos7840_port->shadowLCR = Data;
  524. Data = 0x0b;
  525. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  526. mos7840_port->shadowMCR = Data;
  527. Data = 0x00;
  528. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  529. mos7840_port->shadowLCR = Data;
  530. Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
  531. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  532. Data = 0x0c;
  533. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  534. Data = 0x0;
  535. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  536. Data = 0x00;
  537. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  538. Data = Data & ~SERIAL_LCR_DLAB;
  539. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  540. mos7840_port->shadowLCR = Data;
  541. /* clearing Bulkin and Bulkout Fifo */
  542. Data = 0x0;
  543. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  544. Data = Data | 0x0c;
  545. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  546. Data = Data & ~0x0c;
  547. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  548. /* Finally enable all interrupts */
  549. Data = 0x0c;
  550. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  551. /* clearing rx_disable */
  552. Data = 0x0;
  553. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  554. &Data);
  555. Data = Data & ~0x20;
  556. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  557. Data);
  558. /* rx_negate */
  559. Data = 0x0;
  560. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  561. &Data);
  562. Data = Data | 0x10;
  563. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  564. Data);
  565. dev_dbg(&port->dev, "port number is %d\n", port->port_number);
  566. dev_dbg(&port->dev, "minor number is %d\n", port->minor);
  567. dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
  568. dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
  569. dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
  570. dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
  571. mos7840_port->read_urb = port->read_urb;
  572. /* set up our bulk in urb */
  573. if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
  574. usb_fill_bulk_urb(mos7840_port->read_urb,
  575. serial->dev,
  576. usb_rcvbulkpipe(serial->dev,
  577. (port->bulk_in_endpointAddress) + 2),
  578. port->bulk_in_buffer,
  579. mos7840_port->read_urb->transfer_buffer_length,
  580. mos7840_bulk_in_callback, mos7840_port);
  581. } else {
  582. usb_fill_bulk_urb(mos7840_port->read_urb,
  583. serial->dev,
  584. usb_rcvbulkpipe(serial->dev,
  585. port->bulk_in_endpointAddress),
  586. port->bulk_in_buffer,
  587. mos7840_port->read_urb->transfer_buffer_length,
  588. mos7840_bulk_in_callback, mos7840_port);
  589. }
  590. dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
  591. mos7840_port->read_urb_busy = true;
  592. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  593. if (response) {
  594. dev_err(&port->dev, "%s - Error %d submitting control urb\n",
  595. __func__, response);
  596. mos7840_port->read_urb_busy = false;
  597. }
  598. /* initialize our port settings */
  599. /* Must set to enable ints! */
  600. mos7840_port->shadowMCR = MCR_MASTER_IE;
  601. return 0;
  602. err:
  603. for (j = 0; j < NUM_URBS; ++j) {
  604. urb = mos7840_port->write_urb_pool[j];
  605. if (!urb)
  606. continue;
  607. kfree(urb->transfer_buffer);
  608. usb_free_urb(urb);
  609. }
  610. return status;
  611. }
  612. /*****************************************************************************
  613. * mos7840_chars_in_buffer
  614. * this function is called by the tty driver when it wants to know how many
  615. * bytes of data we currently have outstanding in the port (data that has
  616. * been written, but hasn't made it out the port yet)
  617. *****************************************************************************/
  618. static unsigned int mos7840_chars_in_buffer(struct tty_struct *tty)
  619. {
  620. struct usb_serial_port *port = tty->driver_data;
  621. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  622. int i;
  623. unsigned int chars = 0;
  624. unsigned long flags;
  625. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  626. for (i = 0; i < NUM_URBS; ++i) {
  627. if (mos7840_port->busy[i]) {
  628. struct urb *urb = mos7840_port->write_urb_pool[i];
  629. chars += urb->transfer_buffer_length;
  630. }
  631. }
  632. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  633. dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
  634. return chars;
  635. }
  636. /*****************************************************************************
  637. * mos7840_close
  638. * this function is called by the tty driver when a port is closed
  639. *****************************************************************************/
  640. static void mos7840_close(struct usb_serial_port *port)
  641. {
  642. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  643. int j;
  644. __u16 Data;
  645. for (j = 0; j < NUM_URBS; ++j)
  646. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  647. /* Freeing Write URBs */
  648. for (j = 0; j < NUM_URBS; ++j) {
  649. if (mos7840_port->write_urb_pool[j]) {
  650. kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
  651. usb_free_urb(mos7840_port->write_urb_pool[j]);
  652. }
  653. }
  654. usb_kill_urb(mos7840_port->read_urb);
  655. mos7840_port->read_urb_busy = false;
  656. Data = 0x0;
  657. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  658. Data = 0x00;
  659. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  660. }
  661. /*****************************************************************************
  662. * mos7840_break
  663. * this function sends a break to the port
  664. *****************************************************************************/
  665. static void mos7840_break(struct tty_struct *tty, int break_state)
  666. {
  667. struct usb_serial_port *port = tty->driver_data;
  668. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  669. unsigned char data;
  670. if (break_state == -1)
  671. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  672. else
  673. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  674. /* FIXME: no locking on shadowLCR anywhere in driver */
  675. mos7840_port->shadowLCR = data;
  676. dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
  677. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  678. mos7840_port->shadowLCR);
  679. }
  680. /*****************************************************************************
  681. * mos7840_write_room
  682. * this function is called by the tty driver when it wants to know how many
  683. * bytes of data we can accept for a specific port.
  684. *****************************************************************************/
  685. static unsigned int mos7840_write_room(struct tty_struct *tty)
  686. {
  687. struct usb_serial_port *port = tty->driver_data;
  688. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  689. int i;
  690. unsigned int room = 0;
  691. unsigned long flags;
  692. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  693. for (i = 0; i < NUM_URBS; ++i) {
  694. if (!mos7840_port->busy[i])
  695. room += URB_TRANSFER_BUFFER_SIZE;
  696. }
  697. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  698. room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
  699. dev_dbg(&mos7840_port->port->dev, "%s - returns %u\n", __func__, room);
  700. return room;
  701. }
  702. /*****************************************************************************
  703. * mos7840_write
  704. * this function is called by the tty driver when data should be written to
  705. * the port.
  706. * If successful, we return the number of bytes written, otherwise we
  707. * return a negative error number.
  708. *****************************************************************************/
  709. static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
  710. const unsigned char *data, int count)
  711. {
  712. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  713. struct usb_serial *serial = port->serial;
  714. int status;
  715. int i;
  716. int bytes_sent = 0;
  717. int transfer_size;
  718. unsigned long flags;
  719. struct urb *urb;
  720. /* __u16 Data; */
  721. const unsigned char *current_position = data;
  722. /* try to find a free urb in the list */
  723. urb = NULL;
  724. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  725. for (i = 0; i < NUM_URBS; ++i) {
  726. if (!mos7840_port->busy[i]) {
  727. mos7840_port->busy[i] = 1;
  728. urb = mos7840_port->write_urb_pool[i];
  729. dev_dbg(&port->dev, "URB:%d\n", i);
  730. break;
  731. }
  732. }
  733. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  734. if (urb == NULL) {
  735. dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
  736. goto exit;
  737. }
  738. if (urb->transfer_buffer == NULL) {
  739. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  740. GFP_ATOMIC);
  741. if (!urb->transfer_buffer) {
  742. bytes_sent = -ENOMEM;
  743. goto exit;
  744. }
  745. }
  746. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  747. memcpy(urb->transfer_buffer, current_position, transfer_size);
  748. /* fill urb with data and submit */
  749. if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
  750. usb_fill_bulk_urb(urb,
  751. serial->dev,
  752. usb_sndbulkpipe(serial->dev,
  753. (port->bulk_out_endpointAddress) + 2),
  754. urb->transfer_buffer,
  755. transfer_size,
  756. mos7840_bulk_out_data_callback, mos7840_port);
  757. } else {
  758. usb_fill_bulk_urb(urb,
  759. serial->dev,
  760. usb_sndbulkpipe(serial->dev,
  761. port->bulk_out_endpointAddress),
  762. urb->transfer_buffer,
  763. transfer_size,
  764. mos7840_bulk_out_data_callback, mos7840_port);
  765. }
  766. dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
  767. if (mos7840_port->has_led)
  768. mos7840_led_activity(port);
  769. /* send it down the pipe */
  770. status = usb_submit_urb(urb, GFP_ATOMIC);
  771. if (status) {
  772. mos7840_port->busy[i] = 0;
  773. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  774. "with status = %d\n", __func__, status);
  775. bytes_sent = status;
  776. goto exit;
  777. }
  778. bytes_sent = transfer_size;
  779. port->icount.tx += transfer_size;
  780. dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
  781. exit:
  782. return bytes_sent;
  783. }
  784. /*****************************************************************************
  785. * mos7840_throttle
  786. * this function is called by the tty driver when it wants to stop the data
  787. * being read from the port.
  788. *****************************************************************************/
  789. static void mos7840_throttle(struct tty_struct *tty)
  790. {
  791. struct usb_serial_port *port = tty->driver_data;
  792. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  793. int status;
  794. /* if we are implementing XON/XOFF, send the stop character */
  795. if (I_IXOFF(tty)) {
  796. unsigned char stop_char = STOP_CHAR(tty);
  797. status = mos7840_write(tty, port, &stop_char, 1);
  798. if (status <= 0)
  799. return;
  800. }
  801. /* if we are implementing RTS/CTS, toggle that line */
  802. if (C_CRTSCTS(tty)) {
  803. mos7840_port->shadowMCR &= ~MCR_RTS;
  804. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  805. mos7840_port->shadowMCR);
  806. if (status < 0)
  807. return;
  808. }
  809. }
  810. /*****************************************************************************
  811. * mos7840_unthrottle
  812. * this function is called by the tty driver when it wants to resume
  813. * the data being read from the port (called after mos7840_throttle is
  814. * called)
  815. *****************************************************************************/
  816. static void mos7840_unthrottle(struct tty_struct *tty)
  817. {
  818. struct usb_serial_port *port = tty->driver_data;
  819. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  820. int status;
  821. /* if we are implementing XON/XOFF, send the start character */
  822. if (I_IXOFF(tty)) {
  823. unsigned char start_char = START_CHAR(tty);
  824. status = mos7840_write(tty, port, &start_char, 1);
  825. if (status <= 0)
  826. return;
  827. }
  828. /* if we are implementing RTS/CTS, toggle that line */
  829. if (C_CRTSCTS(tty)) {
  830. mos7840_port->shadowMCR |= MCR_RTS;
  831. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  832. mos7840_port->shadowMCR);
  833. if (status < 0)
  834. return;
  835. }
  836. }
  837. static int mos7840_tiocmget(struct tty_struct *tty)
  838. {
  839. struct usb_serial_port *port = tty->driver_data;
  840. unsigned int result;
  841. __u16 msr;
  842. __u16 mcr;
  843. int status;
  844. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  845. if (status < 0)
  846. return -EIO;
  847. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  848. if (status < 0)
  849. return -EIO;
  850. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  851. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  852. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  853. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  854. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  855. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  856. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  857. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  858. return result;
  859. }
  860. static int mos7840_tiocmset(struct tty_struct *tty,
  861. unsigned int set, unsigned int clear)
  862. {
  863. struct usb_serial_port *port = tty->driver_data;
  864. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  865. unsigned int mcr;
  866. int status;
  867. /* FIXME: What locks the port registers ? */
  868. mcr = mos7840_port->shadowMCR;
  869. if (clear & TIOCM_RTS)
  870. mcr &= ~MCR_RTS;
  871. if (clear & TIOCM_DTR)
  872. mcr &= ~MCR_DTR;
  873. if (clear & TIOCM_LOOP)
  874. mcr &= ~MCR_LOOPBACK;
  875. if (set & TIOCM_RTS)
  876. mcr |= MCR_RTS;
  877. if (set & TIOCM_DTR)
  878. mcr |= MCR_DTR;
  879. if (set & TIOCM_LOOP)
  880. mcr |= MCR_LOOPBACK;
  881. mos7840_port->shadowMCR = mcr;
  882. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  883. if (status < 0) {
  884. dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
  885. return status;
  886. }
  887. return 0;
  888. }
  889. /*****************************************************************************
  890. * mos7840_calc_baud_rate_divisor
  891. * this function calculates the proper baud rate divisor for the specified
  892. * baud rate.
  893. *****************************************************************************/
  894. static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
  895. int baudRate, int *divisor,
  896. __u16 *clk_sel_val)
  897. {
  898. dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
  899. if (baudRate <= 115200) {
  900. *divisor = 115200 / baudRate;
  901. *clk_sel_val = 0x0;
  902. }
  903. if ((baudRate > 115200) && (baudRate <= 230400)) {
  904. *divisor = 230400 / baudRate;
  905. *clk_sel_val = 0x10;
  906. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  907. *divisor = 403200 / baudRate;
  908. *clk_sel_val = 0x20;
  909. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  910. *divisor = 460800 / baudRate;
  911. *clk_sel_val = 0x30;
  912. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  913. *divisor = 806400 / baudRate;
  914. *clk_sel_val = 0x40;
  915. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  916. *divisor = 921600 / baudRate;
  917. *clk_sel_val = 0x50;
  918. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  919. *divisor = 1572864 / baudRate;
  920. *clk_sel_val = 0x60;
  921. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  922. *divisor = 3145728 / baudRate;
  923. *clk_sel_val = 0x70;
  924. }
  925. return 0;
  926. }
  927. /*****************************************************************************
  928. * mos7840_send_cmd_write_baud_rate
  929. * this function sends the proper command to change the baud rate of the
  930. * specified port.
  931. *****************************************************************************/
  932. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  933. int baudRate)
  934. {
  935. struct usb_serial_port *port = mos7840_port->port;
  936. int divisor = 0;
  937. int status;
  938. __u16 Data;
  939. __u16 clk_sel_val;
  940. dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
  941. /* reset clk_uart_sel in spregOffset */
  942. if (baudRate > 115200) {
  943. #ifdef HW_flow_control
  944. /* NOTE: need to see the pther register to modify */
  945. /* setting h/w flow control bit to 1 */
  946. Data = 0x2b;
  947. mos7840_port->shadowMCR = Data;
  948. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  949. Data);
  950. if (status < 0) {
  951. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  952. return -1;
  953. }
  954. #endif
  955. } else {
  956. #ifdef HW_flow_control
  957. /* setting h/w flow control bit to 0 */
  958. Data = 0xb;
  959. mos7840_port->shadowMCR = Data;
  960. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  961. Data);
  962. if (status < 0) {
  963. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  964. return -1;
  965. }
  966. #endif
  967. }
  968. if (1) { /* baudRate <= 115200) */
  969. clk_sel_val = 0x0;
  970. Data = 0x0;
  971. status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
  972. &clk_sel_val);
  973. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  974. &Data);
  975. if (status < 0) {
  976. dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
  977. return -1;
  978. }
  979. Data = (Data & 0x8f) | clk_sel_val;
  980. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
  981. Data);
  982. if (status < 0) {
  983. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  984. return -1;
  985. }
  986. /* Calculate the Divisor */
  987. if (status) {
  988. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  989. return status;
  990. }
  991. /* Enable access to divisor latch */
  992. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  993. mos7840_port->shadowLCR = Data;
  994. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  995. /* Write the divisor */
  996. Data = (unsigned char)(divisor & 0xff);
  997. dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
  998. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  999. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1000. dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
  1001. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1002. /* Disable access to divisor latch */
  1003. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1004. mos7840_port->shadowLCR = Data;
  1005. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1006. }
  1007. return status;
  1008. }
  1009. /*****************************************************************************
  1010. * mos7840_change_port_settings
  1011. * This routine is called to set the UART on the device to match
  1012. * the specified new settings.
  1013. *****************************************************************************/
  1014. static void mos7840_change_port_settings(struct tty_struct *tty,
  1015. struct moschip_port *mos7840_port,
  1016. const struct ktermios *old_termios)
  1017. {
  1018. struct usb_serial_port *port = mos7840_port->port;
  1019. int baud;
  1020. unsigned cflag;
  1021. __u8 lData;
  1022. __u8 lParity;
  1023. __u8 lStop;
  1024. int status;
  1025. __u16 Data;
  1026. lData = LCR_BITS_8;
  1027. lStop = LCR_STOP_1;
  1028. lParity = LCR_PAR_NONE;
  1029. cflag = tty->termios.c_cflag;
  1030. /* Change the number of bits */
  1031. switch (cflag & CSIZE) {
  1032. case CS5:
  1033. lData = LCR_BITS_5;
  1034. break;
  1035. case CS6:
  1036. lData = LCR_BITS_6;
  1037. break;
  1038. case CS7:
  1039. lData = LCR_BITS_7;
  1040. break;
  1041. default:
  1042. case CS8:
  1043. lData = LCR_BITS_8;
  1044. break;
  1045. }
  1046. /* Change the Parity bit */
  1047. if (cflag & PARENB) {
  1048. if (cflag & PARODD) {
  1049. lParity = LCR_PAR_ODD;
  1050. dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
  1051. } else {
  1052. lParity = LCR_PAR_EVEN;
  1053. dev_dbg(&port->dev, "%s - parity = even\n", __func__);
  1054. }
  1055. } else {
  1056. dev_dbg(&port->dev, "%s - parity = none\n", __func__);
  1057. }
  1058. if (cflag & CMSPAR)
  1059. lParity = lParity | 0x20;
  1060. /* Change the Stop bit */
  1061. if (cflag & CSTOPB) {
  1062. lStop = LCR_STOP_2;
  1063. dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
  1064. } else {
  1065. lStop = LCR_STOP_1;
  1066. dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
  1067. }
  1068. /* Update the LCR with the correct value */
  1069. mos7840_port->shadowLCR &=
  1070. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1071. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1072. dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
  1073. mos7840_port->shadowLCR);
  1074. /* Disable Interrupts */
  1075. Data = 0x00;
  1076. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1077. Data = 0x00;
  1078. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1079. Data = 0xcf;
  1080. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1081. /* Send the updated LCR value to the mos7840 */
  1082. Data = mos7840_port->shadowLCR;
  1083. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1084. Data = 0x00b;
  1085. mos7840_port->shadowMCR = Data;
  1086. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1087. Data = 0x00b;
  1088. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1089. /* set up the MCR register and send it to the mos7840 */
  1090. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1091. if (cflag & CBAUD)
  1092. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1093. if (cflag & CRTSCTS)
  1094. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1095. else
  1096. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1097. Data = mos7840_port->shadowMCR;
  1098. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1099. /* Determine divisor based on baud rate */
  1100. baud = tty_get_baud_rate(tty);
  1101. if (!baud) {
  1102. /* pick a default, any default... */
  1103. dev_dbg(&port->dev, "%s", "Picked default baud...\n");
  1104. baud = 9600;
  1105. }
  1106. dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
  1107. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1108. /* Enable Interrupts */
  1109. Data = 0x0c;
  1110. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1111. if (!mos7840_port->read_urb_busy) {
  1112. mos7840_port->read_urb_busy = true;
  1113. status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  1114. if (status) {
  1115. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
  1116. status);
  1117. mos7840_port->read_urb_busy = false;
  1118. }
  1119. }
  1120. dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
  1121. mos7840_port->shadowLCR);
  1122. }
  1123. /*****************************************************************************
  1124. * mos7840_set_termios
  1125. * this function is called by the tty driver when it wants to change
  1126. * the termios structure
  1127. *****************************************************************************/
  1128. static void mos7840_set_termios(struct tty_struct *tty,
  1129. struct usb_serial_port *port,
  1130. const struct ktermios *old_termios)
  1131. {
  1132. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  1133. int status;
  1134. /* change the port settings to the new ones specified */
  1135. mos7840_change_port_settings(tty, mos7840_port, old_termios);
  1136. if (!mos7840_port->read_urb_busy) {
  1137. mos7840_port->read_urb_busy = true;
  1138. status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  1139. if (status) {
  1140. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
  1141. status);
  1142. mos7840_port->read_urb_busy = false;
  1143. }
  1144. }
  1145. }
  1146. /*****************************************************************************
  1147. * mos7840_get_lsr_info - get line status register info
  1148. *
  1149. * Purpose: Let user call ioctl() to get info when the UART physically
  1150. * is emptied. On bus types like RS485, the transmitter must
  1151. * release the bus after transmitting. This must be done when
  1152. * the transmit shift register is empty, not be done when the
  1153. * transmit holding register is empty. This functionality
  1154. * allows an RS485 driver to be written in user space.
  1155. *****************************************************************************/
  1156. static int mos7840_get_lsr_info(struct tty_struct *tty,
  1157. unsigned int __user *value)
  1158. {
  1159. int count;
  1160. unsigned int result = 0;
  1161. count = mos7840_chars_in_buffer(tty);
  1162. if (count == 0)
  1163. result = TIOCSER_TEMT;
  1164. if (copy_to_user(value, &result, sizeof(int)))
  1165. return -EFAULT;
  1166. return 0;
  1167. }
  1168. /*****************************************************************************
  1169. * SerialIoctl
  1170. * this function handles any ioctl calls to the driver
  1171. *****************************************************************************/
  1172. static int mos7840_ioctl(struct tty_struct *tty,
  1173. unsigned int cmd, unsigned long arg)
  1174. {
  1175. struct usb_serial_port *port = tty->driver_data;
  1176. void __user *argp = (void __user *)arg;
  1177. switch (cmd) {
  1178. /* return number of bytes available */
  1179. case TIOCSERGETLSR:
  1180. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1181. return mos7840_get_lsr_info(tty, argp);
  1182. default:
  1183. break;
  1184. }
  1185. return -ENOIOCTLCMD;
  1186. }
  1187. /*
  1188. * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
  1189. * for MCS7810 by bit-banging a 16-bit word.
  1190. *
  1191. * Note that GPO is really RTS of the third port so this will toggle RTS of
  1192. * port two or three on two- and four-port devices.
  1193. */
  1194. static int mos7810_check(struct usb_serial *serial)
  1195. {
  1196. int i, pass_count = 0;
  1197. u8 *buf;
  1198. __u16 data = 0, mcr_data = 0;
  1199. __u16 test_pattern = 0x55AA;
  1200. int res;
  1201. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1202. if (!buf)
  1203. return 0; /* failed to identify 7810 */
  1204. /* Store MCR setting */
  1205. res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1206. MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
  1207. buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1208. if (res == VENDOR_READ_LENGTH)
  1209. mcr_data = *buf;
  1210. for (i = 0; i < 16; i++) {
  1211. /* Send the 1-bit test pattern out to MCS7810 test pin */
  1212. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1213. MCS_WRREQ, MCS_WR_RTYPE,
  1214. (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
  1215. MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
  1216. /* Read the test pattern back */
  1217. res = usb_control_msg(serial->dev,
  1218. usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
  1219. MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1220. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1221. if (res == VENDOR_READ_LENGTH)
  1222. data = *buf;
  1223. /* If this is a MCS7810 device, both test patterns must match */
  1224. if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
  1225. break;
  1226. pass_count++;
  1227. }
  1228. /* Restore MCR setting */
  1229. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
  1230. MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
  1231. 0, MOS_WDR_TIMEOUT);
  1232. kfree(buf);
  1233. if (pass_count == 16)
  1234. return 1;
  1235. return 0;
  1236. }
  1237. static int mos7840_probe(struct usb_serial *serial,
  1238. const struct usb_device_id *id)
  1239. {
  1240. unsigned long device_flags = id->driver_info;
  1241. u8 *buf;
  1242. /* Skip device-type detection if we already have device flags. */
  1243. if (device_flags)
  1244. goto out;
  1245. buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1246. if (!buf)
  1247. return -ENOMEM;
  1248. usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1249. MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1250. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1251. /* For a MCS7840 device GPIO0 must be set to 1 */
  1252. if (buf[0] & 0x01)
  1253. device_flags = MCS_PORTS(4);
  1254. else if (mos7810_check(serial))
  1255. device_flags = MCS_PORTS(1) | MCS_LED;
  1256. else
  1257. device_flags = MCS_PORTS(2);
  1258. kfree(buf);
  1259. out:
  1260. usb_set_serial_data(serial, (void *)device_flags);
  1261. return 0;
  1262. }
  1263. static int mos7840_calc_num_ports(struct usb_serial *serial,
  1264. struct usb_serial_endpoints *epds)
  1265. {
  1266. unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
  1267. int num_ports = MCS_PORTS(device_flags);
  1268. if (num_ports == 0 || num_ports > 4)
  1269. return -ENODEV;
  1270. if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
  1271. dev_err(&serial->interface->dev, "missing endpoints\n");
  1272. return -ENODEV;
  1273. }
  1274. return num_ports;
  1275. }
  1276. static int mos7840_attach(struct usb_serial *serial)
  1277. {
  1278. struct device *dev = &serial->interface->dev;
  1279. int status;
  1280. u16 val;
  1281. /* Zero Length flag enable */
  1282. val = 0x0f;
  1283. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
  1284. if (status < 0)
  1285. dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
  1286. else
  1287. dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
  1288. return status;
  1289. }
  1290. static int mos7840_port_probe(struct usb_serial_port *port)
  1291. {
  1292. struct usb_serial *serial = port->serial;
  1293. unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
  1294. struct moschip_port *mos7840_port;
  1295. int status;
  1296. int pnum;
  1297. __u16 Data;
  1298. /* we set up the pointers to the endpoints in the mos7840_open *
  1299. * function, as the structures aren't created yet. */
  1300. pnum = port->port_number;
  1301. dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
  1302. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1303. if (!mos7840_port)
  1304. return -ENOMEM;
  1305. /* Initialize all port interrupt end point to port 0 int
  1306. * endpoint. Our device has only one interrupt end point
  1307. * common to all port */
  1308. mos7840_port->port = port;
  1309. spin_lock_init(&mos7840_port->pool_lock);
  1310. /* minor is not initialised until later by
  1311. * usb-serial.c:get_free_serial() and cannot therefore be used
  1312. * to index device instances */
  1313. mos7840_port->port_num = pnum + 1;
  1314. dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
  1315. dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
  1316. if (mos7840_port->port_num == 1) {
  1317. mos7840_port->SpRegOffset = 0x0;
  1318. mos7840_port->ControlRegOffset = 0x1;
  1319. mos7840_port->DcrRegOffset = 0x4;
  1320. } else {
  1321. u8 phy_num = mos7840_port->port_num;
  1322. /* Port 2 in the 2-port case uses registers of port 3 */
  1323. if (serial->num_ports == 2)
  1324. phy_num = 3;
  1325. mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
  1326. mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
  1327. mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
  1328. }
  1329. mos7840_dump_serial_port(port, mos7840_port);
  1330. usb_set_serial_port_data(port, mos7840_port);
  1331. /* enable rx_disable bit in control register */
  1332. status = mos7840_get_reg_sync(port,
  1333. mos7840_port->ControlRegOffset, &Data);
  1334. if (status < 0) {
  1335. dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
  1336. goto error;
  1337. } else
  1338. dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
  1339. Data |= 0x08; /* setting driver done bit */
  1340. Data |= 0x04; /* sp1_bit to have cts change reflect in
  1341. modem status reg */
  1342. /* Data |= 0x20; //rx_disable bit */
  1343. status = mos7840_set_reg_sync(port,
  1344. mos7840_port->ControlRegOffset, Data);
  1345. if (status < 0) {
  1346. dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  1347. goto error;
  1348. } else
  1349. dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
  1350. /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
  1351. and 0x24 in DCR3 */
  1352. Data = 0x01;
  1353. status = mos7840_set_reg_sync(port,
  1354. (__u16) (mos7840_port->DcrRegOffset + 0), Data);
  1355. if (status < 0) {
  1356. dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
  1357. goto error;
  1358. } else
  1359. dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
  1360. Data = 0x05;
  1361. status = mos7840_set_reg_sync(port,
  1362. (__u16) (mos7840_port->DcrRegOffset + 1), Data);
  1363. if (status < 0) {
  1364. dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
  1365. goto error;
  1366. } else
  1367. dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
  1368. Data = 0x24;
  1369. status = mos7840_set_reg_sync(port,
  1370. (__u16) (mos7840_port->DcrRegOffset + 2), Data);
  1371. if (status < 0) {
  1372. dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
  1373. goto error;
  1374. } else
  1375. dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
  1376. /* write values in clkstart0x0 and clkmulti 0x20 */
  1377. Data = 0x0;
  1378. status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
  1379. if (status < 0) {
  1380. dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  1381. goto error;
  1382. } else
  1383. dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  1384. Data = 0x20;
  1385. status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
  1386. if (status < 0) {
  1387. dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
  1388. goto error;
  1389. } else
  1390. dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
  1391. /* write value 0x0 to scratchpad register */
  1392. Data = 0x00;
  1393. status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
  1394. if (status < 0) {
  1395. dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
  1396. goto error;
  1397. } else
  1398. dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
  1399. /* Zero Length flag register */
  1400. if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
  1401. Data = 0xff;
  1402. status = mos7840_set_reg_sync(port,
  1403. (__u16) (ZLP_REG1 +
  1404. ((__u16)mos7840_port->port_num)), Data);
  1405. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1406. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
  1407. if (status < 0) {
  1408. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
  1409. goto error;
  1410. } else
  1411. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
  1412. } else {
  1413. Data = 0xff;
  1414. status = mos7840_set_reg_sync(port,
  1415. (__u16) (ZLP_REG1 +
  1416. ((__u16)mos7840_port->port_num) - 0x1), Data);
  1417. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1418. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
  1419. if (status < 0) {
  1420. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
  1421. goto error;
  1422. } else
  1423. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
  1424. }
  1425. mos7840_port->has_led = device_flags & MCS_LED;
  1426. /* Initialize LED timers */
  1427. if (mos7840_port->has_led) {
  1428. mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
  1429. mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
  1430. GFP_KERNEL);
  1431. if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
  1432. status = -ENOMEM;
  1433. goto error;
  1434. }
  1435. timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
  1436. mos7840_port->led_timer1.expires =
  1437. jiffies + msecs_to_jiffies(LED_ON_MS);
  1438. timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
  1439. 0);
  1440. mos7840_port->led_timer2.expires =
  1441. jiffies + msecs_to_jiffies(LED_OFF_MS);
  1442. /* Turn off LED */
  1443. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1444. }
  1445. return 0;
  1446. error:
  1447. kfree(mos7840_port->led_dr);
  1448. usb_free_urb(mos7840_port->led_urb);
  1449. kfree(mos7840_port);
  1450. return status;
  1451. }
  1452. static void mos7840_port_remove(struct usb_serial_port *port)
  1453. {
  1454. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  1455. if (mos7840_port->has_led) {
  1456. /* Turn off LED */
  1457. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1458. del_timer_sync(&mos7840_port->led_timer1);
  1459. del_timer_sync(&mos7840_port->led_timer2);
  1460. usb_kill_urb(mos7840_port->led_urb);
  1461. usb_free_urb(mos7840_port->led_urb);
  1462. kfree(mos7840_port->led_dr);
  1463. }
  1464. kfree(mos7840_port);
  1465. }
  1466. static struct usb_serial_driver moschip7840_4port_device = {
  1467. .driver = {
  1468. .owner = THIS_MODULE,
  1469. .name = "mos7840",
  1470. },
  1471. .description = DRIVER_DESC,
  1472. .id_table = id_table,
  1473. .num_interrupt_in = 1,
  1474. .open = mos7840_open,
  1475. .close = mos7840_close,
  1476. .write = mos7840_write,
  1477. .write_room = mos7840_write_room,
  1478. .chars_in_buffer = mos7840_chars_in_buffer,
  1479. .throttle = mos7840_throttle,
  1480. .unthrottle = mos7840_unthrottle,
  1481. .calc_num_ports = mos7840_calc_num_ports,
  1482. .probe = mos7840_probe,
  1483. .attach = mos7840_attach,
  1484. .ioctl = mos7840_ioctl,
  1485. .set_termios = mos7840_set_termios,
  1486. .break_ctl = mos7840_break,
  1487. .tiocmget = mos7840_tiocmget,
  1488. .tiocmset = mos7840_tiocmset,
  1489. .get_icount = usb_serial_generic_get_icount,
  1490. .port_probe = mos7840_port_probe,
  1491. .port_remove = mos7840_port_remove,
  1492. .read_bulk_callback = mos7840_bulk_in_callback,
  1493. };
  1494. static struct usb_serial_driver * const serial_drivers[] = {
  1495. &moschip7840_4port_device, NULL
  1496. };
  1497. module_usb_serial_driver(serial_drivers, id_table);
  1498. MODULE_DESCRIPTION(DRIVER_DESC);
  1499. MODULE_LICENSE("GPL");