mos7720.c 61 KB

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