mos7840.c 71 KB

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