mos7720.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <asm/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port
  61. {
  62. __u8 shadowLCR; /* last LCR value received */
  63. __u8 shadowMCR; /* last MCR value received */
  64. __u8 shadowMSR; /* last MSR value received */
  65. char open;
  66. struct async_icount icount;
  67. struct usb_serial_port *port; /* loop back to the owner */
  68. struct urb *write_urb_pool[NUM_URBS];
  69. };
  70. /* This structure holds all of the individual serial device information */
  71. struct moschip_serial
  72. {
  73. int interrupt_started;
  74. };
  75. static int debug;
  76. #define USB_VENDOR_ID_MOSCHIP 0x9710
  77. #define MOSCHIP_DEVICE_ID_7720 0x7720
  78. #define MOSCHIP_DEVICE_ID_7715 0x7715
  79. static struct usb_device_id moschip_port_id_table [] = {
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. int status = urb->status;
  94. __u8 *data;
  95. __u8 sp1;
  96. __u8 sp2;
  97. dbg("%s"," : Entering\n");
  98. switch (status) {
  99. case 0:
  100. /* success */
  101. break;
  102. case -ECONNRESET:
  103. case -ENOENT:
  104. case -ESHUTDOWN:
  105. /* this urb is terminated, clean up */
  106. dbg("%s - urb shutting down with status: %d", __func__,
  107. status);
  108. return;
  109. default:
  110. dbg("%s - nonzero urb status received: %d", __func__,
  111. status);
  112. goto exit;
  113. }
  114. length = urb->actual_length;
  115. data = urb->transfer_buffer;
  116. /* Moschip get 4 bytes
  117. * Byte 1 IIR Port 1 (port.number is 0)
  118. * Byte 2 IIR Port 2 (port.number is 1)
  119. * Byte 3 --------------
  120. * Byte 4 FIFO status for both */
  121. /* the above description is inverted
  122. * oneukum 2007-03-14 */
  123. if (unlikely(length != 4)) {
  124. dbg("Wrong data !!!");
  125. return;
  126. }
  127. sp1 = data[3];
  128. sp2 = data[2];
  129. if ((sp1 | sp2) & 0x01) {
  130. /* No Interrupt Pending in both the ports */
  131. dbg("No Interrupt !!!");
  132. } else {
  133. switch (sp1 & 0x0f) {
  134. case SERIAL_IIR_RLS:
  135. dbg("Serial Port 1: Receiver status error or address "
  136. "bit detected in 9-bit mode\n");
  137. break;
  138. case SERIAL_IIR_CTI:
  139. dbg("Serial Port 1: Receiver time out");
  140. break;
  141. case SERIAL_IIR_MS:
  142. dbg("Serial Port 1: Modem status change");
  143. break;
  144. }
  145. switch (sp2 & 0x0f) {
  146. case SERIAL_IIR_RLS:
  147. dbg("Serial Port 2: Receiver status error or address "
  148. "bit detected in 9-bit mode");
  149. break;
  150. case SERIAL_IIR_CTI:
  151. dbg("Serial Port 2: Receiver time out");
  152. break;
  153. case SERIAL_IIR_MS:
  154. dbg("Serial Port 2: Modem status change");
  155. break;
  156. }
  157. }
  158. exit:
  159. result = usb_submit_urb(urb, GFP_ATOMIC);
  160. if (result)
  161. dev_err(&urb->dev->dev,
  162. "%s - Error %d submitting control urb\n",
  163. __func__, result);
  164. return;
  165. }
  166. /*
  167. * mos7720_bulk_in_callback
  168. * this is the callback function for when we have received data on the
  169. * bulk in endpoint.
  170. */
  171. static void mos7720_bulk_in_callback(struct urb *urb)
  172. {
  173. int retval;
  174. unsigned char *data ;
  175. struct usb_serial_port *port;
  176. struct moschip_port *mos7720_port;
  177. struct tty_struct *tty;
  178. int status = urb->status;
  179. if (status) {
  180. dbg("nonzero read bulk status received: %d", status);
  181. return;
  182. }
  183. mos7720_port = urb->context;
  184. if (!mos7720_port) {
  185. dbg("%s","NULL mos7720_port pointer \n");
  186. return ;
  187. }
  188. port = mos7720_port->port;
  189. dbg("Entering...%s", __func__);
  190. data = urb->transfer_buffer;
  191. tty = port->port.tty;
  192. if (tty && urb->actual_length) {
  193. tty_buffer_request_room(tty, urb->actual_length);
  194. tty_insert_flip_string(tty, data, urb->actual_length);
  195. tty_flip_buffer_push(tty);
  196. }
  197. if (!port->read_urb) {
  198. dbg("URB KILLED !!!");
  199. return;
  200. }
  201. if (port->read_urb->status != -EINPROGRESS) {
  202. port->read_urb->dev = port->serial->dev;
  203. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  204. if (retval)
  205. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  206. retval);
  207. }
  208. }
  209. /*
  210. * mos7720_bulk_out_data_callback
  211. * this is the callback function for when we have finished sending serial
  212. * data on the bulk out endpoint.
  213. */
  214. static void mos7720_bulk_out_data_callback(struct urb *urb)
  215. {
  216. struct moschip_port *mos7720_port;
  217. struct tty_struct *tty;
  218. int status = urb->status;
  219. if (status) {
  220. dbg("nonzero write bulk status received:%d", status);
  221. return;
  222. }
  223. mos7720_port = urb->context;
  224. if (!mos7720_port) {
  225. dbg("NULL mos7720_port pointer");
  226. return ;
  227. }
  228. dbg("Entering .........");
  229. tty = mos7720_port->port->port.tty;
  230. if (tty && mos7720_port->open)
  231. tty_wakeup(tty);
  232. }
  233. /*
  234. * send_mos_cmd
  235. * this function will be used for sending command to device
  236. */
  237. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  238. __u16 index, void *data)
  239. {
  240. int status;
  241. unsigned int pipe;
  242. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  243. __u8 requesttype;
  244. __u16 size = 0x0000;
  245. if (value < MOS_MAX_PORT) {
  246. if (product == MOSCHIP_DEVICE_ID_7715) {
  247. value = value*0x100+0x100;
  248. } else {
  249. value = value*0x100+0x200;
  250. }
  251. } else {
  252. value = 0x0000;
  253. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  254. (index != 0x08)) {
  255. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  256. //index = 0x01 ;
  257. }
  258. }
  259. if (request == MOS_WRITE) {
  260. request = (__u8)MOS_WRITE;
  261. requesttype = (__u8)0x40;
  262. value = value + (__u16)*((unsigned char *)data);
  263. data = NULL;
  264. pipe = usb_sndctrlpipe(serial->dev, 0);
  265. } else {
  266. request = (__u8)MOS_READ;
  267. requesttype = (__u8)0xC0;
  268. size = 0x01;
  269. pipe = usb_rcvctrlpipe(serial->dev,0);
  270. }
  271. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  272. value, index, data, size, MOS_WDR_TIMEOUT);
  273. if (status < 0)
  274. dbg("Command Write failed Value %x index %x\n",value,index);
  275. return status;
  276. }
  277. static int mos7720_open(struct tty_struct *tty,
  278. struct usb_serial_port *port, struct file * filp)
  279. {
  280. struct usb_serial *serial;
  281. struct usb_serial_port *port0;
  282. struct urb *urb;
  283. struct moschip_serial *mos7720_serial;
  284. struct moschip_port *mos7720_port;
  285. int response;
  286. int port_number;
  287. char data;
  288. int allocated_urbs = 0;
  289. int j;
  290. serial = port->serial;
  291. mos7720_port = usb_get_serial_port_data(port);
  292. if (mos7720_port == NULL)
  293. return -ENODEV;
  294. port0 = serial->port[0];
  295. mos7720_serial = usb_get_serial_data(serial);
  296. if (mos7720_serial == NULL || port0 == NULL)
  297. return -ENODEV;
  298. usb_clear_halt(serial->dev, port->write_urb->pipe);
  299. usb_clear_halt(serial->dev, port->read_urb->pipe);
  300. /* Initialising the write urb pool */
  301. for (j = 0; j < NUM_URBS; ++j) {
  302. urb = usb_alloc_urb(0,GFP_KERNEL);
  303. mos7720_port->write_urb_pool[j] = urb;
  304. if (urb == NULL) {
  305. err("No more urbs???");
  306. continue;
  307. }
  308. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  309. GFP_KERNEL);
  310. if (!urb->transfer_buffer) {
  311. err("%s-out of memory for urb buffers.", __func__);
  312. usb_free_urb(mos7720_port->write_urb_pool[j]);
  313. mos7720_port->write_urb_pool[j] = NULL;
  314. continue;
  315. }
  316. allocated_urbs++;
  317. }
  318. if (!allocated_urbs)
  319. return -ENOMEM;
  320. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  321. *
  322. * Register Index
  323. * 1 : IER
  324. * 2 : FCR
  325. * 3 : LCR
  326. * 4 : MCR
  327. *
  328. * 0x08 : SP1/2 Control Reg
  329. */
  330. port_number = port->number - port->serial->minor;
  331. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  332. dbg("SS::%p LSR:%x\n",mos7720_port, data);
  333. dbg("Check:Sending Command ..........");
  334. data = 0x02;
  335. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  336. data = 0x02;
  337. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  338. data = 0x00;
  339. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  340. data = 0x00;
  341. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  342. data = 0xCF;
  343. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  344. data = 0x03;
  345. mos7720_port->shadowLCR = data;
  346. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  347. data = 0x0b;
  348. mos7720_port->shadowMCR = data;
  349. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  350. data = 0x0b;
  351. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  352. data = 0x00;
  353. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  354. data = 0x00;
  355. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  356. /* data = 0x00;
  357. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  358. data = 0x03;
  359. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  360. data = 0x00;
  361. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  362. */
  363. data = 0x00;
  364. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  365. data = data | (port->number - port->serial->minor + 1);
  366. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  367. data = 0x83;
  368. mos7720_port->shadowLCR = data;
  369. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  370. data = 0x0c;
  371. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  372. data = 0x00;
  373. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  374. data = 0x03;
  375. mos7720_port->shadowLCR = data;
  376. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  377. data = 0x0c;
  378. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  379. data = 0x0c;
  380. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  381. /* force low_latency on so that our tty_push actually forces *
  382. * the data through,otherwise it is scheduled, and with *
  383. * high data rates (like with OHCI) data can get lost. */
  384. if (tty)
  385. tty->low_latency = 1;
  386. /* see if we've set up our endpoint info yet *
  387. * (can't set it up in mos7720_startup as the *
  388. * structures were not set up at that time.) */
  389. if (!mos7720_serial->interrupt_started) {
  390. dbg("Interrupt buffer NULL !!!");
  391. /* not set up yet, so do it now */
  392. mos7720_serial->interrupt_started = 1;
  393. dbg("To Submit URB !!!");
  394. /* set up our interrupt urb */
  395. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  396. usb_rcvintpipe(serial->dev,
  397. port->interrupt_in_endpointAddress),
  398. port0->interrupt_in_buffer,
  399. port0->interrupt_in_urb->transfer_buffer_length,
  400. mos7720_interrupt_callback, mos7720_port,
  401. port0->interrupt_in_urb->interval);
  402. /* start interrupt read for this mos7720 this interrupt *
  403. * will continue as long as the mos7720 is connected */
  404. dbg("Submit URB over !!!");
  405. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  406. if (response)
  407. dev_err(&port->dev,
  408. "%s - Error %d submitting control urb\n",
  409. __func__, response);
  410. }
  411. /* set up our bulk in urb */
  412. usb_fill_bulk_urb(port->read_urb, serial->dev,
  413. usb_rcvbulkpipe(serial->dev,
  414. port->bulk_in_endpointAddress),
  415. port->bulk_in_buffer,
  416. port->read_urb->transfer_buffer_length,
  417. mos7720_bulk_in_callback, mos7720_port);
  418. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  419. if (response)
  420. dev_err(&port->dev,
  421. "%s - Error %d submitting read urb\n", __func__, response);
  422. /* initialize our icount structure */
  423. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  424. /* initialize our port settings */
  425. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  426. /* send a open port command */
  427. mos7720_port->open = 1;
  428. return 0;
  429. }
  430. /*
  431. * mos7720_chars_in_buffer
  432. * this function is called by the tty driver when it wants to know how many
  433. * bytes of data we currently have outstanding in the port (data that has
  434. * been written, but hasn't made it out the port yet)
  435. * If successful, we return the number of bytes left to be written in the
  436. * system,
  437. * Otherwise we return a negative error number.
  438. */
  439. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  440. {
  441. struct usb_serial_port *port = tty->driver_data;
  442. int i;
  443. int chars = 0;
  444. struct moschip_port *mos7720_port;
  445. dbg("%s:entering ...........", __func__);
  446. mos7720_port = usb_get_serial_port_data(port);
  447. if (mos7720_port == NULL) {
  448. dbg("%s:leaving ...........", __func__);
  449. return -ENODEV;
  450. }
  451. for (i = 0; i < NUM_URBS; ++i) {
  452. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  453. chars += URB_TRANSFER_BUFFER_SIZE;
  454. }
  455. dbg("%s - returns %d", __func__, chars);
  456. return chars;
  457. }
  458. static void mos7720_close(struct tty_struct *tty,
  459. struct usb_serial_port *port, struct file *filp)
  460. {
  461. struct usb_serial *serial;
  462. struct moschip_port *mos7720_port;
  463. char data;
  464. int j;
  465. dbg("mos7720_close:entering...");
  466. serial = port->serial;
  467. mos7720_port = usb_get_serial_port_data(port);
  468. if (mos7720_port == NULL)
  469. return;
  470. for (j = 0; j < NUM_URBS; ++j)
  471. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  472. /* Freeing Write URBs */
  473. for (j = 0; j < NUM_URBS; ++j) {
  474. if (mos7720_port->write_urb_pool[j]) {
  475. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  476. usb_free_urb(mos7720_port->write_urb_pool[j]);
  477. }
  478. }
  479. /* While closing port, shutdown all bulk read, write *
  480. * and interrupt read if they exists, otherwise nop */
  481. dbg("Shutdown bulk write");
  482. usb_kill_urb(port->write_urb);
  483. dbg("Shutdown bulk read");
  484. usb_kill_urb(port->read_urb);
  485. mutex_lock(&serial->disc_mutex);
  486. /* these commands must not be issued if the device has
  487. * been disconnected */
  488. if (!serial->disconnected) {
  489. data = 0x00;
  490. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  491. 0x04, &data);
  492. data = 0x00;
  493. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  494. 0x01, &data);
  495. }
  496. mutex_unlock(&serial->disc_mutex);
  497. mos7720_port->open = 0;
  498. dbg("Leaving %s", __func__);
  499. }
  500. static void mos7720_break(struct tty_struct *tty, int break_state)
  501. {
  502. struct usb_serial_port *port = tty->driver_data;
  503. unsigned char data;
  504. struct usb_serial *serial;
  505. struct moschip_port *mos7720_port;
  506. dbg("Entering %s", __func__);
  507. serial = port->serial;
  508. mos7720_port = usb_get_serial_port_data(port);
  509. if (mos7720_port == NULL)
  510. return;
  511. if (break_state == -1)
  512. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  513. else
  514. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  515. mos7720_port->shadowLCR = data;
  516. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  517. 0x03, &data);
  518. return;
  519. }
  520. /*
  521. * mos7720_write_room
  522. * this function is called by the tty driver when it wants to know how many
  523. * bytes of data we can accept for a specific port.
  524. * If successful, we return the amount of room that we have for this port
  525. * Otherwise we return a negative error number.
  526. */
  527. static int mos7720_write_room(struct tty_struct *tty)
  528. {
  529. struct usb_serial_port *port = tty->driver_data;
  530. struct moschip_port *mos7720_port;
  531. int room = 0;
  532. int i;
  533. dbg("%s:entering ...........", __func__);
  534. mos7720_port = usb_get_serial_port_data(port);
  535. if (mos7720_port == NULL) {
  536. dbg("%s:leaving ...........", __func__);
  537. return -ENODEV;
  538. }
  539. /* FIXME: Locking */
  540. for (i = 0; i < NUM_URBS; ++i) {
  541. if (mos7720_port->write_urb_pool[i] && 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] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  569. urb = mos7720_port->write_urb_pool[i];
  570. dbg("URB:%d",i);
  571. break;
  572. }
  573. }
  574. if (urb == NULL) {
  575. dbg("%s - no more free urbs", __func__);
  576. goto exit;
  577. }
  578. if (urb->transfer_buffer == NULL) {
  579. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  580. GFP_KERNEL);
  581. if (urb->transfer_buffer == NULL) {
  582. err("%s no more kernel memory...", __func__);
  583. goto exit;
  584. }
  585. }
  586. transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
  587. memcpy(urb->transfer_buffer, current_position, transfer_size);
  588. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  589. urb->transfer_buffer);
  590. /* fill urb with data and submit */
  591. usb_fill_bulk_urb(urb, serial->dev,
  592. usb_sndbulkpipe(serial->dev,
  593. port->bulk_out_endpointAddress),
  594. urb->transfer_buffer, transfer_size,
  595. mos7720_bulk_out_data_callback, mos7720_port);
  596. /* send it down the pipe */
  597. status = usb_submit_urb(urb,GFP_ATOMIC);
  598. if (status) {
  599. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  600. __func__, status);
  601. bytes_sent = status;
  602. goto exit;
  603. }
  604. bytes_sent = transfer_size;
  605. exit:
  606. return bytes_sent;
  607. }
  608. static void mos7720_throttle(struct tty_struct *tty)
  609. {
  610. struct usb_serial_port *port = tty->driver_data;
  611. struct moschip_port *mos7720_port;
  612. int status;
  613. dbg("%s- port %d\n", __func__, port->number);
  614. mos7720_port = usb_get_serial_port_data(port);
  615. if (mos7720_port == NULL)
  616. return;
  617. if (!mos7720_port->open) {
  618. dbg("port not opened");
  619. return;
  620. }
  621. dbg("%s: Entering ..........", __func__);
  622. /* if we are implementing XON/XOFF, send the stop character */
  623. if (I_IXOFF(tty)) {
  624. unsigned char stop_char = STOP_CHAR(tty);
  625. status = mos7720_write(tty, port, &stop_char, 1);
  626. if (status <= 0)
  627. return;
  628. }
  629. /* if we are implementing RTS/CTS, toggle that line */
  630. if (tty->termios->c_cflag & CRTSCTS) {
  631. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  632. status = send_mos_cmd(port->serial, MOS_WRITE,
  633. port->number - port->serial->minor,
  634. UART_MCR, &mos7720_port->shadowMCR);
  635. if (status != 0)
  636. return;
  637. }
  638. }
  639. static void mos7720_unthrottle(struct tty_struct *tty)
  640. {
  641. struct usb_serial_port *port = tty->driver_data;
  642. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  643. int status;
  644. if (mos7720_port == NULL)
  645. return;
  646. if (!mos7720_port->open) {
  647. dbg("%s - port not opened", __func__);
  648. return;
  649. }
  650. dbg("%s: Entering ..........", __func__);
  651. /* if we are implementing XON/XOFF, send the start character */
  652. if (I_IXOFF(tty)) {
  653. unsigned char start_char = START_CHAR(tty);
  654. status = mos7720_write(tty, port, &start_char, 1);
  655. if (status <= 0)
  656. return;
  657. }
  658. /* if we are implementing RTS/CTS, toggle that line */
  659. if (tty->termios->c_cflag & CRTSCTS) {
  660. mos7720_port->shadowMCR |= UART_MCR_RTS;
  661. status = send_mos_cmd(port->serial, MOS_WRITE,
  662. port->number - port->serial->minor,
  663. UART_MCR, &mos7720_port->shadowMCR);
  664. if (status != 0)
  665. return;
  666. }
  667. }
  668. static int set_higher_rates(struct moschip_port *mos7720_port,
  669. unsigned int baud)
  670. {
  671. unsigned char data;
  672. struct usb_serial_port *port;
  673. struct usb_serial *serial;
  674. int port_number;
  675. if (mos7720_port == NULL)
  676. return -EINVAL;
  677. port = mos7720_port->port;
  678. serial = port->serial;
  679. /***********************************************
  680. * Init Sequence for higher rates
  681. ***********************************************/
  682. dbg("Sending Setting Commands ..........");
  683. port_number = port->number - port->serial->minor;
  684. data = 0x000;
  685. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  686. data = 0x000;
  687. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  688. data = 0x0CF;
  689. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  690. data = 0x00b;
  691. mos7720_port->shadowMCR = data;
  692. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  693. data = 0x00b;
  694. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  695. data = 0x000;
  696. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  697. data = 0x000;
  698. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  699. /***********************************************
  700. * Set for higher rates *
  701. ***********************************************/
  702. data = baud * 0x10;
  703. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
  704. data = 0x003;
  705. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  706. data = 0x003;
  707. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  708. data = 0x02b;
  709. mos7720_port->shadowMCR = data;
  710. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  711. data = 0x02b;
  712. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  713. /***********************************************
  714. * Set DLL/DLM
  715. ***********************************************/
  716. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  717. mos7720_port->shadowLCR = data;
  718. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  719. data = 0x001; /* DLL */
  720. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  721. data = 0x000; /* DLM */
  722. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  723. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  724. mos7720_port->shadowLCR = data;
  725. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  726. return 0;
  727. }
  728. /* baud rate information */
  729. struct divisor_table_entry
  730. {
  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 &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  915. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  916. /* Disable Interrupts */
  917. data = 0x00;
  918. send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
  919. data = 0x00;
  920. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  921. data = 0xcf;
  922. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  923. /* Send the updated LCR value to the mos7720 */
  924. data = mos7720_port->shadowLCR;
  925. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  926. data = 0x00b;
  927. mos7720_port->shadowMCR = data;
  928. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  929. data = 0x00b;
  930. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  931. /* set up the MCR register and send it to the mos7720 */
  932. mos7720_port->shadowMCR = UART_MCR_OUT2;
  933. if (cflag & CBAUD)
  934. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  935. if (cflag & CRTSCTS) {
  936. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  937. /* To set hardware flow control to the specified *
  938. * serial port, in SP1/2_CONTROL_REG */
  939. if (port->number) {
  940. data = 0x001;
  941. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  942. 0x08, &data);
  943. } else {
  944. data = 0x002;
  945. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  946. 0x08, &data);
  947. }
  948. } else {
  949. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  950. }
  951. data = mos7720_port->shadowMCR;
  952. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  953. /* Determine divisor based on baud rate */
  954. baud = tty_get_baud_rate(tty);
  955. if (!baud) {
  956. /* pick a default, any default... */
  957. dbg("Picked default baud...");
  958. baud = 9600;
  959. }
  960. if (baud >= 230400) {
  961. set_higher_rates(mos7720_port, baud);
  962. /* Enable Interrupts */
  963. data = 0x0c;
  964. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  965. return;
  966. }
  967. dbg("%s - baud rate = %d", __func__, baud);
  968. status = send_cmd_write_baud_rate(mos7720_port, baud);
  969. /* FIXME: needs to write actual resulting baud back not just
  970. blindly do so */
  971. if (cflag & CBAUD)
  972. tty_encode_baud_rate(tty, baud, baud);
  973. /* Enable Interrupts */
  974. data = 0x0c;
  975. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  976. if (port->read_urb->status != -EINPROGRESS) {
  977. port->read_urb->dev = serial->dev;
  978. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  979. if (status)
  980. dbg("usb_submit_urb(read bulk) failed, status = %d",
  981. status);
  982. }
  983. return;
  984. }
  985. /*
  986. * mos7720_set_termios
  987. * this function is called by the tty driver when it wants to change the
  988. * termios structure.
  989. */
  990. static void mos7720_set_termios(struct tty_struct *tty,
  991. struct usb_serial_port *port, struct ktermios *old_termios)
  992. {
  993. int status;
  994. unsigned int cflag;
  995. struct usb_serial *serial;
  996. struct moschip_port *mos7720_port;
  997. serial = port->serial;
  998. mos7720_port = usb_get_serial_port_data(port);
  999. if (mos7720_port == NULL)
  1000. return;
  1001. if (!mos7720_port->open) {
  1002. dbg("%s - port not opened", __func__);
  1003. return;
  1004. }
  1005. dbg("%s\n","setting termios - ASPIRE");
  1006. cflag = tty->termios->c_cflag;
  1007. dbg("%s - cflag %08x iflag %08x", __func__,
  1008. tty->termios->c_cflag,
  1009. RELEVANT_IFLAG(tty->termios->c_iflag));
  1010. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1011. old_termios->c_cflag,
  1012. RELEVANT_IFLAG(old_termios->c_iflag));
  1013. dbg("%s - port %d", __func__, port->number);
  1014. /* change the port settings to the new ones specified */
  1015. change_port_settings(tty, mos7720_port, old_termios);
  1016. if(!port->read_urb) {
  1017. dbg("%s","URB KILLED !!!!!\n");
  1018. return;
  1019. }
  1020. if(port->read_urb->status != -EINPROGRESS) {
  1021. port->read_urb->dev = serial->dev;
  1022. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1023. if (status)
  1024. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1025. status);
  1026. }
  1027. return;
  1028. }
  1029. /*
  1030. * get_lsr_info - get line status register info
  1031. *
  1032. * Purpose: Let user call ioctl() to get info when the UART physically
  1033. * is emptied. On bus types like RS485, the transmitter must
  1034. * release the bus after transmitting. This must be done when
  1035. * the transmit shift register is empty, not be done when the
  1036. * transmit holding register is empty. This functionality
  1037. * allows an RS485 driver to be written in user space.
  1038. */
  1039. static int get_lsr_info(struct tty_struct *tty, struct moschip_port *mos7720_port,
  1040. unsigned int __user *value)
  1041. {
  1042. int count;
  1043. unsigned int result = 0;
  1044. count = mos7720_chars_in_buffer(tty);
  1045. if (count == 0) {
  1046. dbg("%s -- Empty", __func__);
  1047. result = TIOCSER_TEMT;
  1048. }
  1049. if (copy_to_user(value, &result, sizeof(int)))
  1050. return -EFAULT;
  1051. return 0;
  1052. }
  1053. /*
  1054. * get_number_bytes_avail - get number of bytes available
  1055. *
  1056. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1057. */
  1058. static int get_number_bytes_avail(struct moschip_port *mos7720_port,
  1059. unsigned int __user *value)
  1060. {
  1061. unsigned int result = 0;
  1062. struct tty_struct *tty = mos7720_port->port->port.tty;
  1063. if (!tty)
  1064. return -ENOIOCTLCMD;
  1065. result = tty->read_cnt;
  1066. dbg("%s(%d) = %d", __func__, mos7720_port->port->number, result);
  1067. if (copy_to_user(value, &result, sizeof(int)))
  1068. return -EFAULT;
  1069. return -ENOIOCTLCMD;
  1070. }
  1071. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1072. unsigned int __user *value)
  1073. {
  1074. unsigned int mcr ;
  1075. unsigned int arg;
  1076. unsigned char data;
  1077. struct usb_serial_port *port;
  1078. if (mos7720_port == NULL)
  1079. return -1;
  1080. port = (struct usb_serial_port*)mos7720_port->port;
  1081. mcr = mos7720_port->shadowMCR;
  1082. if (copy_from_user(&arg, value, sizeof(int)))
  1083. return -EFAULT;
  1084. switch (cmd) {
  1085. case TIOCMBIS:
  1086. if (arg & TIOCM_RTS)
  1087. mcr |= UART_MCR_RTS;
  1088. if (arg & TIOCM_DTR)
  1089. mcr |= UART_MCR_RTS;
  1090. if (arg & TIOCM_LOOP)
  1091. mcr |= UART_MCR_LOOP;
  1092. break;
  1093. case TIOCMBIC:
  1094. if (arg & TIOCM_RTS)
  1095. mcr &= ~UART_MCR_RTS;
  1096. if (arg & TIOCM_DTR)
  1097. mcr &= ~UART_MCR_RTS;
  1098. if (arg & TIOCM_LOOP)
  1099. mcr &= ~UART_MCR_LOOP;
  1100. break;
  1101. case TIOCMSET:
  1102. /* turn off the RTS and DTR and LOOPBACK
  1103. * and then only turn on what was asked to */
  1104. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1105. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1106. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1107. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1108. break;
  1109. }
  1110. mos7720_port->shadowMCR = mcr;
  1111. data = mos7720_port->shadowMCR;
  1112. send_mos_cmd(port->serial, MOS_WRITE,
  1113. port->number - port->serial->minor, UART_MCR, &data);
  1114. return 0;
  1115. }
  1116. static int get_modem_info(struct moschip_port *mos7720_port,
  1117. unsigned int __user *value)
  1118. {
  1119. unsigned int result = 0;
  1120. unsigned int msr = mos7720_port->shadowMSR;
  1121. unsigned int mcr = mos7720_port->shadowMCR;
  1122. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1123. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1124. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1125. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1126. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1127. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1128. dbg("%s -- %x", __func__, result);
  1129. if (copy_to_user(value, &result, sizeof(int)))
  1130. return -EFAULT;
  1131. return 0;
  1132. }
  1133. static int get_serial_info(struct moschip_port *mos7720_port,
  1134. struct serial_struct __user *retinfo)
  1135. {
  1136. struct serial_struct tmp;
  1137. if (!retinfo)
  1138. return -EFAULT;
  1139. memset(&tmp, 0, sizeof(tmp));
  1140. tmp.type = PORT_16550A;
  1141. tmp.line = mos7720_port->port->serial->minor;
  1142. tmp.port = mos7720_port->port->number;
  1143. tmp.irq = 0;
  1144. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1145. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1146. tmp.baud_base = 9600;
  1147. tmp.close_delay = 5*HZ;
  1148. tmp.closing_wait = 30*HZ;
  1149. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1150. return -EFAULT;
  1151. return 0;
  1152. }
  1153. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1154. unsigned int cmd, unsigned long arg)
  1155. {
  1156. struct usb_serial_port *port = tty->driver_data;
  1157. struct moschip_port *mos7720_port;
  1158. struct async_icount cnow;
  1159. struct async_icount cprev;
  1160. struct serial_icounter_struct icount;
  1161. mos7720_port = usb_get_serial_port_data(port);
  1162. if (mos7720_port == NULL)
  1163. return -ENODEV;
  1164. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1165. switch (cmd) {
  1166. case TIOCINQ:
  1167. /* return number of bytes available */
  1168. dbg("%s (%d) TIOCINQ", __func__, port->number);
  1169. return get_number_bytes_avail(mos7720_port,
  1170. (unsigned int __user *)arg);
  1171. break;
  1172. case TIOCSERGETLSR:
  1173. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1174. return get_lsr_info(tty, mos7720_port, (unsigned int __user *)arg);
  1175. return 0;
  1176. /* FIXME: These should be using the mode methods */
  1177. case TIOCMBIS:
  1178. case TIOCMBIC:
  1179. case TIOCMSET:
  1180. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
  1181. port->number);
  1182. return set_modem_info(mos7720_port, cmd,
  1183. (unsigned int __user *)arg);
  1184. case TIOCMGET:
  1185. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1186. return get_modem_info(mos7720_port,
  1187. (unsigned int __user *)arg);
  1188. case TIOCGSERIAL:
  1189. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1190. return get_serial_info(mos7720_port,
  1191. (struct serial_struct __user *)arg);
  1192. case TIOCMIWAIT:
  1193. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1194. cprev = mos7720_port->icount;
  1195. while (1) {
  1196. if (signal_pending(current))
  1197. return -ERESTARTSYS;
  1198. cnow = mos7720_port->icount;
  1199. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1200. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1201. return -EIO; /* no change => error */
  1202. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1203. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1204. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1205. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1206. return 0;
  1207. }
  1208. cprev = cnow;
  1209. }
  1210. /* NOTREACHED */
  1211. break;
  1212. case TIOCGICOUNT:
  1213. cnow = mos7720_port->icount;
  1214. icount.cts = cnow.cts;
  1215. icount.dsr = cnow.dsr;
  1216. icount.rng = cnow.rng;
  1217. icount.dcd = cnow.dcd;
  1218. icount.rx = cnow.rx;
  1219. icount.tx = cnow.tx;
  1220. icount.frame = cnow.frame;
  1221. icount.overrun = cnow.overrun;
  1222. icount.parity = cnow.parity;
  1223. icount.brk = cnow.brk;
  1224. icount.buf_overrun = cnow.buf_overrun;
  1225. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1226. port->number, icount.rx, icount.tx );
  1227. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1228. return -EFAULT;
  1229. return 0;
  1230. }
  1231. return -ENOIOCTLCMD;
  1232. }
  1233. static int mos7720_startup(struct usb_serial *serial)
  1234. {
  1235. struct moschip_serial *mos7720_serial;
  1236. struct moschip_port *mos7720_port;
  1237. struct usb_device *dev;
  1238. int i;
  1239. char data;
  1240. dbg("%s: Entering ..........", __func__);
  1241. if (!serial) {
  1242. dbg("Invalid Handler");
  1243. return -ENODEV;
  1244. }
  1245. dev = serial->dev;
  1246. /* create our private serial structure */
  1247. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1248. if (mos7720_serial == NULL) {
  1249. err("%s - Out of memory", __func__);
  1250. return -ENOMEM;
  1251. }
  1252. usb_set_serial_data(serial, mos7720_serial);
  1253. /* we set up the pointers to the endpoints in the mos7720_open *
  1254. * function, as the structures aren't created yet. */
  1255. /* set up port private structures */
  1256. for (i = 0; i < serial->num_ports; ++i) {
  1257. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1258. if (mos7720_port == NULL) {
  1259. err("%s - Out of memory", __func__);
  1260. usb_set_serial_data(serial, NULL);
  1261. kfree(mos7720_serial);
  1262. return -ENOMEM;
  1263. }
  1264. /* Initialize all port interrupt end point to port 0 int
  1265. * endpoint. Our device has only one interrupt endpoint
  1266. * comman to all ports */
  1267. serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
  1268. mos7720_port->port = serial->port[i];
  1269. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1270. dbg("port number is %d", serial->port[i]->number);
  1271. dbg("serial number is %d", serial->minor);
  1272. }
  1273. /* setting configuration feature to one */
  1274. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1275. (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
  1276. send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
  1277. dbg("LSR:%x",data);
  1278. send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
  1279. dbg("LSR:%x",data);
  1280. return 0;
  1281. }
  1282. static void mos7720_shutdown(struct usb_serial *serial)
  1283. {
  1284. int i;
  1285. /* free private structure allocated for serial port */
  1286. for (i=0; i < serial->num_ports; ++i) {
  1287. kfree(usb_get_serial_port_data(serial->port[i]));
  1288. usb_set_serial_port_data(serial->port[i], NULL);
  1289. }
  1290. /* free private structure allocated for serial device */
  1291. kfree(usb_get_serial_data(serial));
  1292. usb_set_serial_data(serial, NULL);
  1293. }
  1294. static struct usb_driver usb_driver = {
  1295. .name = "moschip7720",
  1296. .probe = usb_serial_probe,
  1297. .disconnect = usb_serial_disconnect,
  1298. .id_table = moschip_port_id_table,
  1299. .no_dynamic_id = 1,
  1300. };
  1301. static struct usb_serial_driver moschip7720_2port_driver = {
  1302. .driver = {
  1303. .owner = THIS_MODULE,
  1304. .name = "moschip7720",
  1305. },
  1306. .description = "Moschip 2 port adapter",
  1307. .usb_driver = &usb_driver,
  1308. .id_table = moschip_port_id_table,
  1309. .num_ports = 2,
  1310. .open = mos7720_open,
  1311. .close = mos7720_close,
  1312. .throttle = mos7720_throttle,
  1313. .unthrottle = mos7720_unthrottle,
  1314. .attach = mos7720_startup,
  1315. .shutdown = mos7720_shutdown,
  1316. .ioctl = mos7720_ioctl,
  1317. .set_termios = mos7720_set_termios,
  1318. .write = mos7720_write,
  1319. .write_room = mos7720_write_room,
  1320. .chars_in_buffer = mos7720_chars_in_buffer,
  1321. .break_ctl = mos7720_break,
  1322. .read_bulk_callback = mos7720_bulk_in_callback,
  1323. .read_int_callback = mos7720_interrupt_callback,
  1324. };
  1325. static int __init moschip7720_init(void)
  1326. {
  1327. int retval;
  1328. dbg("%s: Entering ..........", __func__);
  1329. /* Register with the usb serial */
  1330. retval = usb_serial_register(&moschip7720_2port_driver);
  1331. if (retval)
  1332. goto failed_port_device_register;
  1333. info(DRIVER_DESC " " DRIVER_VERSION);
  1334. /* Register with the usb */
  1335. retval = usb_register(&usb_driver);
  1336. if (retval)
  1337. goto failed_usb_register;
  1338. return 0;
  1339. failed_usb_register:
  1340. usb_serial_deregister(&moschip7720_2port_driver);
  1341. failed_port_device_register:
  1342. return retval;
  1343. }
  1344. static void __exit moschip7720_exit(void)
  1345. {
  1346. usb_deregister(&usb_driver);
  1347. usb_serial_deregister(&moschip7720_2port_driver);
  1348. }
  1349. module_init(moschip7720_init);
  1350. module_exit(moschip7720_exit);
  1351. /* Module information */
  1352. MODULE_AUTHOR( DRIVER_AUTHOR );
  1353. MODULE_DESCRIPTION( DRIVER_DESC );
  1354. MODULE_LICENSE("GPL");
  1355. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1356. MODULE_PARM_DESC(debug, "Debug enabled or not");