mos7720.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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->status != -EINPROGRESS) {
  842. port->read_urb->dev = port->serial->dev;
  843. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  844. if (retval)
  845. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  846. retval);
  847. }
  848. }
  849. /*
  850. * mos7720_bulk_out_data_callback
  851. * this is the callback function for when we have finished sending serial
  852. * data on the bulk out endpoint.
  853. */
  854. static void mos7720_bulk_out_data_callback(struct urb *urb)
  855. {
  856. struct moschip_port *mos7720_port;
  857. struct tty_struct *tty;
  858. int status = urb->status;
  859. if (status) {
  860. dbg("nonzero write bulk status received:%d", status);
  861. return;
  862. }
  863. mos7720_port = urb->context;
  864. if (!mos7720_port) {
  865. dbg("NULL mos7720_port pointer");
  866. return ;
  867. }
  868. tty = tty_port_tty_get(&mos7720_port->port->port);
  869. if (tty && mos7720_port->open)
  870. tty_wakeup(tty);
  871. tty_kref_put(tty);
  872. }
  873. /*
  874. * mos77xx_probe
  875. * this function installs the appropriate read interrupt endpoint callback
  876. * depending on whether the device is a 7720 or 7715, thus avoiding costly
  877. * run-time checks in the high-frequency callback routine itself.
  878. */
  879. static int mos77xx_probe(struct usb_serial *serial,
  880. const struct usb_device_id *id)
  881. {
  882. if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
  883. moschip7720_2port_driver.read_int_callback =
  884. mos7715_interrupt_callback;
  885. else
  886. moschip7720_2port_driver.read_int_callback =
  887. mos7720_interrupt_callback;
  888. return 0;
  889. }
  890. static int mos77xx_calc_num_ports(struct usb_serial *serial)
  891. {
  892. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  893. if (product == MOSCHIP_DEVICE_ID_7715)
  894. return 1;
  895. return 2;
  896. }
  897. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  898. {
  899. struct usb_serial *serial;
  900. struct urb *urb;
  901. struct moschip_port *mos7720_port;
  902. int response;
  903. int port_number;
  904. __u8 data;
  905. int allocated_urbs = 0;
  906. int j;
  907. serial = port->serial;
  908. mos7720_port = usb_get_serial_port_data(port);
  909. if (mos7720_port == NULL)
  910. return -ENODEV;
  911. usb_clear_halt(serial->dev, port->write_urb->pipe);
  912. usb_clear_halt(serial->dev, port->read_urb->pipe);
  913. /* Initialising the write urb pool */
  914. for (j = 0; j < NUM_URBS; ++j) {
  915. urb = usb_alloc_urb(0, GFP_KERNEL);
  916. mos7720_port->write_urb_pool[j] = urb;
  917. if (urb == NULL) {
  918. dev_err(&port->dev, "No more urbs???\n");
  919. continue;
  920. }
  921. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  922. GFP_KERNEL);
  923. if (!urb->transfer_buffer) {
  924. dev_err(&port->dev,
  925. "%s-out of memory for urb buffers.\n",
  926. __func__);
  927. usb_free_urb(mos7720_port->write_urb_pool[j]);
  928. mos7720_port->write_urb_pool[j] = NULL;
  929. continue;
  930. }
  931. allocated_urbs++;
  932. }
  933. if (!allocated_urbs)
  934. return -ENOMEM;
  935. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  936. *
  937. * Register Index
  938. * 0 : THR/RHR
  939. * 1 : IER
  940. * 2 : FCR
  941. * 3 : LCR
  942. * 4 : MCR
  943. * 5 : LSR
  944. * 6 : MSR
  945. * 7 : SPR
  946. *
  947. * 0x08 : SP1/2 Control Reg
  948. */
  949. port_number = port->number - port->serial->minor;
  950. read_mos_reg(serial, port_number, LSR, &data);
  951. dbg("SS::%p LSR:%x", mos7720_port, data);
  952. dbg("Check:Sending Command ..........");
  953. write_mos_reg(serial, dummy, SP1_REG, 0x02);
  954. write_mos_reg(serial, dummy, SP2_REG, 0x02);
  955. write_mos_reg(serial, port_number, IER, 0x00);
  956. write_mos_reg(serial, port_number, FCR, 0x00);
  957. write_mos_reg(serial, port_number, FCR, 0xcf);
  958. mos7720_port->shadowLCR = 0x03;
  959. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  960. mos7720_port->shadowMCR = 0x0b;
  961. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  962. write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
  963. read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
  964. data = data | (port->number - port->serial->minor + 1);
  965. write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
  966. mos7720_port->shadowLCR = 0x83;
  967. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  968. write_mos_reg(serial, port_number, THR, 0x0c);
  969. write_mos_reg(serial, port_number, IER, 0x00);
  970. mos7720_port->shadowLCR = 0x03;
  971. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  972. write_mos_reg(serial, port_number, IER, 0x0c);
  973. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  974. if (response)
  975. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  976. __func__, response);
  977. /* initialize our icount structure */
  978. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  979. /* initialize our port settings */
  980. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  981. /* send a open port command */
  982. mos7720_port->open = 1;
  983. return 0;
  984. }
  985. /*
  986. * mos7720_chars_in_buffer
  987. * this function is called by the tty driver when it wants to know how many
  988. * bytes of data we currently have outstanding in the port (data that has
  989. * been written, but hasn't made it out the port yet)
  990. * If successful, we return the number of bytes left to be written in the
  991. * system,
  992. * Otherwise we return a negative error number.
  993. */
  994. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  995. {
  996. struct usb_serial_port *port = tty->driver_data;
  997. int i;
  998. int chars = 0;
  999. struct moschip_port *mos7720_port;
  1000. dbg("%s:entering ...........", __func__);
  1001. mos7720_port = usb_get_serial_port_data(port);
  1002. if (mos7720_port == NULL) {
  1003. dbg("%s:leaving ...........", __func__);
  1004. return 0;
  1005. }
  1006. for (i = 0; i < NUM_URBS; ++i) {
  1007. if (mos7720_port->write_urb_pool[i] &&
  1008. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  1009. chars += URB_TRANSFER_BUFFER_SIZE;
  1010. }
  1011. dbg("%s - returns %d", __func__, chars);
  1012. return chars;
  1013. }
  1014. static void mos7720_close(struct usb_serial_port *port)
  1015. {
  1016. struct usb_serial *serial;
  1017. struct moschip_port *mos7720_port;
  1018. int j;
  1019. dbg("mos7720_close:entering...");
  1020. serial = port->serial;
  1021. mos7720_port = usb_get_serial_port_data(port);
  1022. if (mos7720_port == NULL)
  1023. return;
  1024. for (j = 0; j < NUM_URBS; ++j)
  1025. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  1026. /* Freeing Write URBs */
  1027. for (j = 0; j < NUM_URBS; ++j) {
  1028. if (mos7720_port->write_urb_pool[j]) {
  1029. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  1030. usb_free_urb(mos7720_port->write_urb_pool[j]);
  1031. }
  1032. }
  1033. /* While closing port, shutdown all bulk read, write *
  1034. * and interrupt read if they exists, otherwise nop */
  1035. dbg("Shutdown bulk write");
  1036. usb_kill_urb(port->write_urb);
  1037. dbg("Shutdown bulk read");
  1038. usb_kill_urb(port->read_urb);
  1039. mutex_lock(&serial->disc_mutex);
  1040. /* these commands must not be issued if the device has
  1041. * been disconnected */
  1042. if (!serial->disconnected) {
  1043. write_mos_reg(serial, port->number - port->serial->minor,
  1044. MCR, 0x00);
  1045. write_mos_reg(serial, port->number - port->serial->minor,
  1046. IER, 0x00);
  1047. }
  1048. mutex_unlock(&serial->disc_mutex);
  1049. mos7720_port->open = 0;
  1050. dbg("Leaving %s", __func__);
  1051. }
  1052. static void mos7720_break(struct tty_struct *tty, int break_state)
  1053. {
  1054. struct usb_serial_port *port = tty->driver_data;
  1055. unsigned char data;
  1056. struct usb_serial *serial;
  1057. struct moschip_port *mos7720_port;
  1058. dbg("Entering %s", __func__);
  1059. serial = port->serial;
  1060. mos7720_port = usb_get_serial_port_data(port);
  1061. if (mos7720_port == NULL)
  1062. return;
  1063. if (break_state == -1)
  1064. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  1065. else
  1066. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  1067. mos7720_port->shadowLCR = data;
  1068. write_mos_reg(serial, port->number - port->serial->minor,
  1069. LCR, mos7720_port->shadowLCR);
  1070. }
  1071. /*
  1072. * mos7720_write_room
  1073. * this function is called by the tty driver when it wants to know how many
  1074. * bytes of data we can accept for a specific port.
  1075. * If successful, we return the amount of room that we have for this port
  1076. * Otherwise we return a negative error number.
  1077. */
  1078. static int mos7720_write_room(struct tty_struct *tty)
  1079. {
  1080. struct usb_serial_port *port = tty->driver_data;
  1081. struct moschip_port *mos7720_port;
  1082. int room = 0;
  1083. int i;
  1084. dbg("%s:entering ...........", __func__);
  1085. mos7720_port = usb_get_serial_port_data(port);
  1086. if (mos7720_port == NULL) {
  1087. dbg("%s:leaving ...........", __func__);
  1088. return -ENODEV;
  1089. }
  1090. /* FIXME: Locking */
  1091. for (i = 0; i < NUM_URBS; ++i) {
  1092. if (mos7720_port->write_urb_pool[i] &&
  1093. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  1094. room += URB_TRANSFER_BUFFER_SIZE;
  1095. }
  1096. dbg("%s - returns %d", __func__, room);
  1097. return room;
  1098. }
  1099. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  1100. const unsigned char *data, int count)
  1101. {
  1102. int status;
  1103. int i;
  1104. int bytes_sent = 0;
  1105. int transfer_size;
  1106. struct moschip_port *mos7720_port;
  1107. struct usb_serial *serial;
  1108. struct urb *urb;
  1109. const unsigned char *current_position = data;
  1110. dbg("%s:entering ...........", __func__);
  1111. serial = port->serial;
  1112. mos7720_port = usb_get_serial_port_data(port);
  1113. if (mos7720_port == NULL) {
  1114. dbg("mos7720_port is NULL");
  1115. return -ENODEV;
  1116. }
  1117. /* try to find a free urb in the list */
  1118. urb = NULL;
  1119. for (i = 0; i < NUM_URBS; ++i) {
  1120. if (mos7720_port->write_urb_pool[i] &&
  1121. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1122. urb = mos7720_port->write_urb_pool[i];
  1123. dbg("URB:%d", i);
  1124. break;
  1125. }
  1126. }
  1127. if (urb == NULL) {
  1128. dbg("%s - no more free urbs", __func__);
  1129. goto exit;
  1130. }
  1131. if (urb->transfer_buffer == NULL) {
  1132. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  1133. GFP_KERNEL);
  1134. if (urb->transfer_buffer == NULL) {
  1135. dev_err(&port->dev, "%s no more kernel memory...\n",
  1136. __func__);
  1137. goto exit;
  1138. }
  1139. }
  1140. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1141. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1142. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  1143. urb->transfer_buffer);
  1144. /* fill urb with data and submit */
  1145. usb_fill_bulk_urb(urb, serial->dev,
  1146. usb_sndbulkpipe(serial->dev,
  1147. port->bulk_out_endpointAddress),
  1148. urb->transfer_buffer, transfer_size,
  1149. mos7720_bulk_out_data_callback, mos7720_port);
  1150. /* send it down the pipe */
  1151. status = usb_submit_urb(urb, GFP_ATOMIC);
  1152. if (status) {
  1153. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  1154. "with status = %d\n", __func__, status);
  1155. bytes_sent = status;
  1156. goto exit;
  1157. }
  1158. bytes_sent = transfer_size;
  1159. exit:
  1160. return bytes_sent;
  1161. }
  1162. static void mos7720_throttle(struct tty_struct *tty)
  1163. {
  1164. struct usb_serial_port *port = tty->driver_data;
  1165. struct moschip_port *mos7720_port;
  1166. int status;
  1167. dbg("%s- port %d", __func__, port->number);
  1168. mos7720_port = usb_get_serial_port_data(port);
  1169. if (mos7720_port == NULL)
  1170. return;
  1171. if (!mos7720_port->open) {
  1172. dbg("port not opened");
  1173. return;
  1174. }
  1175. dbg("%s: Entering ..........", __func__);
  1176. /* if we are implementing XON/XOFF, send the stop character */
  1177. if (I_IXOFF(tty)) {
  1178. unsigned char stop_char = STOP_CHAR(tty);
  1179. status = mos7720_write(tty, port, &stop_char, 1);
  1180. if (status <= 0)
  1181. return;
  1182. }
  1183. /* if we are implementing RTS/CTS, toggle that line */
  1184. if (tty->termios->c_cflag & CRTSCTS) {
  1185. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  1186. write_mos_reg(port->serial, port->number - port->serial->minor,
  1187. MCR, mos7720_port->shadowMCR);
  1188. if (status != 0)
  1189. return;
  1190. }
  1191. }
  1192. static void mos7720_unthrottle(struct tty_struct *tty)
  1193. {
  1194. struct usb_serial_port *port = tty->driver_data;
  1195. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1196. int status;
  1197. if (mos7720_port == NULL)
  1198. return;
  1199. if (!mos7720_port->open) {
  1200. dbg("%s - port not opened", __func__);
  1201. return;
  1202. }
  1203. dbg("%s: Entering ..........", __func__);
  1204. /* if we are implementing XON/XOFF, send the start character */
  1205. if (I_IXOFF(tty)) {
  1206. unsigned char start_char = START_CHAR(tty);
  1207. status = mos7720_write(tty, port, &start_char, 1);
  1208. if (status <= 0)
  1209. return;
  1210. }
  1211. /* if we are implementing RTS/CTS, toggle that line */
  1212. if (tty->termios->c_cflag & CRTSCTS) {
  1213. mos7720_port->shadowMCR |= UART_MCR_RTS;
  1214. write_mos_reg(port->serial, port->number - port->serial->minor,
  1215. MCR, mos7720_port->shadowMCR);
  1216. if (status != 0)
  1217. return;
  1218. }
  1219. }
  1220. /* FIXME: this function does not work */
  1221. static int set_higher_rates(struct moschip_port *mos7720_port,
  1222. unsigned int baud)
  1223. {
  1224. struct usb_serial_port *port;
  1225. struct usb_serial *serial;
  1226. int port_number;
  1227. enum mos_regs sp_reg;
  1228. if (mos7720_port == NULL)
  1229. return -EINVAL;
  1230. port = mos7720_port->port;
  1231. serial = port->serial;
  1232. /***********************************************
  1233. * Init Sequence for higher rates
  1234. ***********************************************/
  1235. dbg("Sending Setting Commands ..........");
  1236. port_number = port->number - port->serial->minor;
  1237. write_mos_reg(serial, port_number, IER, 0x00);
  1238. write_mos_reg(serial, port_number, FCR, 0x00);
  1239. write_mos_reg(serial, port_number, FCR, 0xcf);
  1240. mos7720_port->shadowMCR = 0x0b;
  1241. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1242. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
  1243. /***********************************************
  1244. * Set for higher rates *
  1245. ***********************************************/
  1246. /* writing baud rate verbatum into uart clock field clearly not right */
  1247. if (port_number == 0)
  1248. sp_reg = SP1_REG;
  1249. else
  1250. sp_reg = SP2_REG;
  1251. write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
  1252. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
  1253. mos7720_port->shadowMCR = 0x2b;
  1254. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1255. /***********************************************
  1256. * Set DLL/DLM
  1257. ***********************************************/
  1258. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1259. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1260. write_mos_reg(serial, port_number, DLL, 0x01);
  1261. write_mos_reg(serial, port_number, DLM, 0x00);
  1262. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1263. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1264. return 0;
  1265. }
  1266. /* baud rate information */
  1267. struct divisor_table_entry {
  1268. __u32 baudrate;
  1269. __u16 divisor;
  1270. };
  1271. /* Define table of divisors for moschip 7720 hardware *
  1272. * These assume a 3.6864MHz crystal, the standard /16, and *
  1273. * MCR.7 = 0. */
  1274. static struct divisor_table_entry divisor_table[] = {
  1275. { 50, 2304},
  1276. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  1277. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  1278. { 150, 768},
  1279. { 300, 384},
  1280. { 600, 192},
  1281. { 1200, 96},
  1282. { 1800, 64},
  1283. { 2400, 48},
  1284. { 4800, 24},
  1285. { 7200, 16},
  1286. { 9600, 12},
  1287. { 19200, 6},
  1288. { 38400, 3},
  1289. { 57600, 2},
  1290. { 115200, 1},
  1291. };
  1292. /*****************************************************************************
  1293. * calc_baud_rate_divisor
  1294. * this function calculates the proper baud rate divisor for the specified
  1295. * baud rate.
  1296. *****************************************************************************/
  1297. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  1298. {
  1299. int i;
  1300. __u16 custom;
  1301. __u16 round1;
  1302. __u16 round;
  1303. dbg("%s - %d", __func__, baudrate);
  1304. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1305. if (divisor_table[i].baudrate == baudrate) {
  1306. *divisor = divisor_table[i].divisor;
  1307. return 0;
  1308. }
  1309. }
  1310. /* After trying for all the standard baud rates *
  1311. * Try calculating the divisor for this baud rate */
  1312. if (baudrate > 75 && baudrate < 230400) {
  1313. /* get the divisor */
  1314. custom = (__u16)(230400L / baudrate);
  1315. /* Check for round off */
  1316. round1 = (__u16)(2304000L / baudrate);
  1317. round = (__u16)(round1 - (custom * 10));
  1318. if (round > 4)
  1319. custom++;
  1320. *divisor = custom;
  1321. dbg("Baud %d = %d", baudrate, custom);
  1322. return 0;
  1323. }
  1324. dbg("Baud calculation Failed...");
  1325. return -EINVAL;
  1326. }
  1327. /*
  1328. * send_cmd_write_baud_rate
  1329. * this function sends the proper command to change the baud rate of the
  1330. * specified port.
  1331. */
  1332. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  1333. int baudrate)
  1334. {
  1335. struct usb_serial_port *port;
  1336. struct usb_serial *serial;
  1337. int divisor;
  1338. int status;
  1339. unsigned char number;
  1340. if (mos7720_port == NULL)
  1341. return -1;
  1342. port = mos7720_port->port;
  1343. serial = port->serial;
  1344. dbg("%s: Entering ..........", __func__);
  1345. number = port->number - port->serial->minor;
  1346. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  1347. /* Calculate the Divisor */
  1348. status = calc_baud_rate_divisor(baudrate, &divisor);
  1349. if (status) {
  1350. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1351. return status;
  1352. }
  1353. /* Enable access to divisor latch */
  1354. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1355. write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
  1356. /* Write the divisor */
  1357. write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
  1358. write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
  1359. /* Disable 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. return status;
  1363. }
  1364. /*
  1365. * change_port_settings
  1366. * This routine is called to set the UART on the device to match
  1367. * the specified new settings.
  1368. */
  1369. static void change_port_settings(struct tty_struct *tty,
  1370. struct moschip_port *mos7720_port,
  1371. struct ktermios *old_termios)
  1372. {
  1373. struct usb_serial_port *port;
  1374. struct usb_serial *serial;
  1375. int baud;
  1376. unsigned cflag;
  1377. unsigned iflag;
  1378. __u8 mask = 0xff;
  1379. __u8 lData;
  1380. __u8 lParity;
  1381. __u8 lStop;
  1382. int status;
  1383. int port_number;
  1384. if (mos7720_port == NULL)
  1385. return ;
  1386. port = mos7720_port->port;
  1387. serial = port->serial;
  1388. port_number = port->number - port->serial->minor;
  1389. dbg("%s - port %d", __func__, port->number);
  1390. if (!mos7720_port->open) {
  1391. dbg("%s - port not opened", __func__);
  1392. return;
  1393. }
  1394. dbg("%s: Entering ..........", __func__);
  1395. lData = UART_LCR_WLEN8;
  1396. lStop = 0x00; /* 1 stop bit */
  1397. lParity = 0x00; /* No parity */
  1398. cflag = tty->termios->c_cflag;
  1399. iflag = tty->termios->c_iflag;
  1400. /* Change the number of bits */
  1401. switch (cflag & CSIZE) {
  1402. case CS5:
  1403. lData = UART_LCR_WLEN5;
  1404. mask = 0x1f;
  1405. break;
  1406. case CS6:
  1407. lData = UART_LCR_WLEN6;
  1408. mask = 0x3f;
  1409. break;
  1410. case CS7:
  1411. lData = UART_LCR_WLEN7;
  1412. mask = 0x7f;
  1413. break;
  1414. default:
  1415. case CS8:
  1416. lData = UART_LCR_WLEN8;
  1417. break;
  1418. }
  1419. /* Change the Parity bit */
  1420. if (cflag & PARENB) {
  1421. if (cflag & PARODD) {
  1422. lParity = UART_LCR_PARITY;
  1423. dbg("%s - parity = odd", __func__);
  1424. } else {
  1425. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  1426. dbg("%s - parity = even", __func__);
  1427. }
  1428. } else {
  1429. dbg("%s - parity = none", __func__);
  1430. }
  1431. if (cflag & CMSPAR)
  1432. lParity = lParity | 0x20;
  1433. /* Change the Stop bit */
  1434. if (cflag & CSTOPB) {
  1435. lStop = UART_LCR_STOP;
  1436. dbg("%s - stop bits = 2", __func__);
  1437. } else {
  1438. lStop = 0x00;
  1439. dbg("%s - stop bits = 1", __func__);
  1440. }
  1441. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1442. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1443. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1444. /* Update the LCR with the correct value */
  1445. mos7720_port->shadowLCR &=
  1446. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1447. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1448. /* Disable Interrupts */
  1449. write_mos_reg(serial, port_number, IER, 0x00);
  1450. write_mos_reg(serial, port_number, FCR, 0x00);
  1451. write_mos_reg(serial, port_number, FCR, 0xcf);
  1452. /* Send the updated LCR value to the mos7720 */
  1453. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1454. mos7720_port->shadowMCR = 0x0b;
  1455. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1456. /* set up the MCR register and send it to the mos7720 */
  1457. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1458. if (cflag & CBAUD)
  1459. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1460. if (cflag & CRTSCTS) {
  1461. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1462. /* To set hardware flow control to the specified *
  1463. * serial port, in SP1/2_CONTROL_REG */
  1464. if (port->number)
  1465. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
  1466. else
  1467. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
  1468. } else
  1469. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1470. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1471. /* Determine divisor based on baud rate */
  1472. baud = tty_get_baud_rate(tty);
  1473. if (!baud) {
  1474. /* pick a default, any default... */
  1475. dbg("Picked default baud...");
  1476. baud = 9600;
  1477. }
  1478. if (baud >= 230400) {
  1479. set_higher_rates(mos7720_port, baud);
  1480. /* Enable Interrupts */
  1481. write_mos_reg(serial, port_number, IER, 0x0c);
  1482. return;
  1483. }
  1484. dbg("%s - baud rate = %d", __func__, baud);
  1485. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1486. /* FIXME: needs to write actual resulting baud back not just
  1487. blindly do so */
  1488. if (cflag & CBAUD)
  1489. tty_encode_baud_rate(tty, baud, baud);
  1490. /* Enable Interrupts */
  1491. write_mos_reg(serial, port_number, IER, 0x0c);
  1492. if (port->read_urb->status != -EINPROGRESS) {
  1493. port->read_urb->dev = serial->dev;
  1494. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1495. if (status)
  1496. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1497. status);
  1498. }
  1499. }
  1500. /*
  1501. * mos7720_set_termios
  1502. * this function is called by the tty driver when it wants to change the
  1503. * termios structure.
  1504. */
  1505. static void mos7720_set_termios(struct tty_struct *tty,
  1506. struct usb_serial_port *port, struct ktermios *old_termios)
  1507. {
  1508. int status;
  1509. unsigned int cflag;
  1510. struct usb_serial *serial;
  1511. struct moschip_port *mos7720_port;
  1512. serial = port->serial;
  1513. mos7720_port = usb_get_serial_port_data(port);
  1514. if (mos7720_port == NULL)
  1515. return;
  1516. if (!mos7720_port->open) {
  1517. dbg("%s - port not opened", __func__);
  1518. return;
  1519. }
  1520. dbg("%s\n", "setting termios - ASPIRE");
  1521. cflag = tty->termios->c_cflag;
  1522. dbg("%s - cflag %08x iflag %08x", __func__,
  1523. tty->termios->c_cflag,
  1524. RELEVANT_IFLAG(tty->termios->c_iflag));
  1525. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1526. old_termios->c_cflag,
  1527. RELEVANT_IFLAG(old_termios->c_iflag));
  1528. dbg("%s - port %d", __func__, port->number);
  1529. /* change the port settings to the new ones specified */
  1530. change_port_settings(tty, mos7720_port, old_termios);
  1531. if (port->read_urb->status != -EINPROGRESS) {
  1532. port->read_urb->dev = serial->dev;
  1533. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1534. if (status)
  1535. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1536. status);
  1537. }
  1538. }
  1539. /*
  1540. * get_lsr_info - get line status register info
  1541. *
  1542. * Purpose: Let user call ioctl() to get info when the UART physically
  1543. * is emptied. On bus types like RS485, the transmitter must
  1544. * release the bus after transmitting. This must be done when
  1545. * the transmit shift register is empty, not be done when the
  1546. * transmit holding register is empty. This functionality
  1547. * allows an RS485 driver to be written in user space.
  1548. */
  1549. static int get_lsr_info(struct tty_struct *tty,
  1550. struct moschip_port *mos7720_port, unsigned int __user *value)
  1551. {
  1552. struct usb_serial_port *port = tty->driver_data;
  1553. unsigned int result = 0;
  1554. unsigned char data = 0;
  1555. int port_number = port->number - port->serial->minor;
  1556. int count;
  1557. count = mos7720_chars_in_buffer(tty);
  1558. if (count == 0) {
  1559. read_mos_reg(port->serial, port_number, LSR, &data);
  1560. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1561. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1562. dbg("%s -- Empty", __func__);
  1563. result = TIOCSER_TEMT;
  1564. }
  1565. }
  1566. if (copy_to_user(value, &result, sizeof(int)))
  1567. return -EFAULT;
  1568. return 0;
  1569. }
  1570. static int mos7720_tiocmget(struct tty_struct *tty)
  1571. {
  1572. struct usb_serial_port *port = tty->driver_data;
  1573. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1574. unsigned int result = 0;
  1575. unsigned int mcr ;
  1576. unsigned int msr ;
  1577. dbg("%s - port %d", __func__, port->number);
  1578. mcr = mos7720_port->shadowMCR;
  1579. msr = mos7720_port->shadowMSR;
  1580. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1581. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1582. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1583. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1584. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1585. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1586. dbg("%s -- %x", __func__, result);
  1587. return result;
  1588. }
  1589. static int mos7720_tiocmset(struct tty_struct *tty,
  1590. unsigned int set, unsigned int clear)
  1591. {
  1592. struct usb_serial_port *port = tty->driver_data;
  1593. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1594. unsigned int mcr ;
  1595. dbg("%s - port %d", __func__, port->number);
  1596. dbg("he was at tiocmset");
  1597. mcr = mos7720_port->shadowMCR;
  1598. if (set & TIOCM_RTS)
  1599. mcr |= UART_MCR_RTS;
  1600. if (set & TIOCM_DTR)
  1601. mcr |= UART_MCR_DTR;
  1602. if (set & TIOCM_LOOP)
  1603. mcr |= UART_MCR_LOOP;
  1604. if (clear & TIOCM_RTS)
  1605. mcr &= ~UART_MCR_RTS;
  1606. if (clear & TIOCM_DTR)
  1607. mcr &= ~UART_MCR_DTR;
  1608. if (clear & TIOCM_LOOP)
  1609. mcr &= ~UART_MCR_LOOP;
  1610. mos7720_port->shadowMCR = mcr;
  1611. write_mos_reg(port->serial, port->number - port->serial->minor,
  1612. MCR, mos7720_port->shadowMCR);
  1613. return 0;
  1614. }
  1615. static int mos7720_get_icount(struct tty_struct *tty,
  1616. struct serial_icounter_struct *icount)
  1617. {
  1618. struct usb_serial_port *port = tty->driver_data;
  1619. struct moschip_port *mos7720_port;
  1620. struct async_icount cnow;
  1621. mos7720_port = usb_get_serial_port_data(port);
  1622. cnow = mos7720_port->icount;
  1623. icount->cts = cnow.cts;
  1624. icount->dsr = cnow.dsr;
  1625. icount->rng = cnow.rng;
  1626. icount->dcd = cnow.dcd;
  1627. icount->rx = cnow.rx;
  1628. icount->tx = cnow.tx;
  1629. icount->frame = cnow.frame;
  1630. icount->overrun = cnow.overrun;
  1631. icount->parity = cnow.parity;
  1632. icount->brk = cnow.brk;
  1633. icount->buf_overrun = cnow.buf_overrun;
  1634. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1635. port->number, icount->rx, icount->tx);
  1636. return 0;
  1637. }
  1638. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1639. unsigned int __user *value)
  1640. {
  1641. unsigned int mcr;
  1642. unsigned int arg;
  1643. struct usb_serial_port *port;
  1644. if (mos7720_port == NULL)
  1645. return -1;
  1646. port = (struct usb_serial_port *)mos7720_port->port;
  1647. mcr = mos7720_port->shadowMCR;
  1648. if (copy_from_user(&arg, value, sizeof(int)))
  1649. return -EFAULT;
  1650. switch (cmd) {
  1651. case TIOCMBIS:
  1652. if (arg & TIOCM_RTS)
  1653. mcr |= UART_MCR_RTS;
  1654. if (arg & TIOCM_DTR)
  1655. mcr |= UART_MCR_RTS;
  1656. if (arg & TIOCM_LOOP)
  1657. mcr |= UART_MCR_LOOP;
  1658. break;
  1659. case TIOCMBIC:
  1660. if (arg & TIOCM_RTS)
  1661. mcr &= ~UART_MCR_RTS;
  1662. if (arg & TIOCM_DTR)
  1663. mcr &= ~UART_MCR_RTS;
  1664. if (arg & TIOCM_LOOP)
  1665. mcr &= ~UART_MCR_LOOP;
  1666. break;
  1667. }
  1668. mos7720_port->shadowMCR = mcr;
  1669. write_mos_reg(port->serial, port->number - port->serial->minor,
  1670. MCR, mos7720_port->shadowMCR);
  1671. return 0;
  1672. }
  1673. static int get_serial_info(struct moschip_port *mos7720_port,
  1674. struct serial_struct __user *retinfo)
  1675. {
  1676. struct serial_struct tmp;
  1677. if (!retinfo)
  1678. return -EFAULT;
  1679. memset(&tmp, 0, sizeof(tmp));
  1680. tmp.type = PORT_16550A;
  1681. tmp.line = mos7720_port->port->serial->minor;
  1682. tmp.port = mos7720_port->port->number;
  1683. tmp.irq = 0;
  1684. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1685. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1686. tmp.baud_base = 9600;
  1687. tmp.close_delay = 5*HZ;
  1688. tmp.closing_wait = 30*HZ;
  1689. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1690. return -EFAULT;
  1691. return 0;
  1692. }
  1693. static int mos7720_ioctl(struct tty_struct *tty,
  1694. unsigned int cmd, unsigned long arg)
  1695. {
  1696. struct usb_serial_port *port = tty->driver_data;
  1697. struct moschip_port *mos7720_port;
  1698. struct async_icount cnow;
  1699. struct async_icount cprev;
  1700. mos7720_port = usb_get_serial_port_data(port);
  1701. if (mos7720_port == NULL)
  1702. return -ENODEV;
  1703. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1704. switch (cmd) {
  1705. case TIOCSERGETLSR:
  1706. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1707. return get_lsr_info(tty, mos7720_port,
  1708. (unsigned int __user *)arg);
  1709. /* FIXME: These should be using the mode methods */
  1710. case TIOCMBIS:
  1711. case TIOCMBIC:
  1712. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1713. __func__, port->number);
  1714. return set_modem_info(mos7720_port, cmd,
  1715. (unsigned int __user *)arg);
  1716. case TIOCGSERIAL:
  1717. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1718. return get_serial_info(mos7720_port,
  1719. (struct serial_struct __user *)arg);
  1720. case TIOCMIWAIT:
  1721. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1722. cprev = mos7720_port->icount;
  1723. while (1) {
  1724. if (signal_pending(current))
  1725. return -ERESTARTSYS;
  1726. cnow = mos7720_port->icount;
  1727. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1728. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1729. return -EIO; /* no change => error */
  1730. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1731. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1732. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1733. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1734. return 0;
  1735. }
  1736. cprev = cnow;
  1737. }
  1738. /* NOTREACHED */
  1739. break;
  1740. }
  1741. return -ENOIOCTLCMD;
  1742. }
  1743. static int mos7720_startup(struct usb_serial *serial)
  1744. {
  1745. struct moschip_port *mos7720_port;
  1746. struct usb_device *dev;
  1747. int i;
  1748. char data;
  1749. u16 product;
  1750. int ret_val;
  1751. dbg("%s: Entering ..........", __func__);
  1752. if (!serial) {
  1753. dbg("Invalid Handler");
  1754. return -ENODEV;
  1755. }
  1756. product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1757. dev = serial->dev;
  1758. /*
  1759. * The 7715 uses the first bulk in/out endpoint pair for the parallel
  1760. * port, and the second for the serial port. Because the usbserial core
  1761. * assumes both pairs are serial ports, we must engage in a bit of
  1762. * subterfuge and swap the pointers for ports 0 and 1 in order to make
  1763. * port 0 point to the serial port. However, both moschip devices use a
  1764. * single interrupt-in endpoint for both ports (as mentioned a little
  1765. * further down), and this endpoint was assigned to port 0. So after
  1766. * the swap, we must copy the interrupt endpoint elements from port 1
  1767. * (as newly assigned) to port 0, and null out port 1 pointers.
  1768. */
  1769. if (product == MOSCHIP_DEVICE_ID_7715) {
  1770. struct usb_serial_port *tmp = serial->port[0];
  1771. serial->port[0] = serial->port[1];
  1772. serial->port[1] = tmp;
  1773. serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
  1774. serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
  1775. serial->port[0]->interrupt_in_endpointAddress =
  1776. tmp->interrupt_in_endpointAddress;
  1777. serial->port[1]->interrupt_in_urb = NULL;
  1778. serial->port[1]->interrupt_in_buffer = NULL;
  1779. }
  1780. /* set up serial port private structures */
  1781. for (i = 0; i < serial->num_ports; ++i) {
  1782. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1783. if (mos7720_port == NULL) {
  1784. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1785. return -ENOMEM;
  1786. }
  1787. /* Initialize all port interrupt end point to port 0 int
  1788. * endpoint. Our device has only one interrupt endpoint
  1789. * common to all ports */
  1790. serial->port[i]->interrupt_in_endpointAddress =
  1791. serial->port[0]->interrupt_in_endpointAddress;
  1792. mos7720_port->port = serial->port[i];
  1793. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1794. dbg("port number is %d", serial->port[i]->number);
  1795. dbg("serial number is %d", serial->minor);
  1796. }
  1797. /* setting configuration feature to one */
  1798. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1799. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1800. /* start the interrupt urb */
  1801. ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
  1802. if (ret_val)
  1803. dev_err(&dev->dev,
  1804. "%s - Error %d submitting control urb\n",
  1805. __func__, ret_val);
  1806. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1807. if (product == MOSCHIP_DEVICE_ID_7715) {
  1808. ret_val = mos7715_parport_init(serial);
  1809. if (ret_val < 0)
  1810. return ret_val;
  1811. }
  1812. #endif
  1813. /* LSR For Port 1 */
  1814. read_mos_reg(serial, 0, LSR, &data);
  1815. dbg("LSR:%x", data);
  1816. return 0;
  1817. }
  1818. static void mos7720_release(struct usb_serial *serial)
  1819. {
  1820. int i;
  1821. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1822. /* close the parallel port */
  1823. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  1824. == MOSCHIP_DEVICE_ID_7715) {
  1825. struct urbtracker *urbtrack;
  1826. unsigned long flags;
  1827. struct mos7715_parport *mos_parport =
  1828. usb_get_serial_data(serial);
  1829. /* prevent NULL ptr dereference in port callbacks */
  1830. spin_lock(&release_lock);
  1831. mos_parport->pp->private_data = NULL;
  1832. spin_unlock(&release_lock);
  1833. /* wait for synchronous usb calls to return */
  1834. if (mos_parport->msg_pending)
  1835. wait_for_completion_timeout(&mos_parport->syncmsg_compl,
  1836. MOS_WDR_TIMEOUT);
  1837. parport_remove_port(mos_parport->pp);
  1838. usb_set_serial_data(serial, NULL);
  1839. mos_parport->serial = NULL;
  1840. /* if tasklet currently scheduled, wait for it to complete */
  1841. tasklet_kill(&mos_parport->urb_tasklet);
  1842. /* unlink any urbs sent by the tasklet */
  1843. spin_lock_irqsave(&mos_parport->listlock, flags);
  1844. list_for_each_entry(urbtrack,
  1845. &mos_parport->active_urbs,
  1846. urblist_entry)
  1847. usb_unlink_urb(urbtrack->urb);
  1848. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  1849. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  1850. }
  1851. #endif
  1852. /* free private structure allocated for serial port */
  1853. for (i = 0; i < serial->num_ports; ++i)
  1854. kfree(usb_get_serial_port_data(serial->port[i]));
  1855. }
  1856. static struct usb_driver usb_driver = {
  1857. .name = "moschip7720",
  1858. .probe = usb_serial_probe,
  1859. .disconnect = usb_serial_disconnect,
  1860. .id_table = moschip_port_id_table,
  1861. .no_dynamic_id = 1,
  1862. };
  1863. static struct usb_serial_driver moschip7720_2port_driver = {
  1864. .driver = {
  1865. .owner = THIS_MODULE,
  1866. .name = "moschip7720",
  1867. },
  1868. .description = "Moschip 2 port adapter",
  1869. .usb_driver = &usb_driver,
  1870. .id_table = moschip_port_id_table,
  1871. .calc_num_ports = mos77xx_calc_num_ports,
  1872. .open = mos7720_open,
  1873. .close = mos7720_close,
  1874. .throttle = mos7720_throttle,
  1875. .unthrottle = mos7720_unthrottle,
  1876. .probe = mos77xx_probe,
  1877. .attach = mos7720_startup,
  1878. .release = mos7720_release,
  1879. .ioctl = mos7720_ioctl,
  1880. .tiocmget = mos7720_tiocmget,
  1881. .tiocmset = mos7720_tiocmset,
  1882. .get_icount = mos7720_get_icount,
  1883. .set_termios = mos7720_set_termios,
  1884. .write = mos7720_write,
  1885. .write_room = mos7720_write_room,
  1886. .chars_in_buffer = mos7720_chars_in_buffer,
  1887. .break_ctl = mos7720_break,
  1888. .read_bulk_callback = mos7720_bulk_in_callback,
  1889. .read_int_callback = NULL /* dynamically assigned in probe() */
  1890. };
  1891. static int __init moschip7720_init(void)
  1892. {
  1893. int retval;
  1894. dbg("%s: Entering ..........", __func__);
  1895. /* Register with the usb serial */
  1896. retval = usb_serial_register(&moschip7720_2port_driver);
  1897. if (retval)
  1898. goto failed_port_device_register;
  1899. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1900. DRIVER_DESC "\n");
  1901. /* Register with the usb */
  1902. retval = usb_register(&usb_driver);
  1903. if (retval)
  1904. goto failed_usb_register;
  1905. return 0;
  1906. failed_usb_register:
  1907. usb_serial_deregister(&moschip7720_2port_driver);
  1908. failed_port_device_register:
  1909. return retval;
  1910. }
  1911. static void __exit moschip7720_exit(void)
  1912. {
  1913. usb_deregister(&usb_driver);
  1914. usb_serial_deregister(&moschip7720_2port_driver);
  1915. }
  1916. module_init(moschip7720_init);
  1917. module_exit(moschip7720_exit);
  1918. /* Module information */
  1919. MODULE_AUTHOR(DRIVER_AUTHOR);
  1920. MODULE_DESCRIPTION(DRIVER_DESC);
  1921. MODULE_LICENSE("GPL");
  1922. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1923. MODULE_PARM_DESC(debug, "Debug enabled or not");