mos7840.c 67 KB

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