mos7720.c 57 KB

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