mos7720.c 58 KB

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