mos7720.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mos7720.c
  4. * Controls the Moschip 7720 usb to dual port serial converter
  5. *
  6. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  7. *
  8. * Developed by:
  9. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  10. * Ajay Kumar <naanuajay@yahoo.com>
  11. * Gurudeva <ngurudeva@yahoo.com>
  12. *
  13. * Cleaned up from the original by:
  14. * Greg Kroah-Hartman <gregkh@suse.de>
  15. *
  16. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  17. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  18. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_driver.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/module.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/serial.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/usb.h>
  31. #include <linux/usb/serial.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/parport.h>
  34. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  35. #define DRIVER_DESC "Moschip USB Serial Driver"
  36. /* default urb timeout */
  37. #define MOS_WDR_TIMEOUT 5000
  38. #define MOS_MAX_PORT 0x02
  39. #define MOS_WRITE 0x0E
  40. #define MOS_READ 0x0D
  41. /* Interrupt Routines Defines */
  42. #define SERIAL_IIR_RLS 0x06
  43. #define SERIAL_IIR_RDA 0x04
  44. #define SERIAL_IIR_CTI 0x0c
  45. #define SERIAL_IIR_THR 0x02
  46. #define SERIAL_IIR_MS 0x00
  47. #define NUM_URBS 16 /* URB Count */
  48. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  49. /* This structure holds all of the local serial port information */
  50. struct moschip_port {
  51. __u8 shadowLCR; /* last LCR value received */
  52. __u8 shadowMCR; /* last MCR value received */
  53. __u8 shadowMSR; /* last MSR value received */
  54. char open;
  55. struct usb_serial_port *port; /* loop back to the owner */
  56. struct urb *write_urb_pool[NUM_URBS];
  57. };
  58. #define USB_VENDOR_ID_MOSCHIP 0x9710
  59. #define MOSCHIP_DEVICE_ID_7720 0x7720
  60. #define MOSCHIP_DEVICE_ID_7715 0x7715
  61. static const struct usb_device_id id_table[] = {
  62. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  63. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  64. { } /* terminating entry */
  65. };
  66. MODULE_DEVICE_TABLE(usb, id_table);
  67. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  68. /* initial values for parport regs */
  69. #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
  70. #define ECR_INIT_VAL 0x00 /* SPP mode */
  71. struct urbtracker {
  72. struct mos7715_parport *mos_parport;
  73. struct list_head urblist_entry;
  74. struct kref ref_count;
  75. struct urb *urb;
  76. struct usb_ctrlrequest *setup;
  77. };
  78. enum mos7715_pp_modes {
  79. SPP = 0<<5,
  80. PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
  81. PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
  82. };
  83. struct mos7715_parport {
  84. struct parport *pp; /* back to containing struct */
  85. struct kref ref_count; /* to instance of this struct */
  86. struct list_head deferred_urbs; /* list deferred async urbs */
  87. struct list_head active_urbs; /* list async urbs in flight */
  88. spinlock_t listlock; /* protects list access */
  89. bool msg_pending; /* usb sync call pending */
  90. struct completion syncmsg_compl; /* usb sync call completed */
  91. struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
  92. struct usb_serial *serial; /* back to containing struct */
  93. __u8 shadowECR; /* parallel port regs... */
  94. __u8 shadowDCR;
  95. atomic_t shadowDSR; /* updated in int-in callback */
  96. };
  97. /* lock guards against dereferencing NULL ptr in parport ops callbacks */
  98. static DEFINE_SPINLOCK(release_lock);
  99. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  100. static const unsigned int dummy; /* for clarity in register access fns */
  101. enum mos_regs {
  102. MOS7720_THR, /* serial port regs */
  103. MOS7720_RHR,
  104. MOS7720_IER,
  105. MOS7720_FCR,
  106. MOS7720_ISR,
  107. MOS7720_LCR,
  108. MOS7720_MCR,
  109. MOS7720_LSR,
  110. MOS7720_MSR,
  111. MOS7720_SPR,
  112. MOS7720_DLL,
  113. MOS7720_DLM,
  114. MOS7720_DPR, /* parallel port regs */
  115. MOS7720_DSR,
  116. MOS7720_DCR,
  117. MOS7720_ECR,
  118. MOS7720_SP1_REG, /* device control regs */
  119. MOS7720_SP2_REG, /* serial port 2 (7720 only) */
  120. MOS7720_PP_REG,
  121. MOS7720_SP_CONTROL_REG,
  122. };
  123. /*
  124. * Return the correct value for the Windex field of the setup packet
  125. * for a control endpoint message. See the 7715 datasheet.
  126. */
  127. static inline __u16 get_reg_index(enum mos_regs reg)
  128. {
  129. static const __u16 mos7715_index_lookup_table[] = {
  130. 0x00, /* MOS7720_THR */
  131. 0x00, /* MOS7720_RHR */
  132. 0x01, /* MOS7720_IER */
  133. 0x02, /* MOS7720_FCR */
  134. 0x02, /* MOS7720_ISR */
  135. 0x03, /* MOS7720_LCR */
  136. 0x04, /* MOS7720_MCR */
  137. 0x05, /* MOS7720_LSR */
  138. 0x06, /* MOS7720_MSR */
  139. 0x07, /* MOS7720_SPR */
  140. 0x00, /* MOS7720_DLL */
  141. 0x01, /* MOS7720_DLM */
  142. 0x00, /* MOS7720_DPR */
  143. 0x01, /* MOS7720_DSR */
  144. 0x02, /* MOS7720_DCR */
  145. 0x0a, /* MOS7720_ECR */
  146. 0x01, /* MOS7720_SP1_REG */
  147. 0x02, /* MOS7720_SP2_REG (7720 only) */
  148. 0x04, /* MOS7720_PP_REG (7715 only) */
  149. 0x08, /* MOS7720_SP_CONTROL_REG */
  150. };
  151. return mos7715_index_lookup_table[reg];
  152. }
  153. /*
  154. * Return the correct value for the upper byte of the Wvalue field of
  155. * the setup packet for a control endpoint message.
  156. */
  157. static inline __u16 get_reg_value(enum mos_regs reg,
  158. unsigned int serial_portnum)
  159. {
  160. if (reg >= MOS7720_SP1_REG) /* control reg */
  161. return 0x0000;
  162. else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
  163. return 0x0100;
  164. else /* serial port reg */
  165. return (serial_portnum + 2) << 8;
  166. }
  167. /*
  168. * Write data byte to the specified device register. The data is embedded in
  169. * the value field of the setup packet. serial_portnum is ignored for registers
  170. * not specific to a particular serial port.
  171. */
  172. static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  173. enum mos_regs reg, __u8 data)
  174. {
  175. struct usb_device *usbdev = serial->dev;
  176. unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
  177. __u8 request = (__u8)0x0e;
  178. __u8 requesttype = (__u8)0x40;
  179. __u16 index = get_reg_index(reg);
  180. __u16 value = get_reg_value(reg, serial_portnum) + data;
  181. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  182. index, NULL, 0, MOS_WDR_TIMEOUT);
  183. if (status < 0)
  184. dev_err(&usbdev->dev,
  185. "mos7720: usb_control_msg() failed: %d\n", status);
  186. return status;
  187. }
  188. /*
  189. * Read data byte from the specified device register. The data returned by the
  190. * device is embedded in the value field of the setup packet. serial_portnum is
  191. * ignored for registers that are not specific to a particular serial port.
  192. */
  193. static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  194. enum mos_regs reg, __u8 *data)
  195. {
  196. struct usb_device *usbdev = serial->dev;
  197. unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
  198. __u8 request = (__u8)0x0d;
  199. __u8 requesttype = (__u8)0xc0;
  200. __u16 index = get_reg_index(reg);
  201. __u16 value = get_reg_value(reg, serial_portnum);
  202. u8 *buf;
  203. int status;
  204. buf = kmalloc(1, GFP_KERNEL);
  205. if (!buf)
  206. return -ENOMEM;
  207. status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  208. index, buf, 1, MOS_WDR_TIMEOUT);
  209. if (status == 1) {
  210. *data = *buf;
  211. } else {
  212. dev_err(&usbdev->dev,
  213. "mos7720: usb_control_msg() failed: %d\n", status);
  214. if (status >= 0)
  215. status = -EIO;
  216. *data = 0;
  217. }
  218. kfree(buf);
  219. return status;
  220. }
  221. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  222. static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
  223. enum mos7715_pp_modes mode)
  224. {
  225. mos_parport->shadowECR = mode;
  226. write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
  227. mos_parport->shadowECR);
  228. return 0;
  229. }
  230. static void destroy_mos_parport(struct kref *kref)
  231. {
  232. struct mos7715_parport *mos_parport =
  233. container_of(kref, struct mos7715_parport, ref_count);
  234. kfree(mos_parport);
  235. }
  236. static void destroy_urbtracker(struct kref *kref)
  237. {
  238. struct urbtracker *urbtrack =
  239. container_of(kref, struct urbtracker, ref_count);
  240. struct mos7715_parport *mos_parport = urbtrack->mos_parport;
  241. usb_free_urb(urbtrack->urb);
  242. kfree(urbtrack->setup);
  243. kfree(urbtrack);
  244. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  245. }
  246. /*
  247. * This runs as a tasklet when sending an urb in a non-blocking parallel
  248. * port callback had to be deferred because the disconnect mutex could not be
  249. * obtained at the time.
  250. */
  251. static void send_deferred_urbs(unsigned long _mos_parport)
  252. {
  253. int ret_val;
  254. unsigned long flags;
  255. struct mos7715_parport *mos_parport = (void *)_mos_parport;
  256. struct urbtracker *urbtrack, *tmp;
  257. struct list_head *cursor, *next;
  258. struct device *dev;
  259. /* if release function ran, game over */
  260. if (unlikely(mos_parport->serial == NULL))
  261. return;
  262. dev = &mos_parport->serial->dev->dev;
  263. /* try again to get the mutex */
  264. if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
  265. dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
  266. tasklet_schedule(&mos_parport->urb_tasklet);
  267. return;
  268. }
  269. /* if device disconnected, game over */
  270. if (unlikely(mos_parport->serial->disconnected)) {
  271. mutex_unlock(&mos_parport->serial->disc_mutex);
  272. return;
  273. }
  274. spin_lock_irqsave(&mos_parport->listlock, flags);
  275. if (list_empty(&mos_parport->deferred_urbs)) {
  276. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  277. mutex_unlock(&mos_parport->serial->disc_mutex);
  278. dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
  279. return;
  280. }
  281. /* move contents of deferred_urbs list to active_urbs list and submit */
  282. list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
  283. list_move_tail(cursor, &mos_parport->active_urbs);
  284. list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
  285. urblist_entry) {
  286. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  287. dev_dbg(dev, "%s: urb submitted\n", __func__);
  288. if (ret_val) {
  289. dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
  290. list_del(&urbtrack->urblist_entry);
  291. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  292. }
  293. }
  294. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  295. mutex_unlock(&mos_parport->serial->disc_mutex);
  296. }
  297. /* callback for parallel port control urbs submitted asynchronously */
  298. static void async_complete(struct urb *urb)
  299. {
  300. struct urbtracker *urbtrack = urb->context;
  301. int status = urb->status;
  302. unsigned long flags;
  303. if (unlikely(status))
  304. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
  305. /* remove the urbtracker from the active_urbs list */
  306. spin_lock_irqsave(&urbtrack->mos_parport->listlock, flags);
  307. list_del(&urbtrack->urblist_entry);
  308. spin_unlock_irqrestore(&urbtrack->mos_parport->listlock, flags);
  309. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  310. }
  311. static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
  312. enum mos_regs reg, __u8 data)
  313. {
  314. struct urbtracker *urbtrack;
  315. int ret_val;
  316. unsigned long flags;
  317. struct usb_serial *serial = mos_parport->serial;
  318. struct usb_device *usbdev = serial->dev;
  319. /* create and initialize the control urb and containing urbtracker */
  320. urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
  321. if (!urbtrack)
  322. return -ENOMEM;
  323. urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
  324. if (!urbtrack->urb) {
  325. kfree(urbtrack);
  326. return -ENOMEM;
  327. }
  328. urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
  329. if (!urbtrack->setup) {
  330. usb_free_urb(urbtrack->urb);
  331. kfree(urbtrack);
  332. return -ENOMEM;
  333. }
  334. urbtrack->setup->bRequestType = (__u8)0x40;
  335. urbtrack->setup->bRequest = (__u8)0x0e;
  336. urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
  337. urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
  338. urbtrack->setup->wLength = 0;
  339. usb_fill_control_urb(urbtrack->urb, usbdev,
  340. usb_sndctrlpipe(usbdev, 0),
  341. (unsigned char *)urbtrack->setup,
  342. NULL, 0, async_complete, urbtrack);
  343. kref_get(&mos_parport->ref_count);
  344. urbtrack->mos_parport = mos_parport;
  345. kref_init(&urbtrack->ref_count);
  346. INIT_LIST_HEAD(&urbtrack->urblist_entry);
  347. /*
  348. * get the disconnect mutex, or add tracker to the deferred_urbs list
  349. * and schedule a tasklet to try again later
  350. */
  351. if (!mutex_trylock(&serial->disc_mutex)) {
  352. spin_lock_irqsave(&mos_parport->listlock, flags);
  353. list_add_tail(&urbtrack->urblist_entry,
  354. &mos_parport->deferred_urbs);
  355. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  356. tasklet_schedule(&mos_parport->urb_tasklet);
  357. dev_dbg(&usbdev->dev, "tasklet scheduled\n");
  358. return 0;
  359. }
  360. /* bail if device disconnected */
  361. if (serial->disconnected) {
  362. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  363. mutex_unlock(&serial->disc_mutex);
  364. return -ENODEV;
  365. }
  366. /* add the tracker to the active_urbs list and submit */
  367. spin_lock_irqsave(&mos_parport->listlock, flags);
  368. list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
  369. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  370. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  371. mutex_unlock(&serial->disc_mutex);
  372. if (ret_val) {
  373. dev_err(&usbdev->dev,
  374. "%s: submit_urb() failed: %d\n", __func__, ret_val);
  375. spin_lock_irqsave(&mos_parport->listlock, flags);
  376. list_del(&urbtrack->urblist_entry);
  377. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  378. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  379. return ret_val;
  380. }
  381. return 0;
  382. }
  383. /*
  384. * This is the the common top part of all parallel port callback operations that
  385. * send synchronous messages to the device. This implements convoluted locking
  386. * that avoids two scenarios: (1) a port operation is called after usbserial
  387. * has called our release function, at which point struct mos7715_parport has
  388. * been destroyed, and (2) the device has been disconnected, but usbserial has
  389. * not called the release function yet because someone has a serial port open.
  390. * The shared release_lock prevents the first, and the mutex and disconnected
  391. * flag maintained by usbserial covers the second. We also use the msg_pending
  392. * flag to ensure that all synchronous usb message calls have completed before
  393. * our release function can return.
  394. */
  395. static int parport_prologue(struct parport *pp)
  396. {
  397. struct mos7715_parport *mos_parport;
  398. spin_lock(&release_lock);
  399. mos_parport = pp->private_data;
  400. if (unlikely(mos_parport == NULL)) {
  401. /* release fn called, port struct destroyed */
  402. spin_unlock(&release_lock);
  403. return -1;
  404. }
  405. mos_parport->msg_pending = true; /* synch usb call pending */
  406. reinit_completion(&mos_parport->syncmsg_compl);
  407. spin_unlock(&release_lock);
  408. mutex_lock(&mos_parport->serial->disc_mutex);
  409. if (mos_parport->serial->disconnected) {
  410. /* device disconnected */
  411. mutex_unlock(&mos_parport->serial->disc_mutex);
  412. mos_parport->msg_pending = false;
  413. complete(&mos_parport->syncmsg_compl);
  414. return -1;
  415. }
  416. return 0;
  417. }
  418. /*
  419. * This is the common bottom part of all parallel port functions that send
  420. * synchronous messages to the device.
  421. */
  422. static inline void parport_epilogue(struct parport *pp)
  423. {
  424. struct mos7715_parport *mos_parport = pp->private_data;
  425. mutex_unlock(&mos_parport->serial->disc_mutex);
  426. mos_parport->msg_pending = false;
  427. complete(&mos_parport->syncmsg_compl);
  428. }
  429. static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
  430. {
  431. struct mos7715_parport *mos_parport = pp->private_data;
  432. if (parport_prologue(pp) < 0)
  433. return;
  434. mos7715_change_mode(mos_parport, SPP);
  435. write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
  436. parport_epilogue(pp);
  437. }
  438. static unsigned char parport_mos7715_read_data(struct parport *pp)
  439. {
  440. struct mos7715_parport *mos_parport = pp->private_data;
  441. unsigned char d;
  442. if (parport_prologue(pp) < 0)
  443. return 0;
  444. read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
  445. parport_epilogue(pp);
  446. return d;
  447. }
  448. static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
  449. {
  450. struct mos7715_parport *mos_parport = pp->private_data;
  451. __u8 data;
  452. if (parport_prologue(pp) < 0)
  453. return;
  454. data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
  455. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
  456. mos_parport->shadowDCR = data;
  457. parport_epilogue(pp);
  458. }
  459. static unsigned char parport_mos7715_read_control(struct parport *pp)
  460. {
  461. struct mos7715_parport *mos_parport;
  462. __u8 dcr;
  463. spin_lock(&release_lock);
  464. mos_parport = pp->private_data;
  465. if (unlikely(mos_parport == NULL)) {
  466. spin_unlock(&release_lock);
  467. return 0;
  468. }
  469. dcr = mos_parport->shadowDCR & 0x0f;
  470. spin_unlock(&release_lock);
  471. return dcr;
  472. }
  473. static unsigned char parport_mos7715_frob_control(struct parport *pp,
  474. unsigned char mask,
  475. unsigned char val)
  476. {
  477. struct mos7715_parport *mos_parport = pp->private_data;
  478. __u8 dcr;
  479. mask &= 0x0f;
  480. val &= 0x0f;
  481. if (parport_prologue(pp) < 0)
  482. return 0;
  483. mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
  484. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  485. mos_parport->shadowDCR);
  486. dcr = mos_parport->shadowDCR & 0x0f;
  487. parport_epilogue(pp);
  488. return dcr;
  489. }
  490. static unsigned char parport_mos7715_read_status(struct parport *pp)
  491. {
  492. unsigned char status;
  493. struct mos7715_parport *mos_parport;
  494. spin_lock(&release_lock);
  495. mos_parport = pp->private_data;
  496. if (unlikely(mos_parport == NULL)) { /* release called */
  497. spin_unlock(&release_lock);
  498. return 0;
  499. }
  500. status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
  501. spin_unlock(&release_lock);
  502. return status;
  503. }
  504. static void parport_mos7715_enable_irq(struct parport *pp)
  505. {
  506. }
  507. static void parport_mos7715_disable_irq(struct parport *pp)
  508. {
  509. }
  510. static void parport_mos7715_data_forward(struct parport *pp)
  511. {
  512. struct mos7715_parport *mos_parport = pp->private_data;
  513. if (parport_prologue(pp) < 0)
  514. return;
  515. mos7715_change_mode(mos_parport, PS2);
  516. mos_parport->shadowDCR &= ~0x20;
  517. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  518. mos_parport->shadowDCR);
  519. parport_epilogue(pp);
  520. }
  521. static void parport_mos7715_data_reverse(struct parport *pp)
  522. {
  523. struct mos7715_parport *mos_parport = pp->private_data;
  524. if (parport_prologue(pp) < 0)
  525. return;
  526. mos7715_change_mode(mos_parport, PS2);
  527. mos_parport->shadowDCR |= 0x20;
  528. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  529. mos_parport->shadowDCR);
  530. parport_epilogue(pp);
  531. }
  532. static void parport_mos7715_init_state(struct pardevice *dev,
  533. struct parport_state *s)
  534. {
  535. s->u.pc.ctr = DCR_INIT_VAL;
  536. s->u.pc.ecr = ECR_INIT_VAL;
  537. }
  538. /* N.B. Parport core code requires that this function not block */
  539. static void parport_mos7715_save_state(struct parport *pp,
  540. struct parport_state *s)
  541. {
  542. struct mos7715_parport *mos_parport;
  543. spin_lock(&release_lock);
  544. mos_parport = pp->private_data;
  545. if (unlikely(mos_parport == NULL)) { /* release called */
  546. spin_unlock(&release_lock);
  547. return;
  548. }
  549. s->u.pc.ctr = mos_parport->shadowDCR;
  550. s->u.pc.ecr = mos_parport->shadowECR;
  551. spin_unlock(&release_lock);
  552. }
  553. /* N.B. Parport core code requires that this function not block */
  554. static void parport_mos7715_restore_state(struct parport *pp,
  555. struct parport_state *s)
  556. {
  557. struct mos7715_parport *mos_parport;
  558. spin_lock(&release_lock);
  559. mos_parport = pp->private_data;
  560. if (unlikely(mos_parport == NULL)) { /* release called */
  561. spin_unlock(&release_lock);
  562. return;
  563. }
  564. mos_parport->shadowDCR = s->u.pc.ctr;
  565. mos_parport->shadowECR = s->u.pc.ecr;
  566. write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
  567. mos_parport->shadowDCR);
  568. write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
  569. mos_parport->shadowECR);
  570. spin_unlock(&release_lock);
  571. }
  572. static size_t parport_mos7715_write_compat(struct parport *pp,
  573. const void *buffer,
  574. size_t len, int flags)
  575. {
  576. int retval;
  577. struct mos7715_parport *mos_parport = pp->private_data;
  578. int actual_len;
  579. if (parport_prologue(pp) < 0)
  580. return 0;
  581. mos7715_change_mode(mos_parport, PPF);
  582. retval = usb_bulk_msg(mos_parport->serial->dev,
  583. usb_sndbulkpipe(mos_parport->serial->dev, 2),
  584. (void *)buffer, len, &actual_len,
  585. MOS_WDR_TIMEOUT);
  586. parport_epilogue(pp);
  587. if (retval) {
  588. dev_err(&mos_parport->serial->dev->dev,
  589. "mos7720: usb_bulk_msg() failed: %d\n", retval);
  590. return 0;
  591. }
  592. return actual_len;
  593. }
  594. static struct parport_operations parport_mos7715_ops = {
  595. .owner = THIS_MODULE,
  596. .write_data = parport_mos7715_write_data,
  597. .read_data = parport_mos7715_read_data,
  598. .write_control = parport_mos7715_write_control,
  599. .read_control = parport_mos7715_read_control,
  600. .frob_control = parport_mos7715_frob_control,
  601. .read_status = parport_mos7715_read_status,
  602. .enable_irq = parport_mos7715_enable_irq,
  603. .disable_irq = parport_mos7715_disable_irq,
  604. .data_forward = parport_mos7715_data_forward,
  605. .data_reverse = parport_mos7715_data_reverse,
  606. .init_state = parport_mos7715_init_state,
  607. .save_state = parport_mos7715_save_state,
  608. .restore_state = parport_mos7715_restore_state,
  609. .compat_write_data = parport_mos7715_write_compat,
  610. .nibble_read_data = parport_ieee1284_read_nibble,
  611. .byte_read_data = parport_ieee1284_read_byte,
  612. };
  613. /*
  614. * Allocate and initialize parallel port control struct, initialize
  615. * the parallel port hardware device, and register with the parport subsystem.
  616. */
  617. static int mos7715_parport_init(struct usb_serial *serial)
  618. {
  619. struct mos7715_parport *mos_parport;
  620. /* allocate and initialize parallel port control struct */
  621. mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
  622. if (!mos_parport)
  623. return -ENOMEM;
  624. mos_parport->msg_pending = false;
  625. kref_init(&mos_parport->ref_count);
  626. spin_lock_init(&mos_parport->listlock);
  627. INIT_LIST_HEAD(&mos_parport->active_urbs);
  628. INIT_LIST_HEAD(&mos_parport->deferred_urbs);
  629. usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
  630. mos_parport->serial = serial;
  631. tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
  632. (unsigned long) mos_parport);
  633. init_completion(&mos_parport->syncmsg_compl);
  634. /* cycle parallel port reset bit */
  635. write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
  636. write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
  637. /* initialize device registers */
  638. mos_parport->shadowDCR = DCR_INIT_VAL;
  639. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  640. mos_parport->shadowDCR);
  641. mos_parport->shadowECR = ECR_INIT_VAL;
  642. write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
  643. mos_parport->shadowECR);
  644. /* register with parport core */
  645. mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
  646. PARPORT_DMA_NONE,
  647. &parport_mos7715_ops);
  648. if (mos_parport->pp == NULL) {
  649. dev_err(&serial->interface->dev,
  650. "Could not register parport\n");
  651. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  652. return -EIO;
  653. }
  654. mos_parport->pp->private_data = mos_parport;
  655. mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
  656. mos_parport->pp->dev = &serial->interface->dev;
  657. parport_announce_port(mos_parport->pp);
  658. return 0;
  659. }
  660. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  661. /*
  662. * mos7720_interrupt_callback
  663. * this is the callback function for when we have received data on the
  664. * interrupt endpoint.
  665. */
  666. static void mos7720_interrupt_callback(struct urb *urb)
  667. {
  668. int result;
  669. int length;
  670. int status = urb->status;
  671. struct device *dev = &urb->dev->dev;
  672. __u8 *data;
  673. __u8 sp1;
  674. __u8 sp2;
  675. switch (status) {
  676. case 0:
  677. /* success */
  678. break;
  679. case -ECONNRESET:
  680. case -ENOENT:
  681. case -ESHUTDOWN:
  682. /* this urb is terminated, clean up */
  683. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  684. return;
  685. default:
  686. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  687. goto exit;
  688. }
  689. length = urb->actual_length;
  690. data = urb->transfer_buffer;
  691. /* Moschip get 4 bytes
  692. * Byte 1 IIR Port 1 (port.number is 0)
  693. * Byte 2 IIR Port 2 (port.number is 1)
  694. * Byte 3 --------------
  695. * Byte 4 FIFO status for both */
  696. /* the above description is inverted
  697. * oneukum 2007-03-14 */
  698. if (unlikely(length != 4)) {
  699. dev_dbg(dev, "Wrong data !!!\n");
  700. return;
  701. }
  702. sp1 = data[3];
  703. sp2 = data[2];
  704. if ((sp1 | sp2) & 0x01) {
  705. /* No Interrupt Pending in both the ports */
  706. dev_dbg(dev, "No Interrupt !!!\n");
  707. } else {
  708. switch (sp1 & 0x0f) {
  709. case SERIAL_IIR_RLS:
  710. dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
  711. break;
  712. case SERIAL_IIR_CTI:
  713. dev_dbg(dev, "Serial Port 1: Receiver time out\n");
  714. break;
  715. case SERIAL_IIR_MS:
  716. /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
  717. break;
  718. }
  719. switch (sp2 & 0x0f) {
  720. case SERIAL_IIR_RLS:
  721. dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
  722. break;
  723. case SERIAL_IIR_CTI:
  724. dev_dbg(dev, "Serial Port 2: Receiver time out\n");
  725. break;
  726. case SERIAL_IIR_MS:
  727. /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
  728. break;
  729. }
  730. }
  731. exit:
  732. result = usb_submit_urb(urb, GFP_ATOMIC);
  733. if (result)
  734. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  735. }
  736. /*
  737. * mos7715_interrupt_callback
  738. * this is the 7715's callback function for when we have received data on
  739. * the interrupt endpoint.
  740. */
  741. static void mos7715_interrupt_callback(struct urb *urb)
  742. {
  743. int result;
  744. int length;
  745. int status = urb->status;
  746. struct device *dev = &urb->dev->dev;
  747. __u8 *data;
  748. __u8 iir;
  749. switch (status) {
  750. case 0:
  751. /* success */
  752. break;
  753. case -ECONNRESET:
  754. case -ENOENT:
  755. case -ESHUTDOWN:
  756. case -ENODEV:
  757. /* this urb is terminated, clean up */
  758. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  759. return;
  760. default:
  761. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  762. goto exit;
  763. }
  764. length = urb->actual_length;
  765. data = urb->transfer_buffer;
  766. /* Structure of data from 7715 device:
  767. * Byte 1: IIR serial Port
  768. * Byte 2: unused
  769. * Byte 2: DSR parallel port
  770. * Byte 4: FIFO status for both */
  771. if (unlikely(length != 4)) {
  772. dev_dbg(dev, "Wrong data !!!\n");
  773. return;
  774. }
  775. iir = data[0];
  776. if (!(iir & 0x01)) { /* serial port interrupt pending */
  777. switch (iir & 0x0f) {
  778. case SERIAL_IIR_RLS:
  779. dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
  780. break;
  781. case SERIAL_IIR_CTI:
  782. dev_dbg(dev, "Serial Port: Receiver time out\n");
  783. break;
  784. case SERIAL_IIR_MS:
  785. /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
  786. break;
  787. }
  788. }
  789. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  790. { /* update local copy of DSR reg */
  791. struct usb_serial_port *port = urb->context;
  792. struct mos7715_parport *mos_parport = port->serial->private;
  793. if (unlikely(mos_parport == NULL))
  794. return;
  795. atomic_set(&mos_parport->shadowDSR, data[2]);
  796. }
  797. #endif
  798. exit:
  799. result = usb_submit_urb(urb, GFP_ATOMIC);
  800. if (result)
  801. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  802. }
  803. /*
  804. * mos7720_bulk_in_callback
  805. * this is the callback function for when we have received data on the
  806. * bulk in endpoint.
  807. */
  808. static void mos7720_bulk_in_callback(struct urb *urb)
  809. {
  810. int retval;
  811. unsigned char *data ;
  812. struct usb_serial_port *port;
  813. int status = urb->status;
  814. if (status) {
  815. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  816. return;
  817. }
  818. port = urb->context;
  819. dev_dbg(&port->dev, "Entering...%s\n", __func__);
  820. data = urb->transfer_buffer;
  821. if (urb->actual_length) {
  822. tty_insert_flip_string(&port->port, data, urb->actual_length);
  823. tty_flip_buffer_push(&port->port);
  824. }
  825. if (port->read_urb->status != -EINPROGRESS) {
  826. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  827. if (retval)
  828. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  829. }
  830. }
  831. /*
  832. * mos7720_bulk_out_data_callback
  833. * this is the callback function for when we have finished sending serial
  834. * data on the bulk out endpoint.
  835. */
  836. static void mos7720_bulk_out_data_callback(struct urb *urb)
  837. {
  838. struct moschip_port *mos7720_port;
  839. int status = urb->status;
  840. if (status) {
  841. dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
  842. return;
  843. }
  844. mos7720_port = urb->context;
  845. if (!mos7720_port) {
  846. dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
  847. return ;
  848. }
  849. if (mos7720_port->open)
  850. tty_port_tty_wakeup(&mos7720_port->port->port);
  851. }
  852. static int mos77xx_calc_num_ports(struct usb_serial *serial,
  853. struct usb_serial_endpoints *epds)
  854. {
  855. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  856. if (product == MOSCHIP_DEVICE_ID_7715) {
  857. /*
  858. * The 7715 uses the first bulk in/out endpoint pair for the
  859. * parallel port, and the second for the serial port. We swap
  860. * the endpoint descriptors here so that the the first and
  861. * only registered port structure uses the serial-port
  862. * endpoints.
  863. */
  864. swap(epds->bulk_in[0], epds->bulk_in[1]);
  865. swap(epds->bulk_out[0], epds->bulk_out[1]);
  866. return 1;
  867. }
  868. return 2;
  869. }
  870. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  871. {
  872. struct usb_serial *serial;
  873. struct urb *urb;
  874. struct moschip_port *mos7720_port;
  875. int response;
  876. int port_number;
  877. __u8 data;
  878. int allocated_urbs = 0;
  879. int j;
  880. serial = port->serial;
  881. mos7720_port = usb_get_serial_port_data(port);
  882. if (mos7720_port == NULL)
  883. return -ENODEV;
  884. usb_clear_halt(serial->dev, port->write_urb->pipe);
  885. usb_clear_halt(serial->dev, port->read_urb->pipe);
  886. /* Initialising the write urb pool */
  887. for (j = 0; j < NUM_URBS; ++j) {
  888. urb = usb_alloc_urb(0, GFP_KERNEL);
  889. mos7720_port->write_urb_pool[j] = urb;
  890. if (!urb)
  891. continue;
  892. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  893. GFP_KERNEL);
  894. if (!urb->transfer_buffer) {
  895. usb_free_urb(mos7720_port->write_urb_pool[j]);
  896. mos7720_port->write_urb_pool[j] = NULL;
  897. continue;
  898. }
  899. allocated_urbs++;
  900. }
  901. if (!allocated_urbs)
  902. return -ENOMEM;
  903. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  904. *
  905. * Register Index
  906. * 0 : MOS7720_THR/MOS7720_RHR
  907. * 1 : MOS7720_IER
  908. * 2 : MOS7720_FCR
  909. * 3 : MOS7720_LCR
  910. * 4 : MOS7720_MCR
  911. * 5 : MOS7720_LSR
  912. * 6 : MOS7720_MSR
  913. * 7 : MOS7720_SPR
  914. *
  915. * 0x08 : SP1/2 Control Reg
  916. */
  917. port_number = port->port_number;
  918. read_mos_reg(serial, port_number, MOS7720_LSR, &data);
  919. dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
  920. write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
  921. write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
  922. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  923. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  924. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  925. mos7720_port->shadowLCR = 0x03;
  926. write_mos_reg(serial, port_number, MOS7720_LCR,
  927. mos7720_port->shadowLCR);
  928. mos7720_port->shadowMCR = 0x0b;
  929. write_mos_reg(serial, port_number, MOS7720_MCR,
  930. mos7720_port->shadowMCR);
  931. write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
  932. read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
  933. data = data | (port->port_number + 1);
  934. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
  935. mos7720_port->shadowLCR = 0x83;
  936. write_mos_reg(serial, port_number, MOS7720_LCR,
  937. mos7720_port->shadowLCR);
  938. write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
  939. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  940. mos7720_port->shadowLCR = 0x03;
  941. write_mos_reg(serial, port_number, MOS7720_LCR,
  942. mos7720_port->shadowLCR);
  943. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  944. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  945. if (response)
  946. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  947. __func__, response);
  948. /* initialize our port settings */
  949. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  950. /* send a open port command */
  951. mos7720_port->open = 1;
  952. return 0;
  953. }
  954. /*
  955. * mos7720_chars_in_buffer
  956. * this function is called by the tty driver when it wants to know how many
  957. * bytes of data we currently have outstanding in the port (data that has
  958. * been written, but hasn't made it out the port yet)
  959. * If successful, we return the number of bytes left to be written in the
  960. * system,
  961. * Otherwise we return a negative error number.
  962. */
  963. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  964. {
  965. struct usb_serial_port *port = tty->driver_data;
  966. int i;
  967. int chars = 0;
  968. struct moschip_port *mos7720_port;
  969. mos7720_port = usb_get_serial_port_data(port);
  970. if (mos7720_port == NULL)
  971. return 0;
  972. for (i = 0; i < NUM_URBS; ++i) {
  973. if (mos7720_port->write_urb_pool[i] &&
  974. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  975. chars += URB_TRANSFER_BUFFER_SIZE;
  976. }
  977. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  978. return chars;
  979. }
  980. static void mos7720_close(struct usb_serial_port *port)
  981. {
  982. struct usb_serial *serial;
  983. struct moschip_port *mos7720_port;
  984. int j;
  985. serial = port->serial;
  986. mos7720_port = usb_get_serial_port_data(port);
  987. if (mos7720_port == NULL)
  988. return;
  989. for (j = 0; j < NUM_URBS; ++j)
  990. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  991. /* Freeing Write URBs */
  992. for (j = 0; j < NUM_URBS; ++j) {
  993. if (mos7720_port->write_urb_pool[j]) {
  994. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  995. usb_free_urb(mos7720_port->write_urb_pool[j]);
  996. }
  997. }
  998. /* While closing port, shutdown all bulk read, write *
  999. * and interrupt read if they exists, otherwise nop */
  1000. usb_kill_urb(port->write_urb);
  1001. usb_kill_urb(port->read_urb);
  1002. write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
  1003. write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
  1004. mos7720_port->open = 0;
  1005. }
  1006. static void mos7720_break(struct tty_struct *tty, int break_state)
  1007. {
  1008. struct usb_serial_port *port = tty->driver_data;
  1009. unsigned char data;
  1010. struct usb_serial *serial;
  1011. struct moschip_port *mos7720_port;
  1012. serial = port->serial;
  1013. mos7720_port = usb_get_serial_port_data(port);
  1014. if (mos7720_port == NULL)
  1015. return;
  1016. if (break_state == -1)
  1017. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  1018. else
  1019. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  1020. mos7720_port->shadowLCR = data;
  1021. write_mos_reg(serial, port->port_number, MOS7720_LCR,
  1022. mos7720_port->shadowLCR);
  1023. }
  1024. /*
  1025. * mos7720_write_room
  1026. * this function is called by the tty driver when it wants to know how many
  1027. * bytes of data we can accept for a specific port.
  1028. * If successful, we return the amount of room that we have for this port
  1029. * Otherwise we return a negative error number.
  1030. */
  1031. static int mos7720_write_room(struct tty_struct *tty)
  1032. {
  1033. struct usb_serial_port *port = tty->driver_data;
  1034. struct moschip_port *mos7720_port;
  1035. int room = 0;
  1036. int i;
  1037. mos7720_port = usb_get_serial_port_data(port);
  1038. if (mos7720_port == NULL)
  1039. return -ENODEV;
  1040. /* FIXME: Locking */
  1041. for (i = 0; i < NUM_URBS; ++i) {
  1042. if (mos7720_port->write_urb_pool[i] &&
  1043. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  1044. room += URB_TRANSFER_BUFFER_SIZE;
  1045. }
  1046. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1047. return room;
  1048. }
  1049. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  1050. const unsigned char *data, int count)
  1051. {
  1052. int status;
  1053. int i;
  1054. int bytes_sent = 0;
  1055. int transfer_size;
  1056. struct moschip_port *mos7720_port;
  1057. struct usb_serial *serial;
  1058. struct urb *urb;
  1059. const unsigned char *current_position = data;
  1060. serial = port->serial;
  1061. mos7720_port = usb_get_serial_port_data(port);
  1062. if (mos7720_port == NULL)
  1063. return -ENODEV;
  1064. /* try to find a free urb in the list */
  1065. urb = NULL;
  1066. for (i = 0; i < NUM_URBS; ++i) {
  1067. if (mos7720_port->write_urb_pool[i] &&
  1068. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1069. urb = mos7720_port->write_urb_pool[i];
  1070. dev_dbg(&port->dev, "URB:%d\n", i);
  1071. break;
  1072. }
  1073. }
  1074. if (urb == NULL) {
  1075. dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
  1076. goto exit;
  1077. }
  1078. if (urb->transfer_buffer == NULL) {
  1079. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  1080. GFP_ATOMIC);
  1081. if (!urb->transfer_buffer) {
  1082. bytes_sent = -ENOMEM;
  1083. goto exit;
  1084. }
  1085. }
  1086. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1087. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1088. usb_serial_debug_data(&port->dev, __func__, transfer_size,
  1089. urb->transfer_buffer);
  1090. /* fill urb with data and submit */
  1091. usb_fill_bulk_urb(urb, serial->dev,
  1092. usb_sndbulkpipe(serial->dev,
  1093. port->bulk_out_endpointAddress),
  1094. urb->transfer_buffer, transfer_size,
  1095. mos7720_bulk_out_data_callback, mos7720_port);
  1096. /* send it down the pipe */
  1097. status = usb_submit_urb(urb, GFP_ATOMIC);
  1098. if (status) {
  1099. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  1100. "with status = %d\n", __func__, status);
  1101. bytes_sent = status;
  1102. goto exit;
  1103. }
  1104. bytes_sent = transfer_size;
  1105. exit:
  1106. return bytes_sent;
  1107. }
  1108. static void mos7720_throttle(struct tty_struct *tty)
  1109. {
  1110. struct usb_serial_port *port = tty->driver_data;
  1111. struct moschip_port *mos7720_port;
  1112. int status;
  1113. mos7720_port = usb_get_serial_port_data(port);
  1114. if (mos7720_port == NULL)
  1115. return;
  1116. if (!mos7720_port->open) {
  1117. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1118. return;
  1119. }
  1120. /* if we are implementing XON/XOFF, send the stop character */
  1121. if (I_IXOFF(tty)) {
  1122. unsigned char stop_char = STOP_CHAR(tty);
  1123. status = mos7720_write(tty, port, &stop_char, 1);
  1124. if (status <= 0)
  1125. return;
  1126. }
  1127. /* if we are implementing RTS/CTS, toggle that line */
  1128. if (C_CRTSCTS(tty)) {
  1129. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  1130. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1131. mos7720_port->shadowMCR);
  1132. }
  1133. }
  1134. static void mos7720_unthrottle(struct tty_struct *tty)
  1135. {
  1136. struct usb_serial_port *port = tty->driver_data;
  1137. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1138. int status;
  1139. if (mos7720_port == NULL)
  1140. return;
  1141. if (!mos7720_port->open) {
  1142. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1143. return;
  1144. }
  1145. /* if we are implementing XON/XOFF, send the start character */
  1146. if (I_IXOFF(tty)) {
  1147. unsigned char start_char = START_CHAR(tty);
  1148. status = mos7720_write(tty, port, &start_char, 1);
  1149. if (status <= 0)
  1150. return;
  1151. }
  1152. /* if we are implementing RTS/CTS, toggle that line */
  1153. if (C_CRTSCTS(tty)) {
  1154. mos7720_port->shadowMCR |= UART_MCR_RTS;
  1155. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1156. mos7720_port->shadowMCR);
  1157. }
  1158. }
  1159. /* FIXME: this function does not work */
  1160. static int set_higher_rates(struct moschip_port *mos7720_port,
  1161. unsigned int baud)
  1162. {
  1163. struct usb_serial_port *port;
  1164. struct usb_serial *serial;
  1165. int port_number;
  1166. enum mos_regs sp_reg;
  1167. if (mos7720_port == NULL)
  1168. return -EINVAL;
  1169. port = mos7720_port->port;
  1170. serial = port->serial;
  1171. /***********************************************
  1172. * Init Sequence for higher rates
  1173. ***********************************************/
  1174. dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
  1175. port_number = port->port_number;
  1176. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  1177. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  1178. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  1179. mos7720_port->shadowMCR = 0x0b;
  1180. write_mos_reg(serial, port_number, MOS7720_MCR,
  1181. mos7720_port->shadowMCR);
  1182. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
  1183. /***********************************************
  1184. * Set for higher rates *
  1185. ***********************************************/
  1186. /* writing baud rate verbatum into uart clock field clearly not right */
  1187. if (port_number == 0)
  1188. sp_reg = MOS7720_SP1_REG;
  1189. else
  1190. sp_reg = MOS7720_SP2_REG;
  1191. write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
  1192. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
  1193. mos7720_port->shadowMCR = 0x2b;
  1194. write_mos_reg(serial, port_number, MOS7720_MCR,
  1195. mos7720_port->shadowMCR);
  1196. /***********************************************
  1197. * Set DLL/DLM
  1198. ***********************************************/
  1199. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1200. write_mos_reg(serial, port_number, MOS7720_LCR,
  1201. mos7720_port->shadowLCR);
  1202. write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
  1203. write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
  1204. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1205. write_mos_reg(serial, port_number, MOS7720_LCR,
  1206. mos7720_port->shadowLCR);
  1207. return 0;
  1208. }
  1209. /* baud rate information */
  1210. struct divisor_table_entry {
  1211. __u32 baudrate;
  1212. __u16 divisor;
  1213. };
  1214. /* Define table of divisors for moschip 7720 hardware *
  1215. * These assume a 3.6864MHz crystal, the standard /16, and *
  1216. * MCR.7 = 0. */
  1217. static const struct divisor_table_entry divisor_table[] = {
  1218. { 50, 2304},
  1219. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  1220. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  1221. { 150, 768},
  1222. { 300, 384},
  1223. { 600, 192},
  1224. { 1200, 96},
  1225. { 1800, 64},
  1226. { 2400, 48},
  1227. { 4800, 24},
  1228. { 7200, 16},
  1229. { 9600, 12},
  1230. { 19200, 6},
  1231. { 38400, 3},
  1232. { 57600, 2},
  1233. { 115200, 1},
  1234. };
  1235. /*****************************************************************************
  1236. * calc_baud_rate_divisor
  1237. * this function calculates the proper baud rate divisor for the specified
  1238. * baud rate.
  1239. *****************************************************************************/
  1240. static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
  1241. {
  1242. int i;
  1243. __u16 custom;
  1244. __u16 round1;
  1245. __u16 round;
  1246. dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
  1247. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1248. if (divisor_table[i].baudrate == baudrate) {
  1249. *divisor = divisor_table[i].divisor;
  1250. return 0;
  1251. }
  1252. }
  1253. /* After trying for all the standard baud rates *
  1254. * Try calculating the divisor for this baud rate */
  1255. if (baudrate > 75 && baudrate < 230400) {
  1256. /* get the divisor */
  1257. custom = (__u16)(230400L / baudrate);
  1258. /* Check for round off */
  1259. round1 = (__u16)(2304000L / baudrate);
  1260. round = (__u16)(round1 - (custom * 10));
  1261. if (round > 4)
  1262. custom++;
  1263. *divisor = custom;
  1264. dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
  1265. return 0;
  1266. }
  1267. dev_dbg(&port->dev, "Baud calculation Failed...\n");
  1268. return -EINVAL;
  1269. }
  1270. /*
  1271. * send_cmd_write_baud_rate
  1272. * this function sends the proper command to change the baud rate of the
  1273. * specified port.
  1274. */
  1275. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  1276. int baudrate)
  1277. {
  1278. struct usb_serial_port *port;
  1279. struct usb_serial *serial;
  1280. int divisor;
  1281. int status;
  1282. unsigned char number;
  1283. if (mos7720_port == NULL)
  1284. return -1;
  1285. port = mos7720_port->port;
  1286. serial = port->serial;
  1287. number = port->port_number;
  1288. dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
  1289. /* Calculate the Divisor */
  1290. status = calc_baud_rate_divisor(port, baudrate, &divisor);
  1291. if (status) {
  1292. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1293. return status;
  1294. }
  1295. /* Enable access to divisor latch */
  1296. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1297. write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
  1298. /* Write the divisor */
  1299. write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
  1300. write_mos_reg(serial, number, MOS7720_DLM,
  1301. (__u8)((divisor & 0xff00) >> 8));
  1302. /* Disable access to divisor latch */
  1303. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1304. write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
  1305. return status;
  1306. }
  1307. /*
  1308. * change_port_settings
  1309. * This routine is called to set the UART on the device to match
  1310. * the specified new settings.
  1311. */
  1312. static void change_port_settings(struct tty_struct *tty,
  1313. struct moschip_port *mos7720_port,
  1314. struct ktermios *old_termios)
  1315. {
  1316. struct usb_serial_port *port;
  1317. struct usb_serial *serial;
  1318. int baud;
  1319. unsigned cflag;
  1320. __u8 lData;
  1321. __u8 lParity;
  1322. __u8 lStop;
  1323. int status;
  1324. int port_number;
  1325. if (mos7720_port == NULL)
  1326. return ;
  1327. port = mos7720_port->port;
  1328. serial = port->serial;
  1329. port_number = port->port_number;
  1330. if (!mos7720_port->open) {
  1331. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1332. return;
  1333. }
  1334. lData = UART_LCR_WLEN8;
  1335. lStop = 0x00; /* 1 stop bit */
  1336. lParity = 0x00; /* No parity */
  1337. cflag = tty->termios.c_cflag;
  1338. /* Change the number of bits */
  1339. switch (cflag & CSIZE) {
  1340. case CS5:
  1341. lData = UART_LCR_WLEN5;
  1342. break;
  1343. case CS6:
  1344. lData = UART_LCR_WLEN6;
  1345. break;
  1346. case CS7:
  1347. lData = UART_LCR_WLEN7;
  1348. break;
  1349. default:
  1350. case CS8:
  1351. lData = UART_LCR_WLEN8;
  1352. break;
  1353. }
  1354. /* Change the Parity bit */
  1355. if (cflag & PARENB) {
  1356. if (cflag & PARODD) {
  1357. lParity = UART_LCR_PARITY;
  1358. dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
  1359. } else {
  1360. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  1361. dev_dbg(&port->dev, "%s - parity = even\n", __func__);
  1362. }
  1363. } else {
  1364. dev_dbg(&port->dev, "%s - parity = none\n", __func__);
  1365. }
  1366. if (cflag & CMSPAR)
  1367. lParity = lParity | 0x20;
  1368. /* Change the Stop bit */
  1369. if (cflag & CSTOPB) {
  1370. lStop = UART_LCR_STOP;
  1371. dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
  1372. } else {
  1373. lStop = 0x00;
  1374. dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
  1375. }
  1376. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1377. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1378. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1379. /* Update the LCR with the correct value */
  1380. mos7720_port->shadowLCR &=
  1381. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1382. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1383. /* Disable Interrupts */
  1384. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  1385. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  1386. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  1387. /* Send the updated LCR value to the mos7720 */
  1388. write_mos_reg(serial, port_number, MOS7720_LCR,
  1389. mos7720_port->shadowLCR);
  1390. mos7720_port->shadowMCR = 0x0b;
  1391. write_mos_reg(serial, port_number, MOS7720_MCR,
  1392. mos7720_port->shadowMCR);
  1393. /* set up the MCR register and send it to the mos7720 */
  1394. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1395. if (cflag & CBAUD)
  1396. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1397. if (cflag & CRTSCTS) {
  1398. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1399. /* To set hardware flow control to the specified *
  1400. * serial port, in SP1/2_CONTROL_REG */
  1401. if (port_number)
  1402. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
  1403. 0x01);
  1404. else
  1405. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
  1406. 0x02);
  1407. } else
  1408. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1409. write_mos_reg(serial, port_number, MOS7720_MCR,
  1410. mos7720_port->shadowMCR);
  1411. /* Determine divisor based on baud rate */
  1412. baud = tty_get_baud_rate(tty);
  1413. if (!baud) {
  1414. /* pick a default, any default... */
  1415. dev_dbg(&port->dev, "Picked default baud...\n");
  1416. baud = 9600;
  1417. }
  1418. if (baud >= 230400) {
  1419. set_higher_rates(mos7720_port, baud);
  1420. /* Enable Interrupts */
  1421. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  1422. return;
  1423. }
  1424. dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
  1425. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1426. /* FIXME: needs to write actual resulting baud back not just
  1427. blindly do so */
  1428. if (cflag & CBAUD)
  1429. tty_encode_baud_rate(tty, baud, baud);
  1430. /* Enable Interrupts */
  1431. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  1432. if (port->read_urb->status != -EINPROGRESS) {
  1433. status = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1434. if (status)
  1435. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1436. }
  1437. }
  1438. /*
  1439. * mos7720_set_termios
  1440. * this function is called by the tty driver when it wants to change the
  1441. * termios structure.
  1442. */
  1443. static void mos7720_set_termios(struct tty_struct *tty,
  1444. struct usb_serial_port *port, struct ktermios *old_termios)
  1445. {
  1446. int status;
  1447. struct moschip_port *mos7720_port;
  1448. mos7720_port = usb_get_serial_port_data(port);
  1449. if (mos7720_port == NULL)
  1450. return;
  1451. if (!mos7720_port->open) {
  1452. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1453. return;
  1454. }
  1455. /* change the port settings to the new ones specified */
  1456. change_port_settings(tty, mos7720_port, old_termios);
  1457. if (port->read_urb->status != -EINPROGRESS) {
  1458. status = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1459. if (status)
  1460. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1461. }
  1462. }
  1463. /*
  1464. * get_lsr_info - get line status register info
  1465. *
  1466. * Purpose: Let user call ioctl() to get info when the UART physically
  1467. * is emptied. On bus types like RS485, the transmitter must
  1468. * release the bus after transmitting. This must be done when
  1469. * the transmit shift register is empty, not be done when the
  1470. * transmit holding register is empty. This functionality
  1471. * allows an RS485 driver to be written in user space.
  1472. */
  1473. static int get_lsr_info(struct tty_struct *tty,
  1474. struct moschip_port *mos7720_port, unsigned int __user *value)
  1475. {
  1476. struct usb_serial_port *port = tty->driver_data;
  1477. unsigned int result = 0;
  1478. unsigned char data = 0;
  1479. int port_number = port->port_number;
  1480. int count;
  1481. count = mos7720_chars_in_buffer(tty);
  1482. if (count == 0) {
  1483. read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
  1484. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1485. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1486. dev_dbg(&port->dev, "%s -- Empty\n", __func__);
  1487. result = TIOCSER_TEMT;
  1488. }
  1489. }
  1490. if (copy_to_user(value, &result, sizeof(int)))
  1491. return -EFAULT;
  1492. return 0;
  1493. }
  1494. static int mos7720_tiocmget(struct tty_struct *tty)
  1495. {
  1496. struct usb_serial_port *port = tty->driver_data;
  1497. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1498. unsigned int result = 0;
  1499. unsigned int mcr ;
  1500. unsigned int msr ;
  1501. mcr = mos7720_port->shadowMCR;
  1502. msr = mos7720_port->shadowMSR;
  1503. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1504. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1505. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1506. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1507. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1508. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1509. return result;
  1510. }
  1511. static int mos7720_tiocmset(struct tty_struct *tty,
  1512. unsigned int set, unsigned int clear)
  1513. {
  1514. struct usb_serial_port *port = tty->driver_data;
  1515. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1516. unsigned int mcr ;
  1517. mcr = mos7720_port->shadowMCR;
  1518. if (set & TIOCM_RTS)
  1519. mcr |= UART_MCR_RTS;
  1520. if (set & TIOCM_DTR)
  1521. mcr |= UART_MCR_DTR;
  1522. if (set & TIOCM_LOOP)
  1523. mcr |= UART_MCR_LOOP;
  1524. if (clear & TIOCM_RTS)
  1525. mcr &= ~UART_MCR_RTS;
  1526. if (clear & TIOCM_DTR)
  1527. mcr &= ~UART_MCR_DTR;
  1528. if (clear & TIOCM_LOOP)
  1529. mcr &= ~UART_MCR_LOOP;
  1530. mos7720_port->shadowMCR = mcr;
  1531. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1532. mos7720_port->shadowMCR);
  1533. return 0;
  1534. }
  1535. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1536. unsigned int __user *value)
  1537. {
  1538. unsigned int mcr;
  1539. unsigned int arg;
  1540. struct usb_serial_port *port;
  1541. if (mos7720_port == NULL)
  1542. return -1;
  1543. port = (struct usb_serial_port *)mos7720_port->port;
  1544. mcr = mos7720_port->shadowMCR;
  1545. if (copy_from_user(&arg, value, sizeof(int)))
  1546. return -EFAULT;
  1547. switch (cmd) {
  1548. case TIOCMBIS:
  1549. if (arg & TIOCM_RTS)
  1550. mcr |= UART_MCR_RTS;
  1551. if (arg & TIOCM_DTR)
  1552. mcr |= UART_MCR_RTS;
  1553. if (arg & TIOCM_LOOP)
  1554. mcr |= UART_MCR_LOOP;
  1555. break;
  1556. case TIOCMBIC:
  1557. if (arg & TIOCM_RTS)
  1558. mcr &= ~UART_MCR_RTS;
  1559. if (arg & TIOCM_DTR)
  1560. mcr &= ~UART_MCR_RTS;
  1561. if (arg & TIOCM_LOOP)
  1562. mcr &= ~UART_MCR_LOOP;
  1563. break;
  1564. }
  1565. mos7720_port->shadowMCR = mcr;
  1566. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1567. mos7720_port->shadowMCR);
  1568. return 0;
  1569. }
  1570. static int get_serial_info(struct moschip_port *mos7720_port,
  1571. struct serial_struct __user *retinfo)
  1572. {
  1573. struct serial_struct tmp;
  1574. memset(&tmp, 0, sizeof(tmp));
  1575. tmp.type = PORT_16550A;
  1576. tmp.line = mos7720_port->port->minor;
  1577. tmp.port = mos7720_port->port->port_number;
  1578. tmp.irq = 0;
  1579. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1580. tmp.baud_base = 9600;
  1581. tmp.close_delay = 5*HZ;
  1582. tmp.closing_wait = 30*HZ;
  1583. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1584. return -EFAULT;
  1585. return 0;
  1586. }
  1587. static int mos7720_ioctl(struct tty_struct *tty,
  1588. unsigned int cmd, unsigned long arg)
  1589. {
  1590. struct usb_serial_port *port = tty->driver_data;
  1591. struct moschip_port *mos7720_port;
  1592. mos7720_port = usb_get_serial_port_data(port);
  1593. if (mos7720_port == NULL)
  1594. return -ENODEV;
  1595. switch (cmd) {
  1596. case TIOCSERGETLSR:
  1597. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1598. return get_lsr_info(tty, mos7720_port,
  1599. (unsigned int __user *)arg);
  1600. /* FIXME: These should be using the mode methods */
  1601. case TIOCMBIS:
  1602. case TIOCMBIC:
  1603. dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
  1604. return set_modem_info(mos7720_port, cmd,
  1605. (unsigned int __user *)arg);
  1606. case TIOCGSERIAL:
  1607. dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
  1608. return get_serial_info(mos7720_port,
  1609. (struct serial_struct __user *)arg);
  1610. }
  1611. return -ENOIOCTLCMD;
  1612. }
  1613. static int mos7720_startup(struct usb_serial *serial)
  1614. {
  1615. struct usb_device *dev;
  1616. char data;
  1617. u16 product;
  1618. int ret_val;
  1619. product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1620. dev = serial->dev;
  1621. if (product == MOSCHIP_DEVICE_ID_7715) {
  1622. struct urb *urb = serial->port[0]->interrupt_in_urb;
  1623. urb->complete = mos7715_interrupt_callback;
  1624. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1625. ret_val = mos7715_parport_init(serial);
  1626. if (ret_val < 0)
  1627. return ret_val;
  1628. #endif
  1629. }
  1630. /* start the interrupt urb */
  1631. ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
  1632. if (ret_val) {
  1633. dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
  1634. ret_val);
  1635. }
  1636. /* LSR For Port 1 */
  1637. read_mos_reg(serial, 0, MOS7720_LSR, &data);
  1638. dev_dbg(&dev->dev, "LSR:%x\n", data);
  1639. return 0;
  1640. }
  1641. static void mos7720_release(struct usb_serial *serial)
  1642. {
  1643. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1644. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1645. /* close the parallel port */
  1646. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  1647. == MOSCHIP_DEVICE_ID_7715) {
  1648. struct urbtracker *urbtrack;
  1649. unsigned long flags;
  1650. struct mos7715_parport *mos_parport =
  1651. usb_get_serial_data(serial);
  1652. /* prevent NULL ptr dereference in port callbacks */
  1653. spin_lock(&release_lock);
  1654. mos_parport->pp->private_data = NULL;
  1655. spin_unlock(&release_lock);
  1656. /* wait for synchronous usb calls to return */
  1657. if (mos_parport->msg_pending)
  1658. wait_for_completion_timeout(&mos_parport->syncmsg_compl,
  1659. msecs_to_jiffies(MOS_WDR_TIMEOUT));
  1660. parport_remove_port(mos_parport->pp);
  1661. usb_set_serial_data(serial, NULL);
  1662. mos_parport->serial = NULL;
  1663. /* if tasklet currently scheduled, wait for it to complete */
  1664. tasklet_kill(&mos_parport->urb_tasklet);
  1665. /* unlink any urbs sent by the tasklet */
  1666. spin_lock_irqsave(&mos_parport->listlock, flags);
  1667. list_for_each_entry(urbtrack,
  1668. &mos_parport->active_urbs,
  1669. urblist_entry)
  1670. usb_unlink_urb(urbtrack->urb);
  1671. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  1672. parport_del_port(mos_parport->pp);
  1673. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  1674. }
  1675. #endif
  1676. }
  1677. static int mos7720_port_probe(struct usb_serial_port *port)
  1678. {
  1679. struct moschip_port *mos7720_port;
  1680. mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
  1681. if (!mos7720_port)
  1682. return -ENOMEM;
  1683. mos7720_port->port = port;
  1684. usb_set_serial_port_data(port, mos7720_port);
  1685. return 0;
  1686. }
  1687. static int mos7720_port_remove(struct usb_serial_port *port)
  1688. {
  1689. struct moschip_port *mos7720_port;
  1690. mos7720_port = usb_get_serial_port_data(port);
  1691. kfree(mos7720_port);
  1692. return 0;
  1693. }
  1694. static struct usb_serial_driver moschip7720_2port_driver = {
  1695. .driver = {
  1696. .owner = THIS_MODULE,
  1697. .name = "moschip7720",
  1698. },
  1699. .description = "Moschip 2 port adapter",
  1700. .id_table = id_table,
  1701. .num_bulk_in = 2,
  1702. .num_bulk_out = 2,
  1703. .num_interrupt_in = 1,
  1704. .calc_num_ports = mos77xx_calc_num_ports,
  1705. .open = mos7720_open,
  1706. .close = mos7720_close,
  1707. .throttle = mos7720_throttle,
  1708. .unthrottle = mos7720_unthrottle,
  1709. .attach = mos7720_startup,
  1710. .release = mos7720_release,
  1711. .port_probe = mos7720_port_probe,
  1712. .port_remove = mos7720_port_remove,
  1713. .ioctl = mos7720_ioctl,
  1714. .tiocmget = mos7720_tiocmget,
  1715. .tiocmset = mos7720_tiocmset,
  1716. .set_termios = mos7720_set_termios,
  1717. .write = mos7720_write,
  1718. .write_room = mos7720_write_room,
  1719. .chars_in_buffer = mos7720_chars_in_buffer,
  1720. .break_ctl = mos7720_break,
  1721. .read_bulk_callback = mos7720_bulk_in_callback,
  1722. .read_int_callback = mos7720_interrupt_callback,
  1723. };
  1724. static struct usb_serial_driver * const serial_drivers[] = {
  1725. &moschip7720_2port_driver, NULL
  1726. };
  1727. module_usb_serial_driver(serial_drivers, id_table);
  1728. MODULE_AUTHOR(DRIVER_AUTHOR);
  1729. MODULE_DESCRIPTION(DRIVER_DESC);
  1730. MODULE_LICENSE("GPL v2");