mos7720.c 41 KB

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