mos7720.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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. * VijayaKumar.G.N. <vijaykumar@aspirecom.net>
  13. * AjayKumar <ajay@aspirecom.net>
  14. * Gurudeva.N. <gurudev@aspirecom.net>
  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 <asm/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port
  61. {
  62. __u8 shadowLCR; /* last LCR value received */
  63. __u8 shadowMCR; /* last MCR value received */
  64. __u8 shadowMSR; /* last MSR value received */
  65. char open;
  66. struct async_icount icount;
  67. struct usb_serial_port *port; /* loop back to the owner */
  68. struct urb *write_urb_pool[NUM_URBS];
  69. };
  70. /* This structure holds all of the individual serial device information */
  71. struct moschip_serial
  72. {
  73. int interrupt_started;
  74. };
  75. static int debug;
  76. #define USB_VENDOR_ID_MOSCHIP 0x9710
  77. #define MOSCHIP_DEVICE_ID_7720 0x7720
  78. #define MOSCHIP_DEVICE_ID_7715 0x7715
  79. static struct usb_device_id moschip_port_id_table [] = {
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. __u32 *data;
  94. unsigned int status;
  95. __u8 sp1;
  96. __u8 sp2;
  97. __u8 st;
  98. dbg("%s"," : Entering\n");
  99. if (!urb) {
  100. dbg("%s","Invalid Pointer !!!!:\n");
  101. return;
  102. }
  103. switch (urb->status) {
  104. case 0:
  105. /* success */
  106. break;
  107. case -ECONNRESET:
  108. case -ENOENT:
  109. case -ESHUTDOWN:
  110. /* this urb is terminated, clean up */
  111. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  112. urb->status);
  113. return;
  114. default:
  115. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  116. urb->status);
  117. goto exit;
  118. }
  119. length = urb->actual_length;
  120. data = urb->transfer_buffer;
  121. /* Moschip get 4 bytes
  122. * Byte 1 IIR Port 1 (port.number is 0)
  123. * Byte 2 IIR Port 2 (port.number is 1)
  124. * Byte 3 --------------
  125. * Byte 4 FIFO status for both */
  126. if (length && length > 4) {
  127. dbg("Wrong data !!!");
  128. return;
  129. }
  130. status = *data;
  131. sp1 = (status & 0xff000000)>>24;
  132. sp2 = (status & 0x00ff0000)>>16;
  133. st = status & 0x000000ff;
  134. if ((sp1 & 0x01) || (sp2 & 0x01)) {
  135. /* No Interrupt Pending in both the ports */
  136. dbg("No Interrupt !!!");
  137. } else {
  138. switch (sp1 & 0x0f) {
  139. case SERIAL_IIR_RLS:
  140. dbg("Serial Port 1: Receiver status error or address "
  141. "bit detected in 9-bit mode\n");
  142. break;
  143. case SERIAL_IIR_CTI:
  144. dbg("Serial Port 1: Receiver time out");
  145. break;
  146. case SERIAL_IIR_MS:
  147. dbg("Serial Port 1: Modem status change");
  148. break;
  149. }
  150. switch (sp2 & 0x0f) {
  151. case SERIAL_IIR_RLS:
  152. dbg("Serial Port 2: Receiver status error or address "
  153. "bit detected in 9-bit mode");
  154. break;
  155. case SERIAL_IIR_CTI:
  156. dbg("Serial Port 2: Receiver time out");
  157. break;
  158. case SERIAL_IIR_MS:
  159. dbg("Serial Port 2: Modem status change");
  160. break;
  161. }
  162. }
  163. exit:
  164. result = usb_submit_urb(urb, GFP_ATOMIC);
  165. if (result)
  166. dev_err(&urb->dev->dev,
  167. "%s - Error %d submitting control urb\n",
  168. __FUNCTION__, result);
  169. return;
  170. }
  171. /*
  172. * mos7720_bulk_in_callback
  173. * this is the callback function for when we have received data on the
  174. * bulk in endpoint.
  175. */
  176. static void mos7720_bulk_in_callback(struct urb *urb)
  177. {
  178. int status;
  179. unsigned char *data ;
  180. struct usb_serial_port *port;
  181. struct moschip_port *mos7720_port;
  182. struct tty_struct *tty;
  183. if (urb->status) {
  184. dbg("nonzero read bulk status received: %d",urb->status);
  185. return;
  186. }
  187. mos7720_port = urb->context;
  188. if (!mos7720_port) {
  189. dbg("%s","NULL mos7720_port pointer \n");
  190. return ;
  191. }
  192. port = mos7720_port->port;
  193. dbg("Entering...%s", __FUNCTION__);
  194. data = urb->transfer_buffer;
  195. tty = port->tty;
  196. if (tty && urb->actual_length) {
  197. tty_buffer_request_room(tty, urb->actual_length);
  198. tty_insert_flip_string(tty, data, urb->actual_length);
  199. tty_flip_buffer_push(tty);
  200. }
  201. if (!port->read_urb) {
  202. dbg("URB KILLED !!!");
  203. return;
  204. }
  205. if (port->read_urb->status != -EINPROGRESS) {
  206. port->read_urb->dev = port->serial->dev;
  207. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  208. if (status)
  209. dbg("usb_submit_urb(read bulk) failed, status = %d",
  210. status);
  211. }
  212. }
  213. /*
  214. * mos7720_bulk_out_data_callback
  215. * this is the callback function for when we have finished sending serial
  216. * data on the bulk out endpoint.
  217. */
  218. static void mos7720_bulk_out_data_callback(struct urb *urb)
  219. {
  220. struct moschip_port *mos7720_port;
  221. struct tty_struct *tty;
  222. if (urb->status) {
  223. dbg("nonzero write bulk status received:%d", urb->status);
  224. return;
  225. }
  226. mos7720_port = urb->context;
  227. if (!mos7720_port) {
  228. dbg("NULL mos7720_port pointer");
  229. return ;
  230. }
  231. dbg("Entering .........");
  232. tty = mos7720_port->port->tty;
  233. if (tty && mos7720_port->open) {
  234. /* let the tty driver wakeup if it has a special *
  235. * write_wakeup function */
  236. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  237. tty->ldisc.write_wakeup)
  238. (tty->ldisc.write_wakeup)(tty);
  239. /* tell the tty driver that something has changed */
  240. wake_up_interruptible(&tty->write_wait);
  241. }
  242. /* schedule_work(&mos7720_port->port->work); */
  243. }
  244. /*
  245. * send_mos_cmd
  246. * this function will be used for sending command to device
  247. */
  248. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  249. __u16 index, void *data)
  250. {
  251. int status;
  252. unsigned int pipe;
  253. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  254. __u8 requesttype;
  255. __u16 size = 0x0000;
  256. if (value < MOS_MAX_PORT) {
  257. if (product == MOSCHIP_DEVICE_ID_7715) {
  258. value = value*0x100+0x100;
  259. } else {
  260. value = value*0x100+0x200;
  261. }
  262. } else {
  263. value = 0x0000;
  264. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  265. (index != 0x08)) {
  266. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  267. //index = 0x01 ;
  268. }
  269. }
  270. if (request == MOS_WRITE) {
  271. request = (__u8)MOS_WRITE;
  272. requesttype = (__u8)0x40;
  273. value = value + (__u16)*((unsigned char *)data);
  274. data = NULL;
  275. pipe = usb_sndctrlpipe(serial->dev, 0);
  276. } else {
  277. request = (__u8)MOS_READ;
  278. requesttype = (__u8)0xC0;
  279. size = 0x01;
  280. pipe = usb_rcvctrlpipe(serial->dev,0);
  281. }
  282. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  283. value, index, data, size, MOS_WDR_TIMEOUT);
  284. if (status < 0)
  285. dbg("Command Write failed Value %x index %x\n",value,index);
  286. return status;
  287. }
  288. static int mos7720_open(struct usb_serial_port *port, struct file * filp)
  289. {
  290. struct usb_serial *serial;
  291. struct usb_serial_port *port0;
  292. struct urb *urb;
  293. struct moschip_serial *mos7720_serial;
  294. struct moschip_port *mos7720_port;
  295. int response;
  296. int port_number;
  297. char data;
  298. int j;
  299. serial = port->serial;
  300. mos7720_port = usb_get_serial_port_data(port);
  301. if (mos7720_port == NULL)
  302. return -ENODEV;
  303. port0 = serial->port[0];
  304. mos7720_serial = usb_get_serial_data(serial);
  305. if (mos7720_serial == NULL || port0 == NULL)
  306. return -ENODEV;
  307. usb_clear_halt(serial->dev, port->write_urb->pipe);
  308. usb_clear_halt(serial->dev, port->read_urb->pipe);
  309. /* Initialising the write urb pool */
  310. for (j = 0; j < NUM_URBS; ++j) {
  311. urb = usb_alloc_urb(0,SLAB_ATOMIC);
  312. mos7720_port->write_urb_pool[j] = urb;
  313. if (urb == NULL) {
  314. err("No more urbs???");
  315. continue;
  316. }
  317. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  318. GFP_KERNEL);
  319. if (!urb->transfer_buffer) {
  320. err("%s-out of memory for urb buffers.", __FUNCTION__);
  321. continue;
  322. }
  323. }
  324. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  325. *
  326. * Register Index
  327. * 1 : IER
  328. * 2 : FCR
  329. * 3 : LCR
  330. * 4 : MCR
  331. *
  332. * 0x08 : SP1/2 Control Reg
  333. */
  334. port_number = port->number - port->serial->minor;
  335. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  336. dbg("SS::%p LSR:%x\n",mos7720_port, data);
  337. dbg("Check:Sending Command ..........");
  338. data = 0x02;
  339. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  340. data = 0x02;
  341. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  342. data = 0x00;
  343. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  344. data = 0x00;
  345. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  346. data = 0xCF;
  347. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  348. data = 0x03;
  349. mos7720_port->shadowLCR = data;
  350. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  351. data = 0x0b;
  352. mos7720_port->shadowMCR = data;
  353. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  354. data = 0x0b;
  355. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  356. data = 0x00;
  357. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  358. data = 0x00;
  359. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  360. /* data = 0x00;
  361. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  362. data = 0x03;
  363. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  364. data = 0x00;
  365. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  366. */
  367. data = 0x00;
  368. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  369. data = data | (port->number - port->serial->minor + 1);
  370. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  371. data = 0x83;
  372. mos7720_port->shadowLCR = data;
  373. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  374. data = 0x0c;
  375. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  376. data = 0x00;
  377. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  378. data = 0x03;
  379. mos7720_port->shadowLCR = data;
  380. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  381. data = 0x0c;
  382. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  383. data = 0x0c;
  384. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  385. //Matrix
  386. /* force low_latency on so that our tty_push actually forces *
  387. * the data through,otherwise it is scheduled, and with *
  388. * high data rates (like with OHCI) data can get lost. */
  389. if (port->tty)
  390. port->tty->low_latency = 1;
  391. /* see if we've set up our endpoint info yet *
  392. * (can't set it up in mos7720_startup as the *
  393. * structures were not set up at that time.) */
  394. if (!mos7720_serial->interrupt_started) {
  395. dbg("Interrupt buffer NULL !!!");
  396. /* not set up yet, so do it now */
  397. mos7720_serial->interrupt_started = 1;
  398. dbg("To Submit URB !!!");
  399. /* set up our interrupt urb */
  400. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  401. usb_rcvintpipe(serial->dev,
  402. port->interrupt_in_endpointAddress),
  403. port0->interrupt_in_buffer,
  404. port0->interrupt_in_urb->transfer_buffer_length,
  405. mos7720_interrupt_callback, mos7720_port,
  406. port0->interrupt_in_urb->interval);
  407. /* start interrupt read for this mos7720 this interrupt *
  408. * will continue as long as the mos7720 is connected */
  409. dbg("Submit URB over !!!");
  410. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  411. if (response)
  412. dev_err(&port->dev,
  413. "%s - Error %d submitting control urb",
  414. __FUNCTION__, response);
  415. }
  416. /* set up our bulk in urb */
  417. usb_fill_bulk_urb(port->read_urb, serial->dev,
  418. usb_rcvbulkpipe(serial->dev,
  419. port->bulk_in_endpointAddress),
  420. port->bulk_in_buffer,
  421. port->read_urb->transfer_buffer_length,
  422. mos7720_bulk_in_callback, mos7720_port);
  423. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  424. if (response)
  425. dev_err(&port->dev,
  426. "%s - Error %d submitting read urb", __FUNCTION__, response);
  427. /* initialize our icount structure */
  428. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  429. /* initialize our port settings */
  430. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  431. /* send a open port command */
  432. mos7720_port->open = 1;
  433. return 0;
  434. }
  435. /*
  436. * mos7720_chars_in_buffer
  437. * this function is called by the tty driver when it wants to know how many
  438. * bytes of data we currently have outstanding in the port (data that has
  439. * been written, but hasn't made it out the port yet)
  440. * If successful, we return the number of bytes left to be written in the
  441. * system,
  442. * Otherwise we return a negative error number.
  443. */
  444. static int mos7720_chars_in_buffer(struct usb_serial_port *port)
  445. {
  446. int i;
  447. int chars = 0;
  448. struct moschip_port *mos7720_port;
  449. dbg("%s:entering ...........", __FUNCTION__);
  450. mos7720_port = usb_get_serial_port_data(port);
  451. if (mos7720_port == NULL) {
  452. dbg("%s:leaving ...........", __FUNCTION__);
  453. return -ENODEV;
  454. }
  455. for (i = 0; i < NUM_URBS; ++i) {
  456. if (mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  457. chars += URB_TRANSFER_BUFFER_SIZE;
  458. }
  459. dbg("%s - returns %d", __FUNCTION__, chars);
  460. return chars;
  461. }
  462. static void mos7720_close(struct usb_serial_port *port, struct file *filp)
  463. {
  464. struct usb_serial *serial;
  465. struct moschip_port *mos7720_port;
  466. char data;
  467. int j;
  468. dbg("mos7720_close:entering...");
  469. serial = port->serial;
  470. mos7720_port = usb_get_serial_port_data(port);
  471. if (mos7720_port == NULL)
  472. return;
  473. for (j = 0; j < NUM_URBS; ++j)
  474. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  475. /* Freeing Write URBs */
  476. for (j = 0; j < NUM_URBS; ++j) {
  477. if (mos7720_port->write_urb_pool[j]) {
  478. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  479. usb_free_urb(mos7720_port->write_urb_pool[j]);
  480. }
  481. }
  482. /* While closing port, shutdown all bulk read, write *
  483. * and interrupt read if they exists */
  484. if (serial->dev) {
  485. dbg("Shutdown bulk write");
  486. usb_kill_urb(port->write_urb);
  487. dbg("Shutdown bulk read");
  488. usb_kill_urb(port->read_urb);
  489. }
  490. data = 0x00;
  491. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  492. 0x04, &data);
  493. data = 0x00;
  494. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  495. 0x01, &data);
  496. mos7720_port->open = 0;
  497. dbg("Leaving %s", __FUNCTION__);
  498. }
  499. static void mos7720_break(struct usb_serial_port *port, int break_state)
  500. {
  501. unsigned char data;
  502. struct usb_serial *serial;
  503. struct moschip_port *mos7720_port;
  504. dbg("Entering %s", __FUNCTION__);
  505. serial = port->serial;
  506. mos7720_port = usb_get_serial_port_data(port);
  507. if (mos7720_port == NULL)
  508. return;
  509. if (break_state == -1)
  510. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  511. else
  512. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  513. mos7720_port->shadowLCR = data;
  514. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  515. 0x03, &data);
  516. return;
  517. }
  518. /*
  519. * mos7720_write_room
  520. * this function is called by the tty driver when it wants to know how many
  521. * bytes of data we can accept for a specific port.
  522. * If successful, we return the amount of room that we have for this port
  523. * Otherwise we return a negative error number.
  524. */
  525. static int mos7720_write_room(struct usb_serial_port *port)
  526. {
  527. struct moschip_port *mos7720_port;
  528. int room = 0;
  529. int i;
  530. dbg("%s:entering ...........", __FUNCTION__);
  531. mos7720_port = usb_get_serial_port_data(port);
  532. if (mos7720_port == NULL) {
  533. dbg("%s:leaving ...........", __FUNCTION__);
  534. return -ENODEV;
  535. }
  536. for (i = 0; i < NUM_URBS; ++i) {
  537. if (mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  538. room += URB_TRANSFER_BUFFER_SIZE;
  539. }
  540. dbg("%s - returns %d", __FUNCTION__, room);
  541. return room;
  542. }
  543. static int mos7720_write(struct usb_serial_port *port,
  544. const unsigned char *data, int count)
  545. {
  546. int status;
  547. int i;
  548. int bytes_sent = 0;
  549. int transfer_size;
  550. struct moschip_port *mos7720_port;
  551. struct usb_serial *serial;
  552. struct urb *urb;
  553. const unsigned char *current_position = data;
  554. dbg("%s:entering ...........", __FUNCTION__);
  555. serial = port->serial;
  556. mos7720_port = usb_get_serial_port_data(port);
  557. if (mos7720_port == NULL) {
  558. dbg("mos7720_port is NULL");
  559. return -ENODEV;
  560. }
  561. /* try to find a free urb in the list */
  562. urb = NULL;
  563. for (i = 0; i < NUM_URBS; ++i) {
  564. if (mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  565. urb = mos7720_port->write_urb_pool[i];
  566. dbg("URB:%d",i);
  567. break;
  568. }
  569. }
  570. if (urb == NULL) {
  571. dbg("%s - no more free urbs", __FUNCTION__);
  572. goto exit;
  573. }
  574. if (urb->transfer_buffer == NULL) {
  575. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  576. GFP_KERNEL);
  577. if (urb->transfer_buffer == NULL) {
  578. err("%s no more kernel memory...", __FUNCTION__);
  579. goto exit;
  580. }
  581. }
  582. transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
  583. memcpy(urb->transfer_buffer, current_position, transfer_size);
  584. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
  585. urb->transfer_buffer);
  586. /* fill urb with data and submit */
  587. usb_fill_bulk_urb(urb, serial->dev,
  588. usb_sndbulkpipe(serial->dev,
  589. port->bulk_out_endpointAddress),
  590. urb->transfer_buffer, transfer_size,
  591. mos7720_bulk_out_data_callback, mos7720_port);
  592. /* send it down the pipe */
  593. status = usb_submit_urb(urb,GFP_ATOMIC);
  594. if (status) {
  595. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  596. __FUNCTION__, status);
  597. bytes_sent = status;
  598. goto exit;
  599. }
  600. bytes_sent = transfer_size;
  601. exit:
  602. return bytes_sent;
  603. }
  604. static void mos7720_throttle(struct usb_serial_port *port)
  605. {
  606. struct moschip_port *mos7720_port;
  607. struct tty_struct *tty;
  608. int status;
  609. dbg("%s- port %d\n", __FUNCTION__, port->number);
  610. mos7720_port = usb_get_serial_port_data(port);
  611. if (mos7720_port == NULL)
  612. return;
  613. if (!mos7720_port->open) {
  614. dbg("port not opened");
  615. return;
  616. }
  617. dbg("%s: Entering ..........", __FUNCTION__);
  618. tty = port->tty;
  619. if (!tty) {
  620. dbg("%s - no tty available", __FUNCTION__);
  621. return;
  622. }
  623. /* if we are implementing XON/XOFF, send the stop character */
  624. if (I_IXOFF(tty)) {
  625. unsigned char stop_char = STOP_CHAR(tty);
  626. status = mos7720_write(port, &stop_char, 1);
  627. if (status <= 0)
  628. return;
  629. }
  630. /* if we are implementing RTS/CTS, toggle that line */
  631. if (tty->termios->c_cflag & CRTSCTS) {
  632. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  633. status = send_mos_cmd(port->serial, MOS_WRITE,
  634. port->number - port->serial->minor,
  635. UART_MCR, &mos7720_port->shadowMCR);
  636. if (status != 0)
  637. return;
  638. }
  639. }
  640. static void mos7720_unthrottle(struct usb_serial_port *port)
  641. {
  642. struct tty_struct *tty;
  643. int status;
  644. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  645. if (mos7720_port == NULL)
  646. return;
  647. if (!mos7720_port->open) {
  648. dbg("%s - port not opened", __FUNCTION__);
  649. return;
  650. }
  651. dbg("%s: Entering ..........", __FUNCTION__);
  652. tty = port->tty;
  653. if (!tty) {
  654. dbg("%s - no tty available", __FUNCTION__);
  655. return;
  656. }
  657. /* if we are implementing XON/XOFF, send the start character */
  658. if (I_IXOFF(tty)) {
  659. unsigned char start_char = START_CHAR(tty);
  660. status = mos7720_write(port, &start_char, 1);
  661. if (status <= 0)
  662. return;
  663. }
  664. /* if we are implementing RTS/CTS, toggle that line */
  665. if (tty->termios->c_cflag & CRTSCTS) {
  666. mos7720_port->shadowMCR |= UART_MCR_RTS;
  667. status = send_mos_cmd(port->serial, MOS_WRITE,
  668. port->number - port->serial->minor,
  669. UART_MCR, &mos7720_port->shadowMCR);
  670. if (status != 0)
  671. return;
  672. }
  673. }
  674. static int set_higher_rates(struct moschip_port *mos7720_port,
  675. unsigned int baud)
  676. {
  677. unsigned char data;
  678. struct usb_serial_port *port;
  679. struct usb_serial *serial;
  680. int port_number;
  681. if (mos7720_port == NULL)
  682. return -EINVAL;
  683. port = mos7720_port->port;
  684. serial = port->serial;
  685. /***********************************************
  686. * Init Sequence for higher rates
  687. ***********************************************/
  688. dbg("Sending Setting Commands ..........");
  689. port_number = port->number - port->serial->minor;
  690. data = 0x000;
  691. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  692. data = 0x000;
  693. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  694. data = 0x0CF;
  695. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  696. data = 0x00b;
  697. mos7720_port->shadowMCR = data;
  698. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  699. data = 0x00b;
  700. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  701. data = 0x000;
  702. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  703. data = 0x000;
  704. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  705. /***********************************************
  706. * Set for higher rates *
  707. ***********************************************/
  708. data = baud * 0x10;
  709. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
  710. data = 0x003;
  711. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  712. data = 0x003;
  713. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  714. data = 0x02b;
  715. mos7720_port->shadowMCR = data;
  716. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  717. data = 0x02b;
  718. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  719. /***********************************************
  720. * Set DLL/DLM
  721. ***********************************************/
  722. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  723. mos7720_port->shadowLCR = data;
  724. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  725. data = 0x001; /* DLL */
  726. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  727. data = 0x000; /* DLM */
  728. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  729. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  730. mos7720_port->shadowLCR = data;
  731. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  732. return 0;
  733. }
  734. /* baud rate information */
  735. struct divisor_table_entry
  736. {
  737. __u32 baudrate;
  738. __u16 divisor;
  739. };
  740. /* Define table of divisors for moschip 7720 hardware *
  741. * These assume a 3.6864MHz crystal, the standard /16, and *
  742. * MCR.7 = 0. */
  743. static struct divisor_table_entry divisor_table[] = {
  744. { 50, 2304},
  745. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  746. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  747. { 150, 768},
  748. { 300, 384},
  749. { 600, 192},
  750. { 1200, 96},
  751. { 1800, 64},
  752. { 2400, 48},
  753. { 4800, 24},
  754. { 7200, 16},
  755. { 9600, 12},
  756. { 19200, 6},
  757. { 38400, 3},
  758. { 57600, 2},
  759. { 115200, 1},
  760. };
  761. /*****************************************************************************
  762. * calc_baud_rate_divisor
  763. * this function calculates the proper baud rate divisor for the specified
  764. * baud rate.
  765. *****************************************************************************/
  766. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  767. {
  768. int i;
  769. __u16 custom;
  770. __u16 round1;
  771. __u16 round;
  772. dbg("%s - %d", __FUNCTION__, baudrate);
  773. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  774. if (divisor_table[i].baudrate == baudrate) {
  775. *divisor = divisor_table[i].divisor;
  776. return 0;
  777. }
  778. }
  779. /* After trying for all the standard baud rates *
  780. * Try calculating the divisor for this baud rate */
  781. if (baudrate > 75 && baudrate < 230400) {
  782. /* get the divisor */
  783. custom = (__u16)(230400L / baudrate);
  784. /* Check for round off */
  785. round1 = (__u16)(2304000L / baudrate);
  786. round = (__u16)(round1 - (custom * 10));
  787. if (round > 4)
  788. custom++;
  789. *divisor = custom;
  790. dbg("Baud %d = %d",baudrate, custom);
  791. return 0;
  792. }
  793. dbg("Baud calculation Failed...");
  794. return -EINVAL;
  795. }
  796. /*
  797. * send_cmd_write_baud_rate
  798. * this function sends the proper command to change the baud rate of the
  799. * specified port.
  800. */
  801. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  802. int baudrate)
  803. {
  804. struct usb_serial_port *port;
  805. struct usb_serial *serial;
  806. int divisor;
  807. int status;
  808. unsigned char data;
  809. unsigned char number;
  810. if (mos7720_port == NULL)
  811. return -1;
  812. port = mos7720_port->port;
  813. serial = port->serial;
  814. dbg("%s: Entering ..........", __FUNCTION__);
  815. number = port->number - port->serial->minor;
  816. dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
  817. /* Calculate the Divisor */
  818. status = calc_baud_rate_divisor(baudrate, &divisor);
  819. if (status) {
  820. err("%s - bad baud rate", __FUNCTION__);
  821. return status;
  822. }
  823. /* Enable access to divisor latch */
  824. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  825. mos7720_port->shadowLCR = data;
  826. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  827. /* Write the divisor */
  828. data = ((unsigned char)(divisor & 0xff));
  829. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  830. data = ((unsigned char)((divisor & 0xff00) >> 8));
  831. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  832. /* Disable access to divisor latch */
  833. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  834. mos7720_port->shadowLCR = data;
  835. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  836. return status;
  837. }
  838. /*
  839. * change_port_settings
  840. * This routine is called to set the UART on the device to match
  841. * the specified new settings.
  842. */
  843. static void change_port_settings(struct moschip_port *mos7720_port,
  844. struct termios *old_termios)
  845. {
  846. struct usb_serial_port *port;
  847. struct usb_serial *serial;
  848. struct tty_struct *tty;
  849. int baud;
  850. unsigned cflag;
  851. unsigned iflag;
  852. __u8 mask = 0xff;
  853. __u8 lData;
  854. __u8 lParity;
  855. __u8 lStop;
  856. int status;
  857. int port_number;
  858. char data;
  859. if (mos7720_port == NULL)
  860. return ;
  861. port = mos7720_port->port;
  862. serial = port->serial;
  863. port_number = port->number - port->serial->minor;
  864. dbg("%s - port %d", __FUNCTION__, port->number);
  865. if (!mos7720_port->open) {
  866. dbg("%s - port not opened", __FUNCTION__);
  867. return;
  868. }
  869. tty = mos7720_port->port->tty;
  870. if ((!tty) || (!tty->termios)) {
  871. dbg("%s - no tty structures", __FUNCTION__);
  872. return;
  873. }
  874. dbg("%s: Entering ..........", __FUNCTION__);
  875. lData = UART_LCR_WLEN8;
  876. lStop = 0x00; /* 1 stop bit */
  877. lParity = 0x00; /* No parity */
  878. cflag = tty->termios->c_cflag;
  879. iflag = tty->termios->c_iflag;
  880. /* Change the number of bits */
  881. switch (cflag & CSIZE) {
  882. case CS5:
  883. lData = UART_LCR_WLEN5;
  884. mask = 0x1f;
  885. break;
  886. case CS6:
  887. lData = UART_LCR_WLEN6;
  888. mask = 0x3f;
  889. break;
  890. case CS7:
  891. lData = UART_LCR_WLEN7;
  892. mask = 0x7f;
  893. break;
  894. default:
  895. case CS8:
  896. lData = UART_LCR_WLEN8;
  897. break;
  898. }
  899. /* Change the Parity bit */
  900. if (cflag & PARENB) {
  901. if (cflag & PARODD) {
  902. lParity = UART_LCR_PARITY;
  903. dbg("%s - parity = odd", __FUNCTION__);
  904. } else {
  905. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  906. dbg("%s - parity = even", __FUNCTION__);
  907. }
  908. } else {
  909. dbg("%s - parity = none", __FUNCTION__);
  910. }
  911. if (cflag & CMSPAR)
  912. lParity = lParity | 0x20;
  913. /* Change the Stop bit */
  914. if (cflag & CSTOPB) {
  915. lStop = UART_LCR_STOP;
  916. dbg("%s - stop bits = 2", __FUNCTION__);
  917. } else {
  918. lStop = 0x00;
  919. dbg("%s - stop bits = 1", __FUNCTION__);
  920. }
  921. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  922. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  923. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  924. /* Update the LCR with the correct value */
  925. mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  926. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  927. /* Disable Interrupts */
  928. data = 0x00;
  929. send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
  930. data = 0x00;
  931. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  932. data = 0xcf;
  933. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  934. /* Send the updated LCR value to the mos7720 */
  935. data = mos7720_port->shadowLCR;
  936. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  937. data = 0x00b;
  938. mos7720_port->shadowMCR = data;
  939. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  940. data = 0x00b;
  941. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  942. /* set up the MCR register and send it to the mos7720 */
  943. mos7720_port->shadowMCR = UART_MCR_OUT2;
  944. if (cflag & CBAUD)
  945. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  946. if (cflag & CRTSCTS) {
  947. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  948. /* To set hardware flow control to the specified *
  949. * serial port, in SP1/2_CONTROL_REG */
  950. if (port->number) {
  951. data = 0x001;
  952. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  953. 0x08, &data);
  954. } else {
  955. data = 0x002;
  956. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  957. 0x08, &data);
  958. }
  959. } else {
  960. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  961. }
  962. data = mos7720_port->shadowMCR;
  963. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  964. /* Determine divisor based on baud rate */
  965. baud = tty_get_baud_rate(tty);
  966. if (!baud) {
  967. /* pick a default, any default... */
  968. dbg("Picked default baud...");
  969. baud = 9600;
  970. }
  971. if (baud >= 230400) {
  972. set_higher_rates(mos7720_port, baud);
  973. /* Enable Interrupts */
  974. data = 0x0c;
  975. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  976. return;
  977. }
  978. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  979. status = send_cmd_write_baud_rate(mos7720_port, baud);
  980. /* Enable Interrupts */
  981. data = 0x0c;
  982. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  983. if (port->read_urb->status != -EINPROGRESS) {
  984. port->read_urb->dev = serial->dev;
  985. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  986. if (status)
  987. dbg("usb_submit_urb(read bulk) failed, status = %d",
  988. status);
  989. }
  990. return;
  991. }
  992. /*
  993. * mos7720_set_termios
  994. * this function is called by the tty driver when it wants to change the
  995. * termios structure.
  996. */
  997. static void mos7720_set_termios(struct usb_serial_port *port,
  998. struct termios *old_termios)
  999. {
  1000. int status;
  1001. unsigned int cflag;
  1002. struct usb_serial *serial;
  1003. struct moschip_port *mos7720_port;
  1004. struct tty_struct *tty;
  1005. serial = port->serial;
  1006. mos7720_port = usb_get_serial_port_data(port);
  1007. if (mos7720_port == NULL)
  1008. return;
  1009. tty = port->tty;
  1010. if (!port->tty || !port->tty->termios) {
  1011. dbg("%s - no tty or termios", __FUNCTION__);
  1012. return;
  1013. }
  1014. if (!mos7720_port->open) {
  1015. dbg("%s - port not opened", __FUNCTION__);
  1016. return;
  1017. }
  1018. dbg("%s\n","setting termios - ASPIRE");
  1019. cflag = tty->termios->c_cflag;
  1020. if (!cflag) {
  1021. printk("%s %s\n",__FUNCTION__,"cflag is NULL");
  1022. return;
  1023. }
  1024. /* check that they really want us to change something */
  1025. if (old_termios) {
  1026. if ((cflag == old_termios->c_cflag) &&
  1027. (RELEVANT_IFLAG(tty->termios->c_iflag) ==
  1028. RELEVANT_IFLAG(old_termios->c_iflag))) {
  1029. dbg("Nothing to change");
  1030. return;
  1031. }
  1032. }
  1033. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1034. tty->termios->c_cflag,
  1035. RELEVANT_IFLAG(tty->termios->c_iflag));
  1036. if (old_termios)
  1037. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1038. old_termios->c_cflag,
  1039. RELEVANT_IFLAG(old_termios->c_iflag));
  1040. dbg("%s - port %d", __FUNCTION__, port->number);
  1041. /* change the port settings to the new ones specified */
  1042. change_port_settings(mos7720_port, old_termios);
  1043. if(!port->read_urb) {
  1044. dbg("%s","URB KILLED !!!!!\n");
  1045. return;
  1046. }
  1047. if(port->read_urb->status != -EINPROGRESS) {
  1048. port->read_urb->dev = serial->dev;
  1049. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1050. if (status)
  1051. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1052. status);
  1053. }
  1054. return;
  1055. }
  1056. /*
  1057. * get_lsr_info - get line status register info
  1058. *
  1059. * Purpose: Let user call ioctl() to get info when the UART physically
  1060. * is emptied. On bus types like RS485, the transmitter must
  1061. * release the bus after transmitting. This must be done when
  1062. * the transmit shift register is empty, not be done when the
  1063. * transmit holding register is empty. This functionality
  1064. * allows an RS485 driver to be written in user space.
  1065. */
  1066. static int get_lsr_info(struct moschip_port *mos7720_port,
  1067. unsigned int __user *value)
  1068. {
  1069. int count;
  1070. unsigned int result = 0;
  1071. count = mos7720_chars_in_buffer(mos7720_port->port);
  1072. if (count == 0) {
  1073. dbg("%s -- Empty", __FUNCTION__);
  1074. result = TIOCSER_TEMT;
  1075. }
  1076. if (copy_to_user(value, &result, sizeof(int)))
  1077. return -EFAULT;
  1078. return 0;
  1079. }
  1080. /*
  1081. * get_number_bytes_avail - get number of bytes available
  1082. *
  1083. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1084. */
  1085. static int get_number_bytes_avail(struct moschip_port *mos7720_port,
  1086. unsigned int __user *value)
  1087. {
  1088. unsigned int result = 0;
  1089. struct tty_struct *tty = mos7720_port->port->tty;
  1090. if (!tty)
  1091. return -ENOIOCTLCMD;
  1092. result = tty->read_cnt;
  1093. dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
  1094. if (copy_to_user(value, &result, sizeof(int)))
  1095. return -EFAULT;
  1096. return -ENOIOCTLCMD;
  1097. }
  1098. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1099. unsigned int __user *value)
  1100. {
  1101. unsigned int mcr ;
  1102. unsigned int arg;
  1103. unsigned char data;
  1104. struct usb_serial_port *port;
  1105. if (mos7720_port == NULL)
  1106. return -1;
  1107. port = (struct usb_serial_port*)mos7720_port->port;
  1108. mcr = mos7720_port->shadowMCR;
  1109. if (copy_from_user(&arg, value, sizeof(int)))
  1110. return -EFAULT;
  1111. switch (cmd) {
  1112. case TIOCMBIS:
  1113. if (arg & TIOCM_RTS)
  1114. mcr |= UART_MCR_RTS;
  1115. if (arg & TIOCM_DTR)
  1116. mcr |= UART_MCR_RTS;
  1117. if (arg & TIOCM_LOOP)
  1118. mcr |= UART_MCR_LOOP;
  1119. break;
  1120. case TIOCMBIC:
  1121. if (arg & TIOCM_RTS)
  1122. mcr &= ~UART_MCR_RTS;
  1123. if (arg & TIOCM_DTR)
  1124. mcr &= ~UART_MCR_RTS;
  1125. if (arg & TIOCM_LOOP)
  1126. mcr &= ~UART_MCR_LOOP;
  1127. break;
  1128. case TIOCMSET:
  1129. /* turn off the RTS and DTR and LOOPBACK
  1130. * and then only turn on what was asked to */
  1131. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1132. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1133. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1134. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1135. break;
  1136. }
  1137. mos7720_port->shadowMCR = mcr;
  1138. data = mos7720_port->shadowMCR;
  1139. send_mos_cmd(port->serial, MOS_WRITE,
  1140. port->number - port->serial->minor, UART_MCR, &data);
  1141. return 0;
  1142. }
  1143. static int get_modem_info(struct moschip_port *mos7720_port,
  1144. unsigned int __user *value)
  1145. {
  1146. unsigned int result = 0;
  1147. unsigned int msr = mos7720_port->shadowMSR;
  1148. unsigned int mcr = mos7720_port->shadowMCR;
  1149. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1150. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1151. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1152. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1153. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1154. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1155. dbg("%s -- %x", __FUNCTION__, result);
  1156. if (copy_to_user(value, &result, sizeof(int)))
  1157. return -EFAULT;
  1158. return 0;
  1159. }
  1160. static int get_serial_info(struct moschip_port *mos7720_port,
  1161. struct serial_struct __user *retinfo)
  1162. {
  1163. struct serial_struct tmp;
  1164. if (!retinfo)
  1165. return -EFAULT;
  1166. memset(&tmp, 0, sizeof(tmp));
  1167. tmp.type = PORT_16550A;
  1168. tmp.line = mos7720_port->port->serial->minor;
  1169. tmp.port = mos7720_port->port->number;
  1170. tmp.irq = 0;
  1171. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1172. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1173. tmp.baud_base = 9600;
  1174. tmp.close_delay = 5*HZ;
  1175. tmp.closing_wait = 30*HZ;
  1176. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1177. return -EFAULT;
  1178. return 0;
  1179. }
  1180. static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
  1181. unsigned int cmd, unsigned long arg)
  1182. {
  1183. struct moschip_port *mos7720_port;
  1184. struct async_icount cnow;
  1185. struct async_icount cprev;
  1186. struct serial_icounter_struct icount;
  1187. mos7720_port = usb_get_serial_port_data(port);
  1188. if (mos7720_port == NULL)
  1189. return -ENODEV;
  1190. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  1191. switch (cmd) {
  1192. case TIOCINQ:
  1193. /* return number of bytes available */
  1194. dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
  1195. return get_number_bytes_avail(mos7720_port,
  1196. (unsigned int __user *)arg);
  1197. break;
  1198. case TIOCSERGETLSR:
  1199. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  1200. return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
  1201. return 0;
  1202. case TIOCMBIS:
  1203. case TIOCMBIC:
  1204. case TIOCMSET:
  1205. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
  1206. port->number);
  1207. return set_modem_info(mos7720_port, cmd,
  1208. (unsigned int __user *)arg);
  1209. case TIOCMGET:
  1210. dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
  1211. return get_modem_info(mos7720_port,
  1212. (unsigned int __user *)arg);
  1213. case TIOCGSERIAL:
  1214. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  1215. return get_serial_info(mos7720_port,
  1216. (struct serial_struct __user *)arg);
  1217. case TIOCSSERIAL:
  1218. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  1219. break;
  1220. case TIOCMIWAIT:
  1221. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  1222. cprev = mos7720_port->icount;
  1223. while (1) {
  1224. if (signal_pending(current))
  1225. return -ERESTARTSYS;
  1226. cnow = mos7720_port->icount;
  1227. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1228. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1229. return -EIO; /* no change => error */
  1230. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1231. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1232. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1233. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1234. return 0;
  1235. }
  1236. cprev = cnow;
  1237. }
  1238. /* NOTREACHED */
  1239. break;
  1240. case TIOCGICOUNT:
  1241. cnow = mos7720_port->icount;
  1242. icount.cts = cnow.cts;
  1243. icount.dsr = cnow.dsr;
  1244. icount.rng = cnow.rng;
  1245. icount.dcd = cnow.dcd;
  1246. icount.rx = cnow.rx;
  1247. icount.tx = cnow.tx;
  1248. icount.frame = cnow.frame;
  1249. icount.overrun = cnow.overrun;
  1250. icount.parity = cnow.parity;
  1251. icount.brk = cnow.brk;
  1252. icount.buf_overrun = cnow.buf_overrun;
  1253. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  1254. port->number, icount.rx, icount.tx );
  1255. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1256. return -EFAULT;
  1257. return 0;
  1258. }
  1259. return -ENOIOCTLCMD;
  1260. }
  1261. static int mos7720_startup(struct usb_serial *serial)
  1262. {
  1263. struct moschip_serial *mos7720_serial;
  1264. struct moschip_port *mos7720_port;
  1265. struct usb_device *dev;
  1266. int i;
  1267. char data;
  1268. dbg("%s: Entering ..........", __FUNCTION__);
  1269. if (!serial) {
  1270. dbg("Invalid Handler");
  1271. return -ENODEV;
  1272. }
  1273. dev = serial->dev;
  1274. /* create our private serial structure */
  1275. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1276. if (mos7720_serial == NULL) {
  1277. err("%s - Out of memory", __FUNCTION__);
  1278. return -ENOMEM;
  1279. }
  1280. usb_set_serial_data(serial, mos7720_serial);
  1281. /* we set up the pointers to the endpoints in the mos7720_open *
  1282. * function, as the structures aren't created yet. */
  1283. /* set up port private structures */
  1284. for (i = 0; i < serial->num_ports; ++i) {
  1285. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1286. if (mos7720_port == NULL) {
  1287. err("%s - Out of memory", __FUNCTION__);
  1288. usb_set_serial_data(serial, NULL);
  1289. kfree(mos7720_serial);
  1290. return -ENOMEM;
  1291. }
  1292. /* Initialize all port interrupt end point to port 0 int
  1293. * endpoint. Our device has only one interrupt endpoint
  1294. * comman to all ports */
  1295. serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
  1296. mos7720_port->port = serial->port[i];
  1297. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1298. dbg("port number is %d", serial->port[i]->number);
  1299. dbg("serial number is %d", serial->minor);
  1300. }
  1301. /* setting configuration feature to one */
  1302. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1303. (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
  1304. send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
  1305. dbg("LSR:%x",data);
  1306. send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
  1307. dbg("LSR:%x",data);
  1308. return 0;
  1309. }
  1310. static void mos7720_shutdown(struct usb_serial *serial)
  1311. {
  1312. int i;
  1313. /* free private structure allocated for serial port */
  1314. for (i=0; i < serial->num_ports; ++i) {
  1315. kfree(usb_get_serial_port_data(serial->port[i]));
  1316. usb_set_serial_port_data(serial->port[i], NULL);
  1317. }
  1318. /* free private structure allocated for serial device */
  1319. kfree(usb_get_serial_data(serial));
  1320. usb_set_serial_data(serial, NULL);
  1321. }
  1322. static struct usb_serial_driver moschip7720_2port_driver = {
  1323. .driver = {
  1324. .owner = THIS_MODULE,
  1325. .name = "moschip7720",
  1326. },
  1327. .description = "Moschip 2 port adapter",
  1328. .id_table = moschip_port_id_table,
  1329. .num_interrupt_in = 1,
  1330. .num_bulk_in = 2,
  1331. .num_bulk_out = 2,
  1332. .num_ports = 2,
  1333. .open = mos7720_open,
  1334. .close = mos7720_close,
  1335. .throttle = mos7720_throttle,
  1336. .unthrottle = mos7720_unthrottle,
  1337. .attach = mos7720_startup,
  1338. .shutdown = mos7720_shutdown,
  1339. .ioctl = mos7720_ioctl,
  1340. .set_termios = mos7720_set_termios,
  1341. .write = mos7720_write,
  1342. .write_room = mos7720_write_room,
  1343. .chars_in_buffer = mos7720_chars_in_buffer,
  1344. .break_ctl = mos7720_break,
  1345. .read_bulk_callback = mos7720_bulk_in_callback,
  1346. };
  1347. static struct usb_driver usb_driver = {
  1348. .name = "moschip7720",
  1349. .probe = usb_serial_probe,
  1350. .disconnect = usb_serial_disconnect,
  1351. .id_table = moschip_port_id_table,
  1352. };
  1353. static int __init moschip7720_init(void)
  1354. {
  1355. int retval;
  1356. dbg("%s: Entering ..........", __FUNCTION__);
  1357. /* Register with the usb serial */
  1358. retval = usb_serial_register(&moschip7720_2port_driver);
  1359. if (retval)
  1360. goto failed_port_device_register;
  1361. info(DRIVER_DESC " " DRIVER_VERSION);
  1362. /* Register with the usb */
  1363. retval = usb_register(&usb_driver);
  1364. if (retval)
  1365. goto failed_usb_register;
  1366. return 0;
  1367. failed_usb_register:
  1368. usb_serial_deregister(&moschip7720_2port_driver);
  1369. failed_port_device_register:
  1370. return retval;
  1371. }
  1372. static void __exit moschip7720_exit(void)
  1373. {
  1374. usb_deregister(&usb_driver);
  1375. usb_serial_deregister(&moschip7720_2port_driver);
  1376. }
  1377. module_init(moschip7720_init);
  1378. module_exit(moschip7720_exit);
  1379. /* Module information */
  1380. MODULE_AUTHOR( DRIVER_AUTHOR );
  1381. MODULE_DESCRIPTION( DRIVER_DESC );
  1382. MODULE_LICENSE("GPL");
  1383. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1384. MODULE_PARM_DESC(debug, "Debug enabled or not");