mos7840.c 71 KB

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