mos7720.c 57 KB

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