mos7720.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. /*
  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. __u8 shadowLCR; /* last LCR value received */
  62. __u8 shadowMCR; /* last MCR value received */
  63. __u8 shadowMSR; /* last MSR value received */
  64. char open;
  65. struct async_icount icount;
  66. struct usb_serial_port *port; /* loop back to the owner */
  67. struct urb *write_urb_pool[NUM_URBS];
  68. };
  69. /* This structure holds all of the individual serial device information */
  70. struct moschip_serial {
  71. int interrupt_started;
  72. };
  73. static int debug;
  74. #define USB_VENDOR_ID_MOSCHIP 0x9710
  75. #define MOSCHIP_DEVICE_ID_7720 0x7720
  76. #define MOSCHIP_DEVICE_ID_7715 0x7715
  77. static struct usb_device_id moschip_port_id_table [] = {
  78. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  79. { } /* terminating entry */
  80. };
  81. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  82. /*
  83. * mos7720_interrupt_callback
  84. * this is the callback function for when we have received data on the
  85. * interrupt endpoint.
  86. */
  87. static void mos7720_interrupt_callback(struct urb *urb)
  88. {
  89. int result;
  90. int length;
  91. int status = urb->status;
  92. __u8 *data;
  93. __u8 sp1;
  94. __u8 sp2;
  95. dbg("%s", " : Entering\n");
  96. switch (status) {
  97. case 0:
  98. /* success */
  99. break;
  100. case -ECONNRESET:
  101. case -ENOENT:
  102. case -ESHUTDOWN:
  103. /* this urb is terminated, clean up */
  104. dbg("%s - urb shutting down with status: %d", __func__,
  105. status);
  106. return;
  107. default:
  108. dbg("%s - nonzero urb status received: %d", __func__,
  109. status);
  110. goto exit;
  111. }
  112. length = urb->actual_length;
  113. data = urb->transfer_buffer;
  114. /* Moschip get 4 bytes
  115. * Byte 1 IIR Port 1 (port.number is 0)
  116. * Byte 2 IIR Port 2 (port.number is 1)
  117. * Byte 3 --------------
  118. * Byte 4 FIFO status for both */
  119. /* the above description is inverted
  120. * oneukum 2007-03-14 */
  121. if (unlikely(length != 4)) {
  122. dbg("Wrong data !!!");
  123. return;
  124. }
  125. sp1 = data[3];
  126. sp2 = data[2];
  127. if ((sp1 | sp2) & 0x01) {
  128. /* No Interrupt Pending in both the ports */
  129. dbg("No Interrupt !!!");
  130. } else {
  131. switch (sp1 & 0x0f) {
  132. case SERIAL_IIR_RLS:
  133. dbg("Serial Port 1: Receiver status error or address "
  134. "bit detected in 9-bit mode\n");
  135. break;
  136. case SERIAL_IIR_CTI:
  137. dbg("Serial Port 1: Receiver time out");
  138. break;
  139. case SERIAL_IIR_MS:
  140. dbg("Serial Port 1: Modem status change");
  141. break;
  142. }
  143. switch (sp2 & 0x0f) {
  144. case SERIAL_IIR_RLS:
  145. dbg("Serial Port 2: Receiver status error or address "
  146. "bit detected in 9-bit mode");
  147. break;
  148. case SERIAL_IIR_CTI:
  149. dbg("Serial Port 2: Receiver time out");
  150. break;
  151. case SERIAL_IIR_MS:
  152. dbg("Serial Port 2: Modem status change");
  153. break;
  154. }
  155. }
  156. exit:
  157. result = usb_submit_urb(urb, GFP_ATOMIC);
  158. if (result)
  159. dev_err(&urb->dev->dev,
  160. "%s - Error %d submitting control urb\n",
  161. __func__, result);
  162. return;
  163. }
  164. /*
  165. * mos7720_bulk_in_callback
  166. * this is the callback function for when we have received data on the
  167. * bulk in endpoint.
  168. */
  169. static void mos7720_bulk_in_callback(struct urb *urb)
  170. {
  171. int retval;
  172. unsigned char *data ;
  173. struct usb_serial_port *port;
  174. struct moschip_port *mos7720_port;
  175. struct tty_struct *tty;
  176. int status = urb->status;
  177. if (status) {
  178. dbg("nonzero read bulk status received: %d", status);
  179. return;
  180. }
  181. mos7720_port = urb->context;
  182. if (!mos7720_port) {
  183. dbg("%s", "NULL mos7720_port pointer \n");
  184. return ;
  185. }
  186. port = mos7720_port->port;
  187. dbg("Entering...%s", __func__);
  188. data = urb->transfer_buffer;
  189. tty = tty_port_tty_get(&port->port);
  190. if (tty && urb->actual_length) {
  191. tty_buffer_request_room(tty, urb->actual_length);
  192. tty_insert_flip_string(tty, data, urb->actual_length);
  193. tty_flip_buffer_push(tty);
  194. }
  195. tty_kref_put(tty);
  196. if (!port->read_urb) {
  197. dbg("URB KILLED !!!");
  198. return;
  199. }
  200. if (port->read_urb->status != -EINPROGRESS) {
  201. port->read_urb->dev = port->serial->dev;
  202. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  203. if (retval)
  204. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  205. retval);
  206. }
  207. }
  208. /*
  209. * mos7720_bulk_out_data_callback
  210. * this is the callback function for when we have finished sending serial
  211. * data on the bulk out endpoint.
  212. */
  213. static void mos7720_bulk_out_data_callback(struct urb *urb)
  214. {
  215. struct moschip_port *mos7720_port;
  216. struct tty_struct *tty;
  217. int status = urb->status;
  218. if (status) {
  219. dbg("nonzero write bulk status received:%d", status);
  220. return;
  221. }
  222. mos7720_port = urb->context;
  223. if (!mos7720_port) {
  224. dbg("NULL mos7720_port pointer");
  225. return ;
  226. }
  227. dbg("Entering .........");
  228. tty = tty_port_tty_get(&mos7720_port->port->port);
  229. if (tty && mos7720_port->open)
  230. tty_wakeup(tty);
  231. tty_kref_put(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. } else {
  251. value = 0x0000;
  252. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  253. (index != 0x08)) {
  254. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  255. /* index = 0x01 ; */
  256. }
  257. }
  258. if (request == MOS_WRITE) {
  259. request = (__u8)MOS_WRITE;
  260. requesttype = (__u8)0x40;
  261. value = value + (__u16)*((unsigned char *)data);
  262. data = NULL;
  263. pipe = usb_sndctrlpipe(serial->dev, 0);
  264. } else {
  265. request = (__u8)MOS_READ;
  266. requesttype = (__u8)0xC0;
  267. size = 0x01;
  268. pipe = usb_rcvctrlpipe(serial->dev, 0);
  269. }
  270. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  271. value, index, data, size, MOS_WDR_TIMEOUT);
  272. if (status < 0)
  273. dbg("Command Write failed Value %x index %x\n", value, index);
  274. return status;
  275. }
  276. static int mos7720_open(struct tty_struct *tty,
  277. 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. dev_err(&port->dev, "No more urbs???\n");
  305. continue;
  306. }
  307. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  308. GFP_KERNEL);
  309. if (!urb->transfer_buffer) {
  310. dev_err(&port->dev,
  311. "%s-out of memory for urb buffers.\n",
  312. __func__);
  313. usb_free_urb(mos7720_port->write_urb_pool[j]);
  314. mos7720_port->write_urb_pool[j] = NULL;
  315. continue;
  316. }
  317. allocated_urbs++;
  318. }
  319. if (!allocated_urbs)
  320. return -ENOMEM;
  321. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  322. *
  323. * Register Index
  324. * 1 : IER
  325. * 2 : FCR
  326. * 3 : LCR
  327. * 4 : MCR
  328. *
  329. * 0x08 : SP1/2 Control Reg
  330. */
  331. port_number = port->number - port->serial->minor;
  332. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  333. dbg("SS::%p LSR:%x\n", mos7720_port, data);
  334. dbg("Check:Sending Command ..........");
  335. data = 0x02;
  336. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  337. data = 0x02;
  338. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  339. data = 0x00;
  340. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  341. data = 0x00;
  342. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  343. data = 0xCF;
  344. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  345. data = 0x03;
  346. mos7720_port->shadowLCR = data;
  347. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  348. data = 0x0b;
  349. mos7720_port->shadowMCR = data;
  350. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  351. data = 0x0b;
  352. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  353. data = 0x00;
  354. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  355. data = 0x00;
  356. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  357. /* data = 0x00;
  358. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  359. data = 0x03;
  360. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  361. data = 0x00;
  362. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
  363. port_number + 1, &data);
  364. */
  365. data = 0x00;
  366. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  367. data = data | (port->number - port->serial->minor + 1);
  368. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  369. data = 0x83;
  370. mos7720_port->shadowLCR = data;
  371. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  372. data = 0x0c;
  373. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  374. data = 0x00;
  375. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  376. data = 0x03;
  377. mos7720_port->shadowLCR = data;
  378. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  379. data = 0x0c;
  380. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  381. data = 0x0c;
  382. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  383. /* force low_latency on so that our tty_push actually forces *
  384. * the data through,otherwise it is scheduled, and with *
  385. * high data rates (like with OHCI) data can get lost. */
  386. if (tty)
  387. tty->low_latency = 1;
  388. /* see if we've set up our endpoint info yet *
  389. * (can't set it up in mos7720_startup as the *
  390. * structures were not set up at that time.) */
  391. if (!mos7720_serial->interrupt_started) {
  392. dbg("Interrupt buffer NULL !!!");
  393. /* not set up yet, so do it now */
  394. mos7720_serial->interrupt_started = 1;
  395. dbg("To Submit URB !!!");
  396. /* set up our interrupt urb */
  397. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  398. usb_rcvintpipe(serial->dev,
  399. port->interrupt_in_endpointAddress),
  400. port0->interrupt_in_buffer,
  401. port0->interrupt_in_urb->transfer_buffer_length,
  402. mos7720_interrupt_callback, mos7720_port,
  403. port0->interrupt_in_urb->interval);
  404. /* start interrupt read for this mos7720 this interrupt *
  405. * will continue as long as the mos7720 is connected */
  406. dbg("Submit URB over !!!");
  407. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  408. if (response)
  409. dev_err(&port->dev,
  410. "%s - Error %d submitting control urb\n",
  411. __func__, response);
  412. }
  413. /* set up our bulk in urb */
  414. usb_fill_bulk_urb(port->read_urb, serial->dev,
  415. usb_rcvbulkpipe(serial->dev,
  416. port->bulk_in_endpointAddress),
  417. port->bulk_in_buffer,
  418. port->read_urb->transfer_buffer_length,
  419. mos7720_bulk_in_callback, mos7720_port);
  420. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  421. if (response)
  422. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  423. __func__, response);
  424. /* initialize our icount structure */
  425. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  426. /* initialize our port settings */
  427. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  428. /* send a open port command */
  429. mos7720_port->open = 1;
  430. return 0;
  431. }
  432. /*
  433. * mos7720_chars_in_buffer
  434. * this function is called by the tty driver when it wants to know how many
  435. * bytes of data we currently have outstanding in the port (data that has
  436. * been written, but hasn't made it out the port yet)
  437. * If successful, we return the number of bytes left to be written in the
  438. * system,
  439. * Otherwise we return a negative error number.
  440. */
  441. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  442. {
  443. struct usb_serial_port *port = tty->driver_data;
  444. int i;
  445. int chars = 0;
  446. struct moschip_port *mos7720_port;
  447. dbg("%s:entering ...........", __func__);
  448. mos7720_port = usb_get_serial_port_data(port);
  449. if (mos7720_port == NULL) {
  450. dbg("%s:leaving ...........", __func__);
  451. return -ENODEV;
  452. }
  453. for (i = 0; i < NUM_URBS; ++i) {
  454. if (mos7720_port->write_urb_pool[i] &&
  455. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  456. chars += URB_TRANSFER_BUFFER_SIZE;
  457. }
  458. dbg("%s - returns %d", __func__, chars);
  459. return chars;
  460. }
  461. static void mos7720_close(struct tty_struct *tty,
  462. 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, otherwise nop */
  484. dbg("Shutdown bulk write");
  485. usb_kill_urb(port->write_urb);
  486. dbg("Shutdown bulk read");
  487. usb_kill_urb(port->read_urb);
  488. mutex_lock(&serial->disc_mutex);
  489. /* these commands must not be issued if the device has
  490. * been disconnected */
  491. if (!serial->disconnected) {
  492. data = 0x00;
  493. send_mos_cmd(serial, MOS_WRITE,
  494. port->number - port->serial->minor, 0x04, &data);
  495. data = 0x00;
  496. send_mos_cmd(serial, MOS_WRITE,
  497. port->number - port->serial->minor, 0x01, &data);
  498. }
  499. mutex_unlock(&serial->disc_mutex);
  500. mos7720_port->open = 0;
  501. dbg("Leaving %s", __func__);
  502. }
  503. static void mos7720_break(struct tty_struct *tty, int break_state)
  504. {
  505. struct usb_serial_port *port = tty->driver_data;
  506. unsigned char data;
  507. struct usb_serial *serial;
  508. struct moschip_port *mos7720_port;
  509. dbg("Entering %s", __func__);
  510. serial = port->serial;
  511. mos7720_port = usb_get_serial_port_data(port);
  512. if (mos7720_port == NULL)
  513. return;
  514. if (break_state == -1)
  515. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  516. else
  517. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  518. mos7720_port->shadowLCR = data;
  519. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  520. 0x03, &data);
  521. return;
  522. }
  523. /*
  524. * mos7720_write_room
  525. * this function is called by the tty driver when it wants to know how many
  526. * bytes of data we can accept for a specific port.
  527. * If successful, we return the amount of room that we have for this port
  528. * Otherwise we return a negative error number.
  529. */
  530. static int mos7720_write_room(struct tty_struct *tty)
  531. {
  532. struct usb_serial_port *port = tty->driver_data;
  533. struct moschip_port *mos7720_port;
  534. int room = 0;
  535. int i;
  536. dbg("%s:entering ...........", __func__);
  537. mos7720_port = usb_get_serial_port_data(port);
  538. if (mos7720_port == NULL) {
  539. dbg("%s:leaving ...........", __func__);
  540. return -ENODEV;
  541. }
  542. /* FIXME: Locking */
  543. for (i = 0; i < NUM_URBS; ++i) {
  544. if (mos7720_port->write_urb_pool[i] &&
  545. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  546. room += URB_TRANSFER_BUFFER_SIZE;
  547. }
  548. dbg("%s - returns %d", __func__, room);
  549. return room;
  550. }
  551. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  552. const unsigned char *data, int count)
  553. {
  554. int status;
  555. int i;
  556. int bytes_sent = 0;
  557. int transfer_size;
  558. struct moschip_port *mos7720_port;
  559. struct usb_serial *serial;
  560. struct urb *urb;
  561. const unsigned char *current_position = data;
  562. dbg("%s:entering ...........", __func__);
  563. serial = port->serial;
  564. mos7720_port = usb_get_serial_port_data(port);
  565. if (mos7720_port == NULL) {
  566. dbg("mos7720_port is NULL");
  567. return -ENODEV;
  568. }
  569. /* try to find a free urb in the list */
  570. urb = NULL;
  571. for (i = 0; i < NUM_URBS; ++i) {
  572. if (mos7720_port->write_urb_pool[i] &&
  573. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  574. urb = mos7720_port->write_urb_pool[i];
  575. dbg("URB:%d", i);
  576. break;
  577. }
  578. }
  579. if (urb == NULL) {
  580. dbg("%s - no more free urbs", __func__);
  581. goto exit;
  582. }
  583. if (urb->transfer_buffer == NULL) {
  584. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  585. GFP_KERNEL);
  586. if (urb->transfer_buffer == NULL) {
  587. dev_err(&port->dev, "%s no more kernel memory...\n",
  588. __func__);
  589. goto exit;
  590. }
  591. }
  592. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  593. memcpy(urb->transfer_buffer, current_position, transfer_size);
  594. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  595. urb->transfer_buffer);
  596. /* fill urb with data and submit */
  597. usb_fill_bulk_urb(urb, serial->dev,
  598. usb_sndbulkpipe(serial->dev,
  599. port->bulk_out_endpointAddress),
  600. urb->transfer_buffer, transfer_size,
  601. mos7720_bulk_out_data_callback, mos7720_port);
  602. /* send it down the pipe */
  603. status = usb_submit_urb(urb, GFP_ATOMIC);
  604. if (status) {
  605. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  606. "with status = %d\n", __func__, status);
  607. bytes_sent = status;
  608. goto exit;
  609. }
  610. bytes_sent = transfer_size;
  611. exit:
  612. return bytes_sent;
  613. }
  614. static void mos7720_throttle(struct tty_struct *tty)
  615. {
  616. struct usb_serial_port *port = tty->driver_data;
  617. struct moschip_port *mos7720_port;
  618. int status;
  619. dbg("%s- port %d\n", __func__, port->number);
  620. mos7720_port = usb_get_serial_port_data(port);
  621. if (mos7720_port == NULL)
  622. return;
  623. if (!mos7720_port->open) {
  624. dbg("port not opened");
  625. return;
  626. }
  627. dbg("%s: Entering ..........", __func__);
  628. /* if we are implementing XON/XOFF, send the stop character */
  629. if (I_IXOFF(tty)) {
  630. unsigned char stop_char = STOP_CHAR(tty);
  631. status = mos7720_write(tty, port, &stop_char, 1);
  632. if (status <= 0)
  633. return;
  634. }
  635. /* if we are implementing RTS/CTS, toggle that line */
  636. if (tty->termios->c_cflag & CRTSCTS) {
  637. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  638. status = send_mos_cmd(port->serial, MOS_WRITE,
  639. port->number - port->serial->minor,
  640. UART_MCR, &mos7720_port->shadowMCR);
  641. if (status != 0)
  642. return;
  643. }
  644. }
  645. static void mos7720_unthrottle(struct tty_struct *tty)
  646. {
  647. struct usb_serial_port *port = tty->driver_data;
  648. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  649. int status;
  650. if (mos7720_port == NULL)
  651. return;
  652. if (!mos7720_port->open) {
  653. dbg("%s - port not opened", __func__);
  654. return;
  655. }
  656. dbg("%s: Entering ..........", __func__);
  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(tty, 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. __u32 baudrate;
  737. __u16 divisor;
  738. };
  739. /* Define table of divisors for moschip 7720 hardware *
  740. * These assume a 3.6864MHz crystal, the standard /16, and *
  741. * MCR.7 = 0. */
  742. static struct divisor_table_entry divisor_table[] = {
  743. { 50, 2304},
  744. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  745. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  746. { 150, 768},
  747. { 300, 384},
  748. { 600, 192},
  749. { 1200, 96},
  750. { 1800, 64},
  751. { 2400, 48},
  752. { 4800, 24},
  753. { 7200, 16},
  754. { 9600, 12},
  755. { 19200, 6},
  756. { 38400, 3},
  757. { 57600, 2},
  758. { 115200, 1},
  759. };
  760. /*****************************************************************************
  761. * calc_baud_rate_divisor
  762. * this function calculates the proper baud rate divisor for the specified
  763. * baud rate.
  764. *****************************************************************************/
  765. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  766. {
  767. int i;
  768. __u16 custom;
  769. __u16 round1;
  770. __u16 round;
  771. dbg("%s - %d", __func__, baudrate);
  772. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  773. if (divisor_table[i].baudrate == baudrate) {
  774. *divisor = divisor_table[i].divisor;
  775. return 0;
  776. }
  777. }
  778. /* After trying for all the standard baud rates *
  779. * Try calculating the divisor for this baud rate */
  780. if (baudrate > 75 && baudrate < 230400) {
  781. /* get the divisor */
  782. custom = (__u16)(230400L / baudrate);
  783. /* Check for round off */
  784. round1 = (__u16)(2304000L / baudrate);
  785. round = (__u16)(round1 - (custom * 10));
  786. if (round > 4)
  787. custom++;
  788. *divisor = custom;
  789. dbg("Baud %d = %d", baudrate, custom);
  790. return 0;
  791. }
  792. dbg("Baud calculation Failed...");
  793. return -EINVAL;
  794. }
  795. /*
  796. * send_cmd_write_baud_rate
  797. * this function sends the proper command to change the baud rate of the
  798. * specified port.
  799. */
  800. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  801. int baudrate)
  802. {
  803. struct usb_serial_port *port;
  804. struct usb_serial *serial;
  805. int divisor;
  806. int status;
  807. unsigned char data;
  808. unsigned char number;
  809. if (mos7720_port == NULL)
  810. return -1;
  811. port = mos7720_port->port;
  812. serial = port->serial;
  813. dbg("%s: Entering ..........", __func__);
  814. number = port->number - port->serial->minor;
  815. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  816. /* Calculate the Divisor */
  817. status = calc_baud_rate_divisor(baudrate, &divisor);
  818. if (status) {
  819. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  820. return status;
  821. }
  822. /* Enable access to divisor latch */
  823. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  824. mos7720_port->shadowLCR = data;
  825. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  826. /* Write the divisor */
  827. data = ((unsigned char)(divisor & 0xff));
  828. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  829. data = ((unsigned char)((divisor & 0xff00) >> 8));
  830. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  831. /* Disable access to divisor latch */
  832. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  833. mos7720_port->shadowLCR = data;
  834. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  835. return status;
  836. }
  837. /*
  838. * change_port_settings
  839. * This routine is called to set the UART on the device to match
  840. * the specified new settings.
  841. */
  842. static void change_port_settings(struct tty_struct *tty,
  843. struct moschip_port *mos7720_port,
  844. struct ktermios *old_termios)
  845. {
  846. struct usb_serial_port *port;
  847. struct usb_serial *serial;
  848. int baud;
  849. unsigned cflag;
  850. unsigned iflag;
  851. __u8 mask = 0xff;
  852. __u8 lData;
  853. __u8 lParity;
  854. __u8 lStop;
  855. int status;
  856. int port_number;
  857. char data;
  858. if (mos7720_port == NULL)
  859. return ;
  860. port = mos7720_port->port;
  861. serial = port->serial;
  862. port_number = port->number - port->serial->minor;
  863. dbg("%s - port %d", __func__, port->number);
  864. if (!mos7720_port->open) {
  865. dbg("%s - port not opened", __func__);
  866. return;
  867. }
  868. dbg("%s: Entering ..........", __func__);
  869. lData = UART_LCR_WLEN8;
  870. lStop = 0x00; /* 1 stop bit */
  871. lParity = 0x00; /* No parity */
  872. cflag = tty->termios->c_cflag;
  873. iflag = tty->termios->c_iflag;
  874. /* Change the number of bits */
  875. switch (cflag & CSIZE) {
  876. case CS5:
  877. lData = UART_LCR_WLEN5;
  878. mask = 0x1f;
  879. break;
  880. case CS6:
  881. lData = UART_LCR_WLEN6;
  882. mask = 0x3f;
  883. break;
  884. case CS7:
  885. lData = UART_LCR_WLEN7;
  886. mask = 0x7f;
  887. break;
  888. default:
  889. case CS8:
  890. lData = UART_LCR_WLEN8;
  891. break;
  892. }
  893. /* Change the Parity bit */
  894. if (cflag & PARENB) {
  895. if (cflag & PARODD) {
  896. lParity = UART_LCR_PARITY;
  897. dbg("%s - parity = odd", __func__);
  898. } else {
  899. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  900. dbg("%s - parity = even", __func__);
  901. }
  902. } else {
  903. dbg("%s - parity = none", __func__);
  904. }
  905. if (cflag & CMSPAR)
  906. lParity = lParity | 0x20;
  907. /* Change the Stop bit */
  908. if (cflag & CSTOPB) {
  909. lStop = UART_LCR_STOP;
  910. dbg("%s - stop bits = 2", __func__);
  911. } else {
  912. lStop = 0x00;
  913. dbg("%s - stop bits = 1", __func__);
  914. }
  915. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  916. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  917. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  918. /* Update the LCR with the correct value */
  919. mos7720_port->shadowLCR &=
  920. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  921. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  922. /* Disable Interrupts */
  923. data = 0x00;
  924. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  925. 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 tty_struct *tty,
  998. struct usb_serial_port *port, struct ktermios *old_termios)
  999. {
  1000. int status;
  1001. unsigned int cflag;
  1002. struct usb_serial *serial;
  1003. struct moschip_port *mos7720_port;
  1004. serial = port->serial;
  1005. mos7720_port = usb_get_serial_port_data(port);
  1006. if (mos7720_port == NULL)
  1007. return;
  1008. if (!mos7720_port->open) {
  1009. dbg("%s - port not opened", __func__);
  1010. return;
  1011. }
  1012. dbg("%s\n", "setting termios - ASPIRE");
  1013. cflag = tty->termios->c_cflag;
  1014. dbg("%s - cflag %08x iflag %08x", __func__,
  1015. tty->termios->c_cflag,
  1016. RELEVANT_IFLAG(tty->termios->c_iflag));
  1017. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1018. old_termios->c_cflag,
  1019. RELEVANT_IFLAG(old_termios->c_iflag));
  1020. dbg("%s - port %d", __func__, port->number);
  1021. /* change the port settings to the new ones specified */
  1022. change_port_settings(tty, mos7720_port, old_termios);
  1023. if (!port->read_urb) {
  1024. dbg("%s", "URB KILLED !!!!!\n");
  1025. return;
  1026. }
  1027. if (port->read_urb->status != -EINPROGRESS) {
  1028. port->read_urb->dev = serial->dev;
  1029. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1030. if (status)
  1031. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1032. status);
  1033. }
  1034. return;
  1035. }
  1036. /*
  1037. * get_lsr_info - get line status register info
  1038. *
  1039. * Purpose: Let user call ioctl() to get info when the UART physically
  1040. * is emptied. On bus types like RS485, the transmitter must
  1041. * release the bus after transmitting. This must be done when
  1042. * the transmit shift register is empty, not be done when the
  1043. * transmit holding register is empty. This functionality
  1044. * allows an RS485 driver to be written in user space.
  1045. */
  1046. static int get_lsr_info(struct tty_struct *tty,
  1047. struct moschip_port *mos7720_port, unsigned int __user *value)
  1048. {
  1049. int count;
  1050. unsigned int result = 0;
  1051. count = mos7720_chars_in_buffer(tty);
  1052. if (count == 0) {
  1053. dbg("%s -- Empty", __func__);
  1054. result = TIOCSER_TEMT;
  1055. }
  1056. if (copy_to_user(value, &result, sizeof(int)))
  1057. return -EFAULT;
  1058. return 0;
  1059. }
  1060. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1061. unsigned int __user *value)
  1062. {
  1063. unsigned int mcr ;
  1064. unsigned int arg;
  1065. unsigned char data;
  1066. struct usb_serial_port *port;
  1067. if (mos7720_port == NULL)
  1068. return -1;
  1069. port = (struct usb_serial_port *)mos7720_port->port;
  1070. mcr = mos7720_port->shadowMCR;
  1071. if (copy_from_user(&arg, value, sizeof(int)))
  1072. return -EFAULT;
  1073. switch (cmd) {
  1074. case TIOCMBIS:
  1075. if (arg & TIOCM_RTS)
  1076. mcr |= UART_MCR_RTS;
  1077. if (arg & TIOCM_DTR)
  1078. mcr |= UART_MCR_RTS;
  1079. if (arg & TIOCM_LOOP)
  1080. mcr |= UART_MCR_LOOP;
  1081. break;
  1082. case TIOCMBIC:
  1083. if (arg & TIOCM_RTS)
  1084. mcr &= ~UART_MCR_RTS;
  1085. if (arg & TIOCM_DTR)
  1086. mcr &= ~UART_MCR_RTS;
  1087. if (arg & TIOCM_LOOP)
  1088. mcr &= ~UART_MCR_LOOP;
  1089. break;
  1090. case TIOCMSET:
  1091. /* turn off the RTS and DTR and LOOPBACK
  1092. * and then only turn on what was asked to */
  1093. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1094. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1095. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1096. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1097. break;
  1098. }
  1099. mos7720_port->shadowMCR = mcr;
  1100. data = mos7720_port->shadowMCR;
  1101. send_mos_cmd(port->serial, MOS_WRITE,
  1102. port->number - port->serial->minor, UART_MCR, &data);
  1103. return 0;
  1104. }
  1105. static int get_modem_info(struct moschip_port *mos7720_port,
  1106. unsigned int __user *value)
  1107. {
  1108. unsigned int result = 0;
  1109. unsigned int msr = mos7720_port->shadowMSR;
  1110. unsigned int mcr = mos7720_port->shadowMCR;
  1111. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1112. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1113. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1114. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1115. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1116. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1117. dbg("%s -- %x", __func__, result);
  1118. if (copy_to_user(value, &result, sizeof(int)))
  1119. return -EFAULT;
  1120. return 0;
  1121. }
  1122. static int get_serial_info(struct moschip_port *mos7720_port,
  1123. struct serial_struct __user *retinfo)
  1124. {
  1125. struct serial_struct tmp;
  1126. if (!retinfo)
  1127. return -EFAULT;
  1128. memset(&tmp, 0, sizeof(tmp));
  1129. tmp.type = PORT_16550A;
  1130. tmp.line = mos7720_port->port->serial->minor;
  1131. tmp.port = mos7720_port->port->number;
  1132. tmp.irq = 0;
  1133. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1134. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1135. tmp.baud_base = 9600;
  1136. tmp.close_delay = 5*HZ;
  1137. tmp.closing_wait = 30*HZ;
  1138. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1139. return -EFAULT;
  1140. return 0;
  1141. }
  1142. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1143. unsigned int cmd, unsigned long arg)
  1144. {
  1145. struct usb_serial_port *port = tty->driver_data;
  1146. struct moschip_port *mos7720_port;
  1147. struct async_icount cnow;
  1148. struct async_icount cprev;
  1149. struct serial_icounter_struct icount;
  1150. mos7720_port = usb_get_serial_port_data(port);
  1151. if (mos7720_port == NULL)
  1152. return -ENODEV;
  1153. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1154. switch (cmd) {
  1155. case TIOCSERGETLSR:
  1156. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1157. return get_lsr_info(tty, mos7720_port,
  1158. (unsigned int __user *)arg);
  1159. return 0;
  1160. /* FIXME: These should be using the mode methods */
  1161. case TIOCMBIS:
  1162. case TIOCMBIC:
  1163. case TIOCMSET:
  1164. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1165. __func__, port->number);
  1166. return set_modem_info(mos7720_port, cmd,
  1167. (unsigned int __user *)arg);
  1168. case TIOCMGET:
  1169. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1170. return get_modem_info(mos7720_port,
  1171. (unsigned int __user *)arg);
  1172. case TIOCGSERIAL:
  1173. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1174. return get_serial_info(mos7720_port,
  1175. (struct serial_struct __user *)arg);
  1176. case TIOCMIWAIT:
  1177. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1178. cprev = mos7720_port->icount;
  1179. while (1) {
  1180. if (signal_pending(current))
  1181. return -ERESTARTSYS;
  1182. cnow = mos7720_port->icount;
  1183. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1184. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1185. return -EIO; /* no change => error */
  1186. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1187. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1188. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1189. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1190. return 0;
  1191. }
  1192. cprev = cnow;
  1193. }
  1194. /* NOTREACHED */
  1195. break;
  1196. case TIOCGICOUNT:
  1197. cnow = mos7720_port->icount;
  1198. icount.cts = cnow.cts;
  1199. icount.dsr = cnow.dsr;
  1200. icount.rng = cnow.rng;
  1201. icount.dcd = cnow.dcd;
  1202. icount.rx = cnow.rx;
  1203. icount.tx = cnow.tx;
  1204. icount.frame = cnow.frame;
  1205. icount.overrun = cnow.overrun;
  1206. icount.parity = cnow.parity;
  1207. icount.brk = cnow.brk;
  1208. icount.buf_overrun = cnow.buf_overrun;
  1209. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1210. port->number, icount.rx, icount.tx);
  1211. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1212. return -EFAULT;
  1213. return 0;
  1214. }
  1215. return -ENOIOCTLCMD;
  1216. }
  1217. static int mos7720_startup(struct usb_serial *serial)
  1218. {
  1219. struct moschip_serial *mos7720_serial;
  1220. struct moschip_port *mos7720_port;
  1221. struct usb_device *dev;
  1222. int i;
  1223. char data;
  1224. dbg("%s: Entering ..........", __func__);
  1225. if (!serial) {
  1226. dbg("Invalid Handler");
  1227. return -ENODEV;
  1228. }
  1229. dev = serial->dev;
  1230. /* create our private serial structure */
  1231. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1232. if (mos7720_serial == NULL) {
  1233. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1234. return -ENOMEM;
  1235. }
  1236. usb_set_serial_data(serial, mos7720_serial);
  1237. /* we set up the pointers to the endpoints in the mos7720_open *
  1238. * function, as the structures aren't created yet. */
  1239. /* set up port private structures */
  1240. for (i = 0; i < serial->num_ports; ++i) {
  1241. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1242. if (mos7720_port == NULL) {
  1243. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1244. usb_set_serial_data(serial, NULL);
  1245. kfree(mos7720_serial);
  1246. return -ENOMEM;
  1247. }
  1248. /* Initialize all port interrupt end point to port 0 int
  1249. * endpoint. Our device has only one interrupt endpoint
  1250. * comman to all ports */
  1251. serial->port[i]->interrupt_in_endpointAddress =
  1252. serial->port[0]->interrupt_in_endpointAddress;
  1253. mos7720_port->port = serial->port[i];
  1254. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1255. dbg("port number is %d", serial->port[i]->number);
  1256. dbg("serial number is %d", serial->minor);
  1257. }
  1258. /* setting configuration feature to one */
  1259. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1260. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1261. /* LSR For Port 1 */
  1262. send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
  1263. dbg("LSR:%x", data);
  1264. /* LSR For Port 2 */
  1265. send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
  1266. dbg("LSR:%x", data);
  1267. return 0;
  1268. }
  1269. static void mos7720_shutdown(struct usb_serial *serial)
  1270. {
  1271. int i;
  1272. /* free private structure allocated for serial port */
  1273. for (i = 0; i < serial->num_ports; ++i) {
  1274. kfree(usb_get_serial_port_data(serial->port[i]));
  1275. usb_set_serial_port_data(serial->port[i], NULL);
  1276. }
  1277. /* free private structure allocated for serial device */
  1278. kfree(usb_get_serial_data(serial));
  1279. usb_set_serial_data(serial, NULL);
  1280. }
  1281. static struct usb_driver usb_driver = {
  1282. .name = "moschip7720",
  1283. .probe = usb_serial_probe,
  1284. .disconnect = usb_serial_disconnect,
  1285. .id_table = moschip_port_id_table,
  1286. .no_dynamic_id = 1,
  1287. };
  1288. static struct usb_serial_driver moschip7720_2port_driver = {
  1289. .driver = {
  1290. .owner = THIS_MODULE,
  1291. .name = "moschip7720",
  1292. },
  1293. .description = "Moschip 2 port adapter",
  1294. .usb_driver = &usb_driver,
  1295. .id_table = moschip_port_id_table,
  1296. .num_ports = 2,
  1297. .open = mos7720_open,
  1298. .close = mos7720_close,
  1299. .throttle = mos7720_throttle,
  1300. .unthrottle = mos7720_unthrottle,
  1301. .attach = mos7720_startup,
  1302. .shutdown = mos7720_shutdown,
  1303. .ioctl = mos7720_ioctl,
  1304. .set_termios = mos7720_set_termios,
  1305. .write = mos7720_write,
  1306. .write_room = mos7720_write_room,
  1307. .chars_in_buffer = mos7720_chars_in_buffer,
  1308. .break_ctl = mos7720_break,
  1309. .read_bulk_callback = mos7720_bulk_in_callback,
  1310. .read_int_callback = mos7720_interrupt_callback,
  1311. };
  1312. static int __init moschip7720_init(void)
  1313. {
  1314. int retval;
  1315. dbg("%s: Entering ..........", __func__);
  1316. /* Register with the usb serial */
  1317. retval = usb_serial_register(&moschip7720_2port_driver);
  1318. if (retval)
  1319. goto failed_port_device_register;
  1320. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1321. DRIVER_DESC "\n");
  1322. /* Register with the usb */
  1323. retval = usb_register(&usb_driver);
  1324. if (retval)
  1325. goto failed_usb_register;
  1326. return 0;
  1327. failed_usb_register:
  1328. usb_serial_deregister(&moschip7720_2port_driver);
  1329. failed_port_device_register:
  1330. return retval;
  1331. }
  1332. static void __exit moschip7720_exit(void)
  1333. {
  1334. usb_deregister(&usb_driver);
  1335. usb_serial_deregister(&moschip7720_2port_driver);
  1336. }
  1337. module_init(moschip7720_init);
  1338. module_exit(moschip7720_exit);
  1339. /* Module information */
  1340. MODULE_AUTHOR(DRIVER_AUTHOR);
  1341. MODULE_DESCRIPTION(DRIVER_DESC);
  1342. MODULE_LICENSE("GPL");
  1343. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1344. MODULE_PARM_DESC(debug, "Debug enabled or not");