mos7720.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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 <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. int status = urb->status;
  94. __u8 *data;
  95. __u8 sp1;
  96. __u8 sp2;
  97. dbg("%s"," : Entering\n");
  98. switch (status) {
  99. case 0:
  100. /* success */
  101. break;
  102. case -ECONNRESET:
  103. case -ENOENT:
  104. case -ESHUTDOWN:
  105. /* this urb is terminated, clean up */
  106. dbg("%s - urb shutting down with status: %d", __func__,
  107. status);
  108. return;
  109. default:
  110. dbg("%s - nonzero urb status received: %d", __func__,
  111. status);
  112. goto exit;
  113. }
  114. length = urb->actual_length;
  115. data = urb->transfer_buffer;
  116. /* Moschip get 4 bytes
  117. * Byte 1 IIR Port 1 (port.number is 0)
  118. * Byte 2 IIR Port 2 (port.number is 1)
  119. * Byte 3 --------------
  120. * Byte 4 FIFO status for both */
  121. /* the above description is inverted
  122. * oneukum 2007-03-14 */
  123. if (unlikely(length != 4)) {
  124. dbg("Wrong data !!!");
  125. return;
  126. }
  127. sp1 = data[3];
  128. sp2 = data[2];
  129. if ((sp1 | sp2) & 0x01) {
  130. /* No Interrupt Pending in both the ports */
  131. dbg("No Interrupt !!!");
  132. } else {
  133. switch (sp1 & 0x0f) {
  134. case SERIAL_IIR_RLS:
  135. dbg("Serial Port 1: Receiver status error or address "
  136. "bit detected in 9-bit mode\n");
  137. break;
  138. case SERIAL_IIR_CTI:
  139. dbg("Serial Port 1: Receiver time out");
  140. break;
  141. case SERIAL_IIR_MS:
  142. dbg("Serial Port 1: Modem status change");
  143. break;
  144. }
  145. switch (sp2 & 0x0f) {
  146. case SERIAL_IIR_RLS:
  147. dbg("Serial Port 2: Receiver status error or address "
  148. "bit detected in 9-bit mode");
  149. break;
  150. case SERIAL_IIR_CTI:
  151. dbg("Serial Port 2: Receiver time out");
  152. break;
  153. case SERIAL_IIR_MS:
  154. dbg("Serial Port 2: Modem status change");
  155. break;
  156. }
  157. }
  158. exit:
  159. result = usb_submit_urb(urb, GFP_ATOMIC);
  160. if (result)
  161. dev_err(&urb->dev->dev,
  162. "%s - Error %d submitting control urb\n",
  163. __func__, result);
  164. return;
  165. }
  166. /*
  167. * mos7720_bulk_in_callback
  168. * this is the callback function for when we have received data on the
  169. * bulk in endpoint.
  170. */
  171. static void mos7720_bulk_in_callback(struct urb *urb)
  172. {
  173. int retval;
  174. unsigned char *data ;
  175. struct usb_serial_port *port;
  176. struct moschip_port *mos7720_port;
  177. struct tty_struct *tty;
  178. int status = urb->status;
  179. if (status) {
  180. dbg("nonzero read bulk status received: %d", status);
  181. return;
  182. }
  183. mos7720_port = urb->context;
  184. if (!mos7720_port) {
  185. dbg("%s","NULL mos7720_port pointer \n");
  186. return ;
  187. }
  188. port = mos7720_port->port;
  189. dbg("Entering...%s", __func__);
  190. data = urb->transfer_buffer;
  191. tty = port->tty;
  192. if (tty && urb->actual_length) {
  193. tty_buffer_request_room(tty, urb->actual_length);
  194. tty_insert_flip_string(tty, data, urb->actual_length);
  195. tty_flip_buffer_push(tty);
  196. }
  197. if (!port->read_urb) {
  198. dbg("URB KILLED !!!");
  199. return;
  200. }
  201. if (port->read_urb->status != -EINPROGRESS) {
  202. port->read_urb->dev = port->serial->dev;
  203. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  204. if (retval)
  205. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  206. retval);
  207. }
  208. }
  209. /*
  210. * mos7720_bulk_out_data_callback
  211. * this is the callback function for when we have finished sending serial
  212. * data on the bulk out endpoint.
  213. */
  214. static void mos7720_bulk_out_data_callback(struct urb *urb)
  215. {
  216. struct moschip_port *mos7720_port;
  217. struct tty_struct *tty;
  218. int status = urb->status;
  219. if (status) {
  220. dbg("nonzero write bulk status received:%d", status);
  221. return;
  222. }
  223. mos7720_port = urb->context;
  224. if (!mos7720_port) {
  225. dbg("NULL mos7720_port pointer");
  226. return ;
  227. }
  228. dbg("Entering .........");
  229. tty = mos7720_port->port->tty;
  230. if (tty && mos7720_port->open)
  231. tty_wakeup(tty);
  232. }
  233. /*
  234. * send_mos_cmd
  235. * this function will be used for sending command to device
  236. */
  237. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  238. __u16 index, void *data)
  239. {
  240. int status;
  241. unsigned int pipe;
  242. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  243. __u8 requesttype;
  244. __u16 size = 0x0000;
  245. if (value < MOS_MAX_PORT) {
  246. if (product == MOSCHIP_DEVICE_ID_7715) {
  247. value = value*0x100+0x100;
  248. } else {
  249. value = value*0x100+0x200;
  250. }
  251. } else {
  252. value = 0x0000;
  253. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  254. (index != 0x08)) {
  255. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  256. //index = 0x01 ;
  257. }
  258. }
  259. if (request == MOS_WRITE) {
  260. request = (__u8)MOS_WRITE;
  261. requesttype = (__u8)0x40;
  262. value = value + (__u16)*((unsigned char *)data);
  263. data = NULL;
  264. pipe = usb_sndctrlpipe(serial->dev, 0);
  265. } else {
  266. request = (__u8)MOS_READ;
  267. requesttype = (__u8)0xC0;
  268. size = 0x01;
  269. pipe = usb_rcvctrlpipe(serial->dev,0);
  270. }
  271. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  272. value, index, data, size, MOS_WDR_TIMEOUT);
  273. if (status < 0)
  274. dbg("Command Write failed Value %x index %x\n",value,index);
  275. return status;
  276. }
  277. static int mos7720_open(struct usb_serial_port *port, struct file * filp)
  278. {
  279. struct usb_serial *serial;
  280. struct usb_serial_port *port0;
  281. struct urb *urb;
  282. struct moschip_serial *mos7720_serial;
  283. struct moschip_port *mos7720_port;
  284. int response;
  285. int port_number;
  286. char data;
  287. int allocated_urbs = 0;
  288. int j;
  289. serial = port->serial;
  290. mos7720_port = usb_get_serial_port_data(port);
  291. if (mos7720_port == NULL)
  292. return -ENODEV;
  293. port0 = serial->port[0];
  294. mos7720_serial = usb_get_serial_data(serial);
  295. if (mos7720_serial == NULL || port0 == NULL)
  296. return -ENODEV;
  297. usb_clear_halt(serial->dev, port->write_urb->pipe);
  298. usb_clear_halt(serial->dev, port->read_urb->pipe);
  299. /* Initialising the write urb pool */
  300. for (j = 0; j < NUM_URBS; ++j) {
  301. urb = usb_alloc_urb(0,GFP_KERNEL);
  302. mos7720_port->write_urb_pool[j] = urb;
  303. if (urb == NULL) {
  304. err("No more urbs???");
  305. continue;
  306. }
  307. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  308. GFP_KERNEL);
  309. if (!urb->transfer_buffer) {
  310. err("%s-out of memory for urb buffers.", __func__);
  311. usb_free_urb(mos7720_port->write_urb_pool[j]);
  312. mos7720_port->write_urb_pool[j] = NULL;
  313. continue;
  314. }
  315. allocated_urbs++;
  316. }
  317. if (!allocated_urbs)
  318. return -ENOMEM;
  319. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  320. *
  321. * Register Index
  322. * 1 : IER
  323. * 2 : FCR
  324. * 3 : LCR
  325. * 4 : MCR
  326. *
  327. * 0x08 : SP1/2 Control Reg
  328. */
  329. port_number = port->number - port->serial->minor;
  330. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  331. dbg("SS::%p LSR:%x\n",mos7720_port, data);
  332. dbg("Check:Sending Command ..........");
  333. data = 0x02;
  334. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  335. data = 0x02;
  336. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  337. data = 0x00;
  338. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  339. data = 0x00;
  340. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  341. data = 0xCF;
  342. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  343. data = 0x03;
  344. mos7720_port->shadowLCR = data;
  345. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  346. data = 0x0b;
  347. mos7720_port->shadowMCR = data;
  348. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  349. data = 0x0b;
  350. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  351. data = 0x00;
  352. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  353. data = 0x00;
  354. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  355. /* data = 0x00;
  356. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  357. data = 0x03;
  358. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  359. data = 0x00;
  360. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  361. */
  362. data = 0x00;
  363. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  364. data = data | (port->number - port->serial->minor + 1);
  365. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  366. data = 0x83;
  367. mos7720_port->shadowLCR = data;
  368. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  369. data = 0x0c;
  370. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  371. data = 0x00;
  372. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  373. data = 0x03;
  374. mos7720_port->shadowLCR = data;
  375. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  376. data = 0x0c;
  377. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  378. data = 0x0c;
  379. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  380. //Matrix
  381. /* force low_latency on so that our tty_push actually forces *
  382. * the data through,otherwise it is scheduled, and with *
  383. * high data rates (like with OHCI) data can get lost. */
  384. if (port->tty)
  385. port->tty->low_latency = 1;
  386. /* see if we've set up our endpoint info yet *
  387. * (can't set it up in mos7720_startup as the *
  388. * structures were not set up at that time.) */
  389. if (!mos7720_serial->interrupt_started) {
  390. dbg("Interrupt buffer NULL !!!");
  391. /* not set up yet, so do it now */
  392. mos7720_serial->interrupt_started = 1;
  393. dbg("To Submit URB !!!");
  394. /* set up our interrupt urb */
  395. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  396. usb_rcvintpipe(serial->dev,
  397. port->interrupt_in_endpointAddress),
  398. port0->interrupt_in_buffer,
  399. port0->interrupt_in_urb->transfer_buffer_length,
  400. mos7720_interrupt_callback, mos7720_port,
  401. port0->interrupt_in_urb->interval);
  402. /* start interrupt read for this mos7720 this interrupt *
  403. * will continue as long as the mos7720 is connected */
  404. dbg("Submit URB over !!!");
  405. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  406. if (response)
  407. dev_err(&port->dev,
  408. "%s - Error %d submitting control urb\n",
  409. __func__, response);
  410. }
  411. /* set up our bulk in urb */
  412. usb_fill_bulk_urb(port->read_urb, serial->dev,
  413. usb_rcvbulkpipe(serial->dev,
  414. port->bulk_in_endpointAddress),
  415. port->bulk_in_buffer,
  416. port->read_urb->transfer_buffer_length,
  417. mos7720_bulk_in_callback, mos7720_port);
  418. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  419. if (response)
  420. dev_err(&port->dev,
  421. "%s - Error %d submitting read urb\n", __func__, response);
  422. /* initialize our icount structure */
  423. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  424. /* initialize our port settings */
  425. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  426. /* send a open port command */
  427. mos7720_port->open = 1;
  428. return 0;
  429. }
  430. /*
  431. * mos7720_chars_in_buffer
  432. * this function is called by the tty driver when it wants to know how many
  433. * bytes of data we currently have outstanding in the port (data that has
  434. * been written, but hasn't made it out the port yet)
  435. * If successful, we return the number of bytes left to be written in the
  436. * system,
  437. * Otherwise we return a negative error number.
  438. */
  439. static int mos7720_chars_in_buffer(struct usb_serial_port *port)
  440. {
  441. int i;
  442. int chars = 0;
  443. struct moschip_port *mos7720_port;
  444. dbg("%s:entering ...........", __func__);
  445. mos7720_port = usb_get_serial_port_data(port);
  446. if (mos7720_port == NULL) {
  447. dbg("%s:leaving ...........", __func__);
  448. return -ENODEV;
  449. }
  450. for (i = 0; i < NUM_URBS; ++i) {
  451. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  452. chars += URB_TRANSFER_BUFFER_SIZE;
  453. }
  454. dbg("%s - returns %d", __func__, chars);
  455. return chars;
  456. }
  457. static void mos7720_close(struct usb_serial_port *port, struct file *filp)
  458. {
  459. struct usb_serial *serial;
  460. struct moschip_port *mos7720_port;
  461. char data;
  462. int j;
  463. dbg("mos7720_close:entering...");
  464. serial = port->serial;
  465. mos7720_port = usb_get_serial_port_data(port);
  466. if (mos7720_port == NULL)
  467. return;
  468. for (j = 0; j < NUM_URBS; ++j)
  469. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  470. /* Freeing Write URBs */
  471. for (j = 0; j < NUM_URBS; ++j) {
  472. if (mos7720_port->write_urb_pool[j]) {
  473. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  474. usb_free_urb(mos7720_port->write_urb_pool[j]);
  475. }
  476. }
  477. /* While closing port, shutdown all bulk read, write *
  478. * and interrupt read if they exists, otherwise nop */
  479. dbg("Shutdown bulk write");
  480. usb_kill_urb(port->write_urb);
  481. dbg("Shutdown bulk read");
  482. usb_kill_urb(port->read_urb);
  483. mutex_lock(&serial->disc_mutex);
  484. /* these commands must not be issued if the device has
  485. * been disconnected */
  486. if (!serial->disconnected) {
  487. data = 0x00;
  488. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  489. 0x04, &data);
  490. data = 0x00;
  491. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  492. 0x01, &data);
  493. }
  494. mutex_unlock(&serial->disc_mutex);
  495. mos7720_port->open = 0;
  496. dbg("Leaving %s", __func__);
  497. }
  498. static void mos7720_break(struct usb_serial_port *port, int break_state)
  499. {
  500. unsigned char data;
  501. struct usb_serial *serial;
  502. struct moschip_port *mos7720_port;
  503. dbg("Entering %s", __func__);
  504. serial = port->serial;
  505. mos7720_port = usb_get_serial_port_data(port);
  506. if (mos7720_port == NULL)
  507. return;
  508. if (break_state == -1)
  509. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  510. else
  511. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  512. mos7720_port->shadowLCR = data;
  513. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  514. 0x03, &data);
  515. return;
  516. }
  517. /*
  518. * mos7720_write_room
  519. * this function is called by the tty driver when it wants to know how many
  520. * bytes of data we can accept for a specific port.
  521. * If successful, we return the amount of room that we have for this port
  522. * Otherwise we return a negative error number.
  523. */
  524. static int mos7720_write_room(struct usb_serial_port *port)
  525. {
  526. struct moschip_port *mos7720_port;
  527. int room = 0;
  528. int i;
  529. dbg("%s:entering ...........", __func__);
  530. mos7720_port = usb_get_serial_port_data(port);
  531. if (mos7720_port == NULL) {
  532. dbg("%s:leaving ...........", __func__);
  533. return -ENODEV;
  534. }
  535. /* FIXME: Locking */
  536. for (i = 0; i < NUM_URBS; ++i) {
  537. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  538. room += URB_TRANSFER_BUFFER_SIZE;
  539. }
  540. dbg("%s - returns %d", __func__, 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 ...........", __func__);
  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] && 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", __func__);
  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...", __func__);
  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, __func__, 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. __func__, 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", __func__, 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 ..........", __func__);
  618. tty = port->tty;
  619. if (!tty) {
  620. dbg("%s - no tty available", __func__);
  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", __func__);
  649. return;
  650. }
  651. dbg("%s: Entering ..........", __func__);
  652. tty = port->tty;
  653. if (!tty) {
  654. dbg("%s - no tty available", __func__);
  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", __func__, 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 ..........", __func__);
  815. number = port->number - port->serial->minor;
  816. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  817. /* Calculate the Divisor */
  818. status = calc_baud_rate_divisor(baudrate, &divisor);
  819. if (status) {
  820. err("%s - bad baud rate", __func__);
  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 ktermios *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", __func__, port->number);
  865. if (!mos7720_port->open) {
  866. dbg("%s - port not opened", __func__);
  867. return;
  868. }
  869. tty = mos7720_port->port->tty;
  870. dbg("%s: Entering ..........", __func__);
  871. lData = UART_LCR_WLEN8;
  872. lStop = 0x00; /* 1 stop bit */
  873. lParity = 0x00; /* No parity */
  874. cflag = tty->termios->c_cflag;
  875. iflag = tty->termios->c_iflag;
  876. /* Change the number of bits */
  877. switch (cflag & CSIZE) {
  878. case CS5:
  879. lData = UART_LCR_WLEN5;
  880. mask = 0x1f;
  881. break;
  882. case CS6:
  883. lData = UART_LCR_WLEN6;
  884. mask = 0x3f;
  885. break;
  886. case CS7:
  887. lData = UART_LCR_WLEN7;
  888. mask = 0x7f;
  889. break;
  890. default:
  891. case CS8:
  892. lData = UART_LCR_WLEN8;
  893. break;
  894. }
  895. /* Change the Parity bit */
  896. if (cflag & PARENB) {
  897. if (cflag & PARODD) {
  898. lParity = UART_LCR_PARITY;
  899. dbg("%s - parity = odd", __func__);
  900. } else {
  901. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  902. dbg("%s - parity = even", __func__);
  903. }
  904. } else {
  905. dbg("%s - parity = none", __func__);
  906. }
  907. if (cflag & CMSPAR)
  908. lParity = lParity | 0x20;
  909. /* Change the Stop bit */
  910. if (cflag & CSTOPB) {
  911. lStop = UART_LCR_STOP;
  912. dbg("%s - stop bits = 2", __func__);
  913. } else {
  914. lStop = 0x00;
  915. dbg("%s - stop bits = 1", __func__);
  916. }
  917. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  918. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  919. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  920. /* Update the LCR with the correct value */
  921. mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  922. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  923. /* Disable Interrupts */
  924. data = 0x00;
  925. send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
  926. data = 0x00;
  927. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  928. data = 0xcf;
  929. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  930. /* Send the updated LCR value to the mos7720 */
  931. data = mos7720_port->shadowLCR;
  932. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  933. data = 0x00b;
  934. mos7720_port->shadowMCR = data;
  935. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  936. data = 0x00b;
  937. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  938. /* set up the MCR register and send it to the mos7720 */
  939. mos7720_port->shadowMCR = UART_MCR_OUT2;
  940. if (cflag & CBAUD)
  941. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  942. if (cflag & CRTSCTS) {
  943. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  944. /* To set hardware flow control to the specified *
  945. * serial port, in SP1/2_CONTROL_REG */
  946. if (port->number) {
  947. data = 0x001;
  948. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  949. 0x08, &data);
  950. } else {
  951. data = 0x002;
  952. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  953. 0x08, &data);
  954. }
  955. } else {
  956. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  957. }
  958. data = mos7720_port->shadowMCR;
  959. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  960. /* Determine divisor based on baud rate */
  961. baud = tty_get_baud_rate(tty);
  962. if (!baud) {
  963. /* pick a default, any default... */
  964. dbg("Picked default baud...");
  965. baud = 9600;
  966. }
  967. if (baud >= 230400) {
  968. set_higher_rates(mos7720_port, baud);
  969. /* Enable Interrupts */
  970. data = 0x0c;
  971. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  972. return;
  973. }
  974. dbg("%s - baud rate = %d", __func__, baud);
  975. status = send_cmd_write_baud_rate(mos7720_port, baud);
  976. /* FIXME: needs to write actual resulting baud back not just
  977. blindly do so */
  978. if (cflag & CBAUD)
  979. tty_encode_baud_rate(tty, baud, 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 ktermios *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 (!mos7720_port->open) {
  1011. dbg("%s - port not opened", __func__);
  1012. return;
  1013. }
  1014. dbg("%s\n","setting termios - ASPIRE");
  1015. cflag = tty->termios->c_cflag;
  1016. dbg("%s - cflag %08x iflag %08x", __func__,
  1017. tty->termios->c_cflag,
  1018. RELEVANT_IFLAG(tty->termios->c_iflag));
  1019. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1020. old_termios->c_cflag,
  1021. RELEVANT_IFLAG(old_termios->c_iflag));
  1022. dbg("%s - port %d", __func__, port->number);
  1023. /* change the port settings to the new ones specified */
  1024. change_port_settings(mos7720_port, old_termios);
  1025. if(!port->read_urb) {
  1026. dbg("%s","URB KILLED !!!!!\n");
  1027. return;
  1028. }
  1029. if(port->read_urb->status != -EINPROGRESS) {
  1030. port->read_urb->dev = serial->dev;
  1031. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1032. if (status)
  1033. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1034. status);
  1035. }
  1036. return;
  1037. }
  1038. /*
  1039. * get_lsr_info - get line status register info
  1040. *
  1041. * Purpose: Let user call ioctl() to get info when the UART physically
  1042. * is emptied. On bus types like RS485, the transmitter must
  1043. * release the bus after transmitting. This must be done when
  1044. * the transmit shift register is empty, not be done when the
  1045. * transmit holding register is empty. This functionality
  1046. * allows an RS485 driver to be written in user space.
  1047. */
  1048. static int get_lsr_info(struct moschip_port *mos7720_port,
  1049. unsigned int __user *value)
  1050. {
  1051. int count;
  1052. unsigned int result = 0;
  1053. count = mos7720_chars_in_buffer(mos7720_port->port);
  1054. if (count == 0) {
  1055. dbg("%s -- Empty", __func__);
  1056. result = TIOCSER_TEMT;
  1057. }
  1058. if (copy_to_user(value, &result, sizeof(int)))
  1059. return -EFAULT;
  1060. return 0;
  1061. }
  1062. /*
  1063. * get_number_bytes_avail - get number of bytes available
  1064. *
  1065. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1066. */
  1067. static int get_number_bytes_avail(struct moschip_port *mos7720_port,
  1068. unsigned int __user *value)
  1069. {
  1070. unsigned int result = 0;
  1071. struct tty_struct *tty = mos7720_port->port->tty;
  1072. if (!tty)
  1073. return -ENOIOCTLCMD;
  1074. result = tty->read_cnt;
  1075. dbg("%s(%d) = %d", __func__, mos7720_port->port->number, result);
  1076. if (copy_to_user(value, &result, sizeof(int)))
  1077. return -EFAULT;
  1078. return -ENOIOCTLCMD;
  1079. }
  1080. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1081. unsigned int __user *value)
  1082. {
  1083. unsigned int mcr ;
  1084. unsigned int arg;
  1085. unsigned char data;
  1086. struct usb_serial_port *port;
  1087. if (mos7720_port == NULL)
  1088. return -1;
  1089. port = (struct usb_serial_port*)mos7720_port->port;
  1090. mcr = mos7720_port->shadowMCR;
  1091. if (copy_from_user(&arg, value, sizeof(int)))
  1092. return -EFAULT;
  1093. switch (cmd) {
  1094. case TIOCMBIS:
  1095. if (arg & TIOCM_RTS)
  1096. mcr |= UART_MCR_RTS;
  1097. if (arg & TIOCM_DTR)
  1098. mcr |= UART_MCR_RTS;
  1099. if (arg & TIOCM_LOOP)
  1100. mcr |= UART_MCR_LOOP;
  1101. break;
  1102. case TIOCMBIC:
  1103. if (arg & TIOCM_RTS)
  1104. mcr &= ~UART_MCR_RTS;
  1105. if (arg & TIOCM_DTR)
  1106. mcr &= ~UART_MCR_RTS;
  1107. if (arg & TIOCM_LOOP)
  1108. mcr &= ~UART_MCR_LOOP;
  1109. break;
  1110. case TIOCMSET:
  1111. /* turn off the RTS and DTR and LOOPBACK
  1112. * and then only turn on what was asked to */
  1113. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1114. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1115. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1116. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1117. break;
  1118. }
  1119. mos7720_port->shadowMCR = mcr;
  1120. data = mos7720_port->shadowMCR;
  1121. send_mos_cmd(port->serial, MOS_WRITE,
  1122. port->number - port->serial->minor, UART_MCR, &data);
  1123. return 0;
  1124. }
  1125. static int get_modem_info(struct moschip_port *mos7720_port,
  1126. unsigned int __user *value)
  1127. {
  1128. unsigned int result = 0;
  1129. unsigned int msr = mos7720_port->shadowMSR;
  1130. unsigned int mcr = mos7720_port->shadowMCR;
  1131. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1132. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1133. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1134. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1135. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1136. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1137. dbg("%s -- %x", __func__, result);
  1138. if (copy_to_user(value, &result, sizeof(int)))
  1139. return -EFAULT;
  1140. return 0;
  1141. }
  1142. static int get_serial_info(struct moschip_port *mos7720_port,
  1143. struct serial_struct __user *retinfo)
  1144. {
  1145. struct serial_struct tmp;
  1146. if (!retinfo)
  1147. return -EFAULT;
  1148. memset(&tmp, 0, sizeof(tmp));
  1149. tmp.type = PORT_16550A;
  1150. tmp.line = mos7720_port->port->serial->minor;
  1151. tmp.port = mos7720_port->port->number;
  1152. tmp.irq = 0;
  1153. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1154. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1155. tmp.baud_base = 9600;
  1156. tmp.close_delay = 5*HZ;
  1157. tmp.closing_wait = 30*HZ;
  1158. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1159. return -EFAULT;
  1160. return 0;
  1161. }
  1162. static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
  1163. unsigned int cmd, unsigned long arg)
  1164. {
  1165. struct moschip_port *mos7720_port;
  1166. struct async_icount cnow;
  1167. struct async_icount cprev;
  1168. struct serial_icounter_struct icount;
  1169. mos7720_port = usb_get_serial_port_data(port);
  1170. if (mos7720_port == NULL)
  1171. return -ENODEV;
  1172. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1173. switch (cmd) {
  1174. case TIOCINQ:
  1175. /* return number of bytes available */
  1176. dbg("%s (%d) TIOCINQ", __func__, port->number);
  1177. return get_number_bytes_avail(mos7720_port,
  1178. (unsigned int __user *)arg);
  1179. break;
  1180. case TIOCSERGETLSR:
  1181. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1182. return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
  1183. return 0;
  1184. case TIOCMBIS:
  1185. case TIOCMBIC:
  1186. case TIOCMSET:
  1187. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
  1188. port->number);
  1189. return set_modem_info(mos7720_port, cmd,
  1190. (unsigned int __user *)arg);
  1191. case TIOCMGET:
  1192. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1193. return get_modem_info(mos7720_port,
  1194. (unsigned int __user *)arg);
  1195. case TIOCGSERIAL:
  1196. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1197. return get_serial_info(mos7720_port,
  1198. (struct serial_struct __user *)arg);
  1199. case TIOCSSERIAL:
  1200. dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
  1201. break;
  1202. case TIOCMIWAIT:
  1203. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1204. cprev = mos7720_port->icount;
  1205. while (1) {
  1206. if (signal_pending(current))
  1207. return -ERESTARTSYS;
  1208. cnow = mos7720_port->icount;
  1209. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1210. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1211. return -EIO; /* no change => error */
  1212. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1213. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1214. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1215. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1216. return 0;
  1217. }
  1218. cprev = cnow;
  1219. }
  1220. /* NOTREACHED */
  1221. break;
  1222. case TIOCGICOUNT:
  1223. cnow = mos7720_port->icount;
  1224. icount.cts = cnow.cts;
  1225. icount.dsr = cnow.dsr;
  1226. icount.rng = cnow.rng;
  1227. icount.dcd = cnow.dcd;
  1228. icount.rx = cnow.rx;
  1229. icount.tx = cnow.tx;
  1230. icount.frame = cnow.frame;
  1231. icount.overrun = cnow.overrun;
  1232. icount.parity = cnow.parity;
  1233. icount.brk = cnow.brk;
  1234. icount.buf_overrun = cnow.buf_overrun;
  1235. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1236. port->number, icount.rx, icount.tx );
  1237. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1238. return -EFAULT;
  1239. return 0;
  1240. }
  1241. return -ENOIOCTLCMD;
  1242. }
  1243. static int mos7720_startup(struct usb_serial *serial)
  1244. {
  1245. struct moschip_serial *mos7720_serial;
  1246. struct moschip_port *mos7720_port;
  1247. struct usb_device *dev;
  1248. int i;
  1249. char data;
  1250. dbg("%s: Entering ..........", __func__);
  1251. if (!serial) {
  1252. dbg("Invalid Handler");
  1253. return -ENODEV;
  1254. }
  1255. dev = serial->dev;
  1256. /* create our private serial structure */
  1257. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1258. if (mos7720_serial == NULL) {
  1259. err("%s - Out of memory", __func__);
  1260. return -ENOMEM;
  1261. }
  1262. usb_set_serial_data(serial, mos7720_serial);
  1263. /* we set up the pointers to the endpoints in the mos7720_open *
  1264. * function, as the structures aren't created yet. */
  1265. /* set up port private structures */
  1266. for (i = 0; i < serial->num_ports; ++i) {
  1267. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1268. if (mos7720_port == NULL) {
  1269. err("%s - Out of memory", __func__);
  1270. usb_set_serial_data(serial, NULL);
  1271. kfree(mos7720_serial);
  1272. return -ENOMEM;
  1273. }
  1274. /* Initialize all port interrupt end point to port 0 int
  1275. * endpoint. Our device has only one interrupt endpoint
  1276. * comman to all ports */
  1277. serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
  1278. mos7720_port->port = serial->port[i];
  1279. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1280. dbg("port number is %d", serial->port[i]->number);
  1281. dbg("serial number is %d", serial->minor);
  1282. }
  1283. /* setting configuration feature to one */
  1284. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1285. (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
  1286. send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
  1287. dbg("LSR:%x",data);
  1288. send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
  1289. dbg("LSR:%x",data);
  1290. return 0;
  1291. }
  1292. static void mos7720_shutdown(struct usb_serial *serial)
  1293. {
  1294. int i;
  1295. /* free private structure allocated for serial port */
  1296. for (i=0; i < serial->num_ports; ++i) {
  1297. kfree(usb_get_serial_port_data(serial->port[i]));
  1298. usb_set_serial_port_data(serial->port[i], NULL);
  1299. }
  1300. /* free private structure allocated for serial device */
  1301. kfree(usb_get_serial_data(serial));
  1302. usb_set_serial_data(serial, NULL);
  1303. }
  1304. static struct usb_driver usb_driver = {
  1305. .name = "moschip7720",
  1306. .probe = usb_serial_probe,
  1307. .disconnect = usb_serial_disconnect,
  1308. .id_table = moschip_port_id_table,
  1309. .no_dynamic_id = 1,
  1310. };
  1311. static struct usb_serial_driver moschip7720_2port_driver = {
  1312. .driver = {
  1313. .owner = THIS_MODULE,
  1314. .name = "moschip7720",
  1315. },
  1316. .description = "Moschip 2 port adapter",
  1317. .usb_driver = &usb_driver,
  1318. .id_table = moschip_port_id_table,
  1319. .num_ports = 2,
  1320. .open = mos7720_open,
  1321. .close = mos7720_close,
  1322. .throttle = mos7720_throttle,
  1323. .unthrottle = mos7720_unthrottle,
  1324. .attach = mos7720_startup,
  1325. .shutdown = mos7720_shutdown,
  1326. .ioctl = mos7720_ioctl,
  1327. .set_termios = mos7720_set_termios,
  1328. .write = mos7720_write,
  1329. .write_room = mos7720_write_room,
  1330. .chars_in_buffer = mos7720_chars_in_buffer,
  1331. .break_ctl = mos7720_break,
  1332. .read_bulk_callback = mos7720_bulk_in_callback,
  1333. .read_int_callback = mos7720_interrupt_callback,
  1334. };
  1335. static int __init moschip7720_init(void)
  1336. {
  1337. int retval;
  1338. dbg("%s: Entering ..........", __func__);
  1339. /* Register with the usb serial */
  1340. retval = usb_serial_register(&moschip7720_2port_driver);
  1341. if (retval)
  1342. goto failed_port_device_register;
  1343. info(DRIVER_DESC " " DRIVER_VERSION);
  1344. /* Register with the usb */
  1345. retval = usb_register(&usb_driver);
  1346. if (retval)
  1347. goto failed_usb_register;
  1348. return 0;
  1349. failed_usb_register:
  1350. usb_serial_deregister(&moschip7720_2port_driver);
  1351. failed_port_device_register:
  1352. return retval;
  1353. }
  1354. static void __exit moschip7720_exit(void)
  1355. {
  1356. usb_deregister(&usb_driver);
  1357. usb_serial_deregister(&moschip7720_2port_driver);
  1358. }
  1359. module_init(moschip7720_init);
  1360. module_exit(moschip7720_exit);
  1361. /* Module information */
  1362. MODULE_AUTHOR( DRIVER_AUTHOR );
  1363. MODULE_DESCRIPTION( DRIVER_DESC );
  1364. MODULE_LICENSE("GPL");
  1365. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1366. MODULE_PARM_DESC(debug, "Debug enabled or not");