mos7720.c 61 KB

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