hso.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  7. * <ajb@spheresystems.co.uk>
  8. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  9. * Copyright (C) 2008 Novell, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  23. * USA
  24. *
  25. *
  26. *****************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Description of the device:
  30. *
  31. * Interface 0: Contains the IP network interface on the bulk end points.
  32. * The multiplexed serial ports are using the interrupt and
  33. * control endpoints.
  34. * Interrupt contains a bitmap telling which multiplexed
  35. * serialport needs servicing.
  36. *
  37. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  38. * port is opened, as this have a huge impact on the network port
  39. * throughput.
  40. *
  41. * Interface 2: Standard modem interface - circuit switched interface, should
  42. * not be used.
  43. *
  44. *****************************************************************************/
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/init.h>
  48. #include <linux/delay.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/module.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/usb.h>
  53. #include <linux/timer.h>
  54. #include <linux/tty.h>
  55. #include <linux/tty_driver.h>
  56. #include <linux/tty_flip.h>
  57. #include <linux/kmod.h>
  58. #include <linux/rfkill.h>
  59. #include <linux/ip.h>
  60. #include <linux/uaccess.h>
  61. #include <linux/usb/cdc.h>
  62. #include <net/arp.h>
  63. #include <asm/byteorder.h>
  64. #define DRIVER_VERSION "1.2"
  65. #define MOD_AUTHOR "Option Wireless"
  66. #define MOD_DESCRIPTION "USB High Speed Option driver"
  67. #define MOD_LICENSE "GPL"
  68. #define HSO_MAX_NET_DEVICES 10
  69. #define HSO__MAX_MTU 2048
  70. #define DEFAULT_MTU 1500
  71. #define DEFAULT_MRU 1500
  72. #define CTRL_URB_RX_SIZE 1024
  73. #define CTRL_URB_TX_SIZE 64
  74. #define BULK_URB_RX_SIZE 4096
  75. #define BULK_URB_TX_SIZE 8192
  76. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  77. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  78. #define MUX_BULK_RX_BUF_COUNT 4
  79. #define USB_TYPE_OPTION_VENDOR 0x20
  80. /* These definitions are used with the struct hso_net flags element */
  81. /* - use *_bit operations on it. (bit indices not values.) */
  82. #define HSO_NET_RUNNING 0
  83. #define HSO_NET_TX_TIMEOUT (HZ*10)
  84. #define HSO_SERIAL_MAGIC 0x48534f31
  85. /* Number of ttys to handle */
  86. #define HSO_SERIAL_TTY_MINORS 256
  87. #define MAX_RX_URBS 2
  88. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  89. {
  90. if (tty)
  91. return tty->driver_data;
  92. return NULL;
  93. }
  94. /*****************************************************************************/
  95. /* Debugging functions */
  96. /*****************************************************************************/
  97. #define D__(lvl_, fmt, arg...) \
  98. do { \
  99. printk(lvl_ "[%d:%s]: " fmt "\n", \
  100. __LINE__, __func__, ## arg); \
  101. } while (0)
  102. #define D_(lvl, args...) \
  103. do { \
  104. if (lvl & debug) \
  105. D__(KERN_INFO, args); \
  106. } while (0)
  107. #define D1(args...) D_(0x01, ##args)
  108. #define D2(args...) D_(0x02, ##args)
  109. #define D3(args...) D_(0x04, ##args)
  110. #define D4(args...) D_(0x08, ##args)
  111. #define D5(args...) D_(0x10, ##args)
  112. /*****************************************************************************/
  113. /* Enumerators */
  114. /*****************************************************************************/
  115. enum pkt_parse_state {
  116. WAIT_IP,
  117. WAIT_DATA,
  118. WAIT_SYNC
  119. };
  120. /*****************************************************************************/
  121. /* Structs */
  122. /*****************************************************************************/
  123. struct hso_shared_int {
  124. struct usb_endpoint_descriptor *intr_endp;
  125. void *shared_intr_buf;
  126. struct urb *shared_intr_urb;
  127. struct usb_device *usb;
  128. int use_count;
  129. int ref_count;
  130. struct mutex shared_int_lock;
  131. };
  132. struct hso_net {
  133. struct hso_device *parent;
  134. struct net_device *net;
  135. struct rfkill *rfkill;
  136. struct usb_endpoint_descriptor *in_endp;
  137. struct usb_endpoint_descriptor *out_endp;
  138. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  139. struct urb *mux_bulk_tx_urb;
  140. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  141. void *mux_bulk_tx_buf;
  142. struct sk_buff *skb_rx_buf;
  143. struct sk_buff *skb_tx_buf;
  144. enum pkt_parse_state rx_parse_state;
  145. spinlock_t net_lock;
  146. unsigned short rx_buf_size;
  147. unsigned short rx_buf_missing;
  148. struct iphdr rx_ip_hdr;
  149. unsigned long flags;
  150. };
  151. enum rx_ctrl_state{
  152. RX_IDLE,
  153. RX_SENT,
  154. RX_PENDING
  155. };
  156. struct hso_serial {
  157. struct hso_device *parent;
  158. int magic;
  159. u8 minor;
  160. struct hso_shared_int *shared_int;
  161. /* rx/tx urb could be either a bulk urb or a control urb depending
  162. on which serial port it is used on. */
  163. struct urb *rx_urb[MAX_RX_URBS];
  164. u8 num_rx_urbs;
  165. u8 *rx_data[MAX_RX_URBS];
  166. u16 rx_data_length; /* should contain allocated length */
  167. struct urb *tx_urb;
  168. u8 *tx_data;
  169. u8 *tx_buffer;
  170. u16 tx_data_length; /* should contain allocated length */
  171. u16 tx_data_count;
  172. u16 tx_buffer_count;
  173. struct usb_ctrlrequest ctrl_req_tx;
  174. struct usb_ctrlrequest ctrl_req_rx;
  175. struct usb_endpoint_descriptor *in_endp;
  176. struct usb_endpoint_descriptor *out_endp;
  177. enum rx_ctrl_state rx_state;
  178. u8 rts_state;
  179. u8 dtr_state;
  180. unsigned tx_urb_used:1;
  181. /* from usb_serial_port */
  182. struct tty_struct *tty;
  183. int open_count;
  184. spinlock_t serial_lock;
  185. int (*write_data) (struct hso_serial *serial);
  186. /* Hacks required to get flow control
  187. * working on the serial receive buffers
  188. * so as not to drop characters on the floor.
  189. */
  190. int curr_rx_urb_idx;
  191. u16 curr_rx_urb_offset;
  192. u8 rx_urb_filled[MAX_RX_URBS];
  193. struct tasklet_struct unthrottle_tasklet;
  194. struct work_struct retry_unthrottle_workqueue;
  195. };
  196. struct hso_device {
  197. union {
  198. struct hso_serial *dev_serial;
  199. struct hso_net *dev_net;
  200. } port_data;
  201. u32 port_spec;
  202. u8 is_active;
  203. u8 usb_gone;
  204. struct work_struct async_get_intf;
  205. struct work_struct async_put_intf;
  206. struct usb_device *usb;
  207. struct usb_interface *interface;
  208. struct device *dev;
  209. struct kref ref;
  210. struct mutex mutex;
  211. };
  212. /* Type of interface */
  213. #define HSO_INTF_MASK 0xFF00
  214. #define HSO_INTF_MUX 0x0100
  215. #define HSO_INTF_BULK 0x0200
  216. /* Type of port */
  217. #define HSO_PORT_MASK 0xFF
  218. #define HSO_PORT_NO_PORT 0x0
  219. #define HSO_PORT_CONTROL 0x1
  220. #define HSO_PORT_APP 0x2
  221. #define HSO_PORT_GPS 0x3
  222. #define HSO_PORT_PCSC 0x4
  223. #define HSO_PORT_APP2 0x5
  224. #define HSO_PORT_GPS_CONTROL 0x6
  225. #define HSO_PORT_MSD 0x7
  226. #define HSO_PORT_VOICE 0x8
  227. #define HSO_PORT_DIAG2 0x9
  228. #define HSO_PORT_DIAG 0x10
  229. #define HSO_PORT_MODEM 0x11
  230. #define HSO_PORT_NETWORK 0x12
  231. /* Additional device info */
  232. #define HSO_INFO_MASK 0xFF000000
  233. #define HSO_INFO_CRC_BUG 0x01000000
  234. /*****************************************************************************/
  235. /* Prototypes */
  236. /*****************************************************************************/
  237. /* Serial driver functions */
  238. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  239. unsigned int set, unsigned int clear);
  240. static void ctrl_callback(struct urb *urb);
  241. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  242. static void hso_kick_transmit(struct hso_serial *serial);
  243. /* Helper functions */
  244. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  245. struct usb_device *usb, gfp_t gfp);
  246. static void log_usb_status(int status, const char *function);
  247. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  248. int type, int dir);
  249. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  250. static void hso_free_interface(struct usb_interface *intf);
  251. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  252. static int hso_stop_serial_device(struct hso_device *hso_dev);
  253. static int hso_start_net_device(struct hso_device *hso_dev);
  254. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  255. static int hso_stop_net_device(struct hso_device *hso_dev);
  256. static void hso_serial_ref_free(struct kref *ref);
  257. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  258. static int hso_mux_serial_read(struct hso_serial *serial);
  259. static void async_get_intf(struct work_struct *data);
  260. static void async_put_intf(struct work_struct *data);
  261. static int hso_put_activity(struct hso_device *hso_dev);
  262. static int hso_get_activity(struct hso_device *hso_dev);
  263. /*****************************************************************************/
  264. /* Helping functions */
  265. /*****************************************************************************/
  266. /* #define DEBUG */
  267. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  268. {
  269. return hso_dev->port_data.dev_net;
  270. }
  271. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  272. {
  273. return hso_dev->port_data.dev_serial;
  274. }
  275. /* Debugging functions */
  276. #ifdef DEBUG
  277. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  278. unsigned int len)
  279. {
  280. static char name[255];
  281. sprintf(name, "hso[%d:%s]", line_count, func_name);
  282. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  283. }
  284. #define DUMP(buf_, len_) \
  285. dbg_dump(__LINE__, __func__, buf_, len_)
  286. #define DUMP1(buf_, len_) \
  287. do { \
  288. if (0x01 & debug) \
  289. DUMP(buf_, len_); \
  290. } while (0)
  291. #else
  292. #define DUMP(buf_, len_)
  293. #define DUMP1(buf_, len_)
  294. #endif
  295. /* module parameters */
  296. static int debug;
  297. static int tty_major;
  298. static int disable_net;
  299. /* driver info */
  300. static const char driver_name[] = "hso";
  301. static const char tty_filename[] = "ttyHS";
  302. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  303. /* the usb driver itself (registered in hso_init) */
  304. static struct usb_driver hso_driver;
  305. /* serial structures */
  306. static struct tty_driver *tty_drv;
  307. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  308. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  309. static spinlock_t serial_table_lock;
  310. static const s32 default_port_spec[] = {
  311. HSO_INTF_MUX | HSO_PORT_NETWORK,
  312. HSO_INTF_BULK | HSO_PORT_DIAG,
  313. HSO_INTF_BULK | HSO_PORT_MODEM,
  314. 0
  315. };
  316. static const s32 icon321_port_spec[] = {
  317. HSO_INTF_MUX | HSO_PORT_NETWORK,
  318. HSO_INTF_BULK | HSO_PORT_DIAG2,
  319. HSO_INTF_BULK | HSO_PORT_MODEM,
  320. HSO_INTF_BULK | HSO_PORT_DIAG,
  321. 0
  322. };
  323. #define default_port_device(vendor, product) \
  324. USB_DEVICE(vendor, product), \
  325. .driver_info = (kernel_ulong_t)default_port_spec
  326. #define icon321_port_device(vendor, product) \
  327. USB_DEVICE(vendor, product), \
  328. .driver_info = (kernel_ulong_t)icon321_port_spec
  329. /* list of devices we support */
  330. static const struct usb_device_id hso_ids[] = {
  331. {default_port_device(0x0af0, 0x6711)},
  332. {default_port_device(0x0af0, 0x6731)},
  333. {default_port_device(0x0af0, 0x6751)},
  334. {default_port_device(0x0af0, 0x6771)},
  335. {default_port_device(0x0af0, 0x6791)},
  336. {default_port_device(0x0af0, 0x6811)},
  337. {default_port_device(0x0af0, 0x6911)},
  338. {default_port_device(0x0af0, 0x6951)},
  339. {default_port_device(0x0af0, 0x6971)},
  340. {default_port_device(0x0af0, 0x7011)},
  341. {default_port_device(0x0af0, 0x7031)},
  342. {default_port_device(0x0af0, 0x7051)},
  343. {default_port_device(0x0af0, 0x7071)},
  344. {default_port_device(0x0af0, 0x7111)},
  345. {default_port_device(0x0af0, 0x7211)},
  346. {default_port_device(0x0af0, 0x7251)},
  347. {default_port_device(0x0af0, 0x7271)},
  348. {default_port_device(0x0af0, 0x7311)},
  349. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  350. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  351. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  352. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  353. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  354. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  355. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  356. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  357. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  358. {USB_DEVICE(0x0af0, 0x7701)},
  359. {USB_DEVICE(0x0af0, 0x7801)},
  360. {USB_DEVICE(0x0af0, 0x7901)},
  361. {USB_DEVICE(0x0af0, 0x7361)},
  362. {icon321_port_device(0x0af0, 0xd051)},
  363. {}
  364. };
  365. MODULE_DEVICE_TABLE(usb, hso_ids);
  366. /* Sysfs attribute */
  367. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  368. struct device_attribute *attr,
  369. char *buf)
  370. {
  371. struct hso_device *hso_dev = dev->driver_data;
  372. char *port_name;
  373. if (!hso_dev)
  374. return 0;
  375. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  376. case HSO_PORT_CONTROL:
  377. port_name = "Control";
  378. break;
  379. case HSO_PORT_APP:
  380. port_name = "Application";
  381. break;
  382. case HSO_PORT_APP2:
  383. port_name = "Application2";
  384. break;
  385. case HSO_PORT_GPS:
  386. port_name = "GPS";
  387. break;
  388. case HSO_PORT_GPS_CONTROL:
  389. port_name = "GPS Control";
  390. break;
  391. case HSO_PORT_PCSC:
  392. port_name = "PCSC";
  393. break;
  394. case HSO_PORT_DIAG:
  395. port_name = "Diagnostic";
  396. break;
  397. case HSO_PORT_DIAG2:
  398. port_name = "Diagnostic2";
  399. break;
  400. case HSO_PORT_MODEM:
  401. port_name = "Modem";
  402. break;
  403. case HSO_PORT_NETWORK:
  404. port_name = "Network";
  405. break;
  406. default:
  407. port_name = "Unknown";
  408. break;
  409. }
  410. return sprintf(buf, "%s\n", port_name);
  411. }
  412. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  413. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  414. {
  415. int idx;
  416. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  417. if (serial->rx_urb[idx] == urb)
  418. return idx;
  419. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  420. return -1;
  421. }
  422. /* converts mux value to a port spec value */
  423. static u32 hso_mux_to_port(int mux)
  424. {
  425. u32 result;
  426. switch (mux) {
  427. case 0x1:
  428. result = HSO_PORT_CONTROL;
  429. break;
  430. case 0x2:
  431. result = HSO_PORT_APP;
  432. break;
  433. case 0x4:
  434. result = HSO_PORT_PCSC;
  435. break;
  436. case 0x8:
  437. result = HSO_PORT_GPS;
  438. break;
  439. case 0x10:
  440. result = HSO_PORT_APP2;
  441. break;
  442. default:
  443. result = HSO_PORT_NO_PORT;
  444. }
  445. return result;
  446. }
  447. /* converts port spec value to a mux value */
  448. static u32 hso_port_to_mux(int port)
  449. {
  450. u32 result;
  451. switch (port & HSO_PORT_MASK) {
  452. case HSO_PORT_CONTROL:
  453. result = 0x0;
  454. break;
  455. case HSO_PORT_APP:
  456. result = 0x1;
  457. break;
  458. case HSO_PORT_PCSC:
  459. result = 0x2;
  460. break;
  461. case HSO_PORT_GPS:
  462. result = 0x3;
  463. break;
  464. case HSO_PORT_APP2:
  465. result = 0x4;
  466. break;
  467. default:
  468. result = 0x0;
  469. }
  470. return result;
  471. }
  472. static struct hso_serial *get_serial_by_shared_int_and_type(
  473. struct hso_shared_int *shared_int,
  474. int mux)
  475. {
  476. int i, port;
  477. port = hso_mux_to_port(mux);
  478. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  479. if (serial_table[i]
  480. && (dev2ser(serial_table[i])->shared_int == shared_int)
  481. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  482. return dev2ser(serial_table[i]);
  483. }
  484. }
  485. return NULL;
  486. }
  487. static struct hso_serial *get_serial_by_index(unsigned index)
  488. {
  489. struct hso_serial *serial = NULL;
  490. unsigned long flags;
  491. spin_lock_irqsave(&serial_table_lock, flags);
  492. if (serial_table[index])
  493. serial = dev2ser(serial_table[index]);
  494. spin_unlock_irqrestore(&serial_table_lock, flags);
  495. return serial;
  496. }
  497. static int get_free_serial_index(void)
  498. {
  499. int index;
  500. unsigned long flags;
  501. spin_lock_irqsave(&serial_table_lock, flags);
  502. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  503. if (serial_table[index] == NULL) {
  504. spin_unlock_irqrestore(&serial_table_lock, flags);
  505. return index;
  506. }
  507. }
  508. spin_unlock_irqrestore(&serial_table_lock, flags);
  509. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  510. return -1;
  511. }
  512. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  513. {
  514. unsigned long flags;
  515. spin_lock_irqsave(&serial_table_lock, flags);
  516. if (serial)
  517. serial_table[index] = serial->parent;
  518. else
  519. serial_table[index] = NULL;
  520. spin_unlock_irqrestore(&serial_table_lock, flags);
  521. }
  522. /* log a meaningful explanation of an USB status */
  523. static void log_usb_status(int status, const char *function)
  524. {
  525. char *explanation;
  526. switch (status) {
  527. case -ENODEV:
  528. explanation = "no device";
  529. break;
  530. case -ENOENT:
  531. explanation = "endpoint not enabled";
  532. break;
  533. case -EPIPE:
  534. explanation = "endpoint stalled";
  535. break;
  536. case -ENOSPC:
  537. explanation = "not enough bandwidth";
  538. break;
  539. case -ESHUTDOWN:
  540. explanation = "device disabled";
  541. break;
  542. case -EHOSTUNREACH:
  543. explanation = "device suspended";
  544. break;
  545. case -EINVAL:
  546. case -EAGAIN:
  547. case -EFBIG:
  548. case -EMSGSIZE:
  549. explanation = "internal error";
  550. break;
  551. default:
  552. explanation = "unknown status";
  553. break;
  554. }
  555. D1("%s: received USB status - %s (%d)", function, explanation, status);
  556. }
  557. /* Network interface functions */
  558. /* called when net interface is brought up by ifconfig */
  559. static int hso_net_open(struct net_device *net)
  560. {
  561. struct hso_net *odev = netdev_priv(net);
  562. unsigned long flags = 0;
  563. if (!odev) {
  564. dev_err(&net->dev, "No net device !\n");
  565. return -ENODEV;
  566. }
  567. odev->skb_tx_buf = NULL;
  568. /* setup environment */
  569. spin_lock_irqsave(&odev->net_lock, flags);
  570. odev->rx_parse_state = WAIT_IP;
  571. odev->rx_buf_size = 0;
  572. odev->rx_buf_missing = sizeof(struct iphdr);
  573. spin_unlock_irqrestore(&odev->net_lock, flags);
  574. /* We are up and running. */
  575. set_bit(HSO_NET_RUNNING, &odev->flags);
  576. hso_start_net_device(odev->parent);
  577. /* Tell the kernel we are ready to start receiving from it */
  578. netif_start_queue(net);
  579. return 0;
  580. }
  581. /* called when interface is brought down by ifconfig */
  582. static int hso_net_close(struct net_device *net)
  583. {
  584. struct hso_net *odev = netdev_priv(net);
  585. /* we don't need the queue anymore */
  586. netif_stop_queue(net);
  587. /* no longer running */
  588. clear_bit(HSO_NET_RUNNING, &odev->flags);
  589. hso_stop_net_device(odev->parent);
  590. /* done */
  591. return 0;
  592. }
  593. /* USB tells is xmit done, we should start the netqueue again */
  594. static void write_bulk_callback(struct urb *urb)
  595. {
  596. struct hso_net *odev = urb->context;
  597. int status = urb->status;
  598. /* Sanity check */
  599. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  600. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  601. return;
  602. }
  603. /* Do we still have a valid kernel network device? */
  604. if (!netif_device_present(odev->net)) {
  605. dev_err(&urb->dev->dev, "%s: net device not present\n",
  606. __func__);
  607. return;
  608. }
  609. /* log status, but don't act on it, we don't need to resubmit anything
  610. * anyhow */
  611. if (status)
  612. log_usb_status(status, __func__);
  613. hso_put_activity(odev->parent);
  614. /* Tell the network interface we are ready for another frame */
  615. netif_wake_queue(odev->net);
  616. }
  617. /* called by kernel when we need to transmit a packet */
  618. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  619. {
  620. struct hso_net *odev = netdev_priv(net);
  621. int result;
  622. /* Tell the kernel, "No more frames 'til we are done with this one." */
  623. netif_stop_queue(net);
  624. if (hso_get_activity(odev->parent) == -EAGAIN) {
  625. odev->skb_tx_buf = skb;
  626. return 0;
  627. }
  628. /* log if asked */
  629. DUMP1(skb->data, skb->len);
  630. /* Copy it from kernel memory to OUR memory */
  631. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  632. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  633. /* Fill in the URB for shipping it out. */
  634. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  635. odev->parent->usb,
  636. usb_sndbulkpipe(odev->parent->usb,
  637. odev->out_endp->
  638. bEndpointAddress & 0x7F),
  639. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  640. odev);
  641. /* Deal with the Zero Length packet problem, I hope */
  642. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  643. /* Send the URB on its merry way. */
  644. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  645. if (result) {
  646. dev_warn(&odev->parent->interface->dev,
  647. "failed mux_bulk_tx_urb %d", result);
  648. net->stats.tx_errors++;
  649. netif_start_queue(net);
  650. } else {
  651. net->stats.tx_packets++;
  652. net->stats.tx_bytes += skb->len;
  653. /* And tell the kernel when the last transmit started. */
  654. net->trans_start = jiffies;
  655. }
  656. dev_kfree_skb(skb);
  657. /* we're done */
  658. return result;
  659. }
  660. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  661. {
  662. struct hso_net *odev = netdev_priv(net);
  663. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  664. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  665. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  666. }
  667. static struct ethtool_ops ops = {
  668. .get_drvinfo = hso_get_drvinfo,
  669. .get_link = ethtool_op_get_link
  670. };
  671. /* called when a packet did not ack after watchdogtimeout */
  672. static void hso_net_tx_timeout(struct net_device *net)
  673. {
  674. struct hso_net *odev = netdev_priv(net);
  675. if (!odev)
  676. return;
  677. /* Tell syslog we are hosed. */
  678. dev_warn(&net->dev, "Tx timed out.\n");
  679. /* Tear the waiting frame off the list */
  680. if (odev->mux_bulk_tx_urb
  681. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  682. usb_unlink_urb(odev->mux_bulk_tx_urb);
  683. /* Update statistics */
  684. net->stats.tx_errors++;
  685. }
  686. /* make a real packet from the received USB buffer */
  687. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  688. unsigned int count, unsigned char is_eop)
  689. {
  690. unsigned short temp_bytes;
  691. unsigned short buffer_offset = 0;
  692. unsigned short frame_len;
  693. unsigned char *tmp_rx_buf;
  694. /* log if needed */
  695. D1("Rx %d bytes", count);
  696. DUMP(ip_pkt, min(128, (int)count));
  697. while (count) {
  698. switch (odev->rx_parse_state) {
  699. case WAIT_IP:
  700. /* waiting for IP header. */
  701. /* wanted bytes - size of ip header */
  702. temp_bytes =
  703. (count <
  704. odev->rx_buf_missing) ? count : odev->
  705. rx_buf_missing;
  706. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  707. odev->rx_buf_size, ip_pkt + buffer_offset,
  708. temp_bytes);
  709. odev->rx_buf_size += temp_bytes;
  710. buffer_offset += temp_bytes;
  711. odev->rx_buf_missing -= temp_bytes;
  712. count -= temp_bytes;
  713. if (!odev->rx_buf_missing) {
  714. /* header is complete allocate an sk_buffer and
  715. * continue to WAIT_DATA */
  716. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  717. if ((frame_len > DEFAULT_MRU) ||
  718. (frame_len < sizeof(struct iphdr))) {
  719. dev_err(&odev->net->dev,
  720. "Invalid frame (%d) length\n",
  721. frame_len);
  722. odev->rx_parse_state = WAIT_SYNC;
  723. continue;
  724. }
  725. /* Allocate an sk_buff */
  726. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  727. if (!odev->skb_rx_buf) {
  728. /* We got no receive buffer. */
  729. D1("could not allocate memory");
  730. odev->rx_parse_state = WAIT_SYNC;
  731. return;
  732. }
  733. /* Here's where it came from */
  734. odev->skb_rx_buf->dev = odev->net;
  735. /* Copy what we got so far. make room for iphdr
  736. * after tail. */
  737. tmp_rx_buf =
  738. skb_put(odev->skb_rx_buf,
  739. sizeof(struct iphdr));
  740. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  741. sizeof(struct iphdr));
  742. /* ETH_HLEN */
  743. odev->rx_buf_size = sizeof(struct iphdr);
  744. /* Filip actually use .tot_len */
  745. odev->rx_buf_missing =
  746. frame_len - sizeof(struct iphdr);
  747. odev->rx_parse_state = WAIT_DATA;
  748. }
  749. break;
  750. case WAIT_DATA:
  751. temp_bytes = (count < odev->rx_buf_missing)
  752. ? count : odev->rx_buf_missing;
  753. /* Copy the rest of the bytes that are left in the
  754. * buffer into the waiting sk_buf. */
  755. /* Make room for temp_bytes after tail. */
  756. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  757. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  758. odev->rx_buf_missing -= temp_bytes;
  759. count -= temp_bytes;
  760. buffer_offset += temp_bytes;
  761. odev->rx_buf_size += temp_bytes;
  762. if (!odev->rx_buf_missing) {
  763. /* Packet is complete. Inject into stack. */
  764. /* We have IP packet here */
  765. odev->skb_rx_buf->protocol =
  766. __constant_htons(ETH_P_IP);
  767. /* don't check it */
  768. odev->skb_rx_buf->ip_summed =
  769. CHECKSUM_UNNECESSARY;
  770. skb_reset_mac_header(odev->skb_rx_buf);
  771. /* Ship it off to the kernel */
  772. netif_rx(odev->skb_rx_buf);
  773. /* No longer our buffer. */
  774. odev->skb_rx_buf = NULL;
  775. /* update out statistics */
  776. odev->net->stats.rx_packets++;
  777. odev->net->stats.rx_bytes += odev->rx_buf_size;
  778. odev->rx_buf_size = 0;
  779. odev->rx_buf_missing = sizeof(struct iphdr);
  780. odev->rx_parse_state = WAIT_IP;
  781. }
  782. break;
  783. case WAIT_SYNC:
  784. D1(" W_S");
  785. count = 0;
  786. break;
  787. default:
  788. D1(" ");
  789. count--;
  790. break;
  791. }
  792. }
  793. /* Recovery mechanism for WAIT_SYNC state. */
  794. if (is_eop) {
  795. if (odev->rx_parse_state == WAIT_SYNC) {
  796. odev->rx_parse_state = WAIT_IP;
  797. odev->rx_buf_size = 0;
  798. odev->rx_buf_missing = sizeof(struct iphdr);
  799. }
  800. }
  801. }
  802. /* Moving data from usb to kernel (in interrupt state) */
  803. static void read_bulk_callback(struct urb *urb)
  804. {
  805. struct hso_net *odev = urb->context;
  806. struct net_device *net;
  807. int result;
  808. int status = urb->status;
  809. /* is al ok? (Filip: Who's Al ?) */
  810. if (status) {
  811. log_usb_status(status, __func__);
  812. return;
  813. }
  814. /* Sanity check */
  815. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  816. D1("BULK IN callback but driver is not active!");
  817. return;
  818. }
  819. usb_mark_last_busy(urb->dev);
  820. net = odev->net;
  821. if (!netif_device_present(net)) {
  822. /* Somebody killed our network interface... */
  823. return;
  824. }
  825. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  826. u32 rest;
  827. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  828. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  829. if (((rest == 5) || (rest == 6))
  830. && !memcmp(((u8 *) urb->transfer_buffer) +
  831. urb->actual_length - 4, crc_check, 4)) {
  832. urb->actual_length -= 4;
  833. }
  834. }
  835. /* do we even have a packet? */
  836. if (urb->actual_length) {
  837. /* Handle the IP stream, add header and push it onto network
  838. * stack if the packet is complete. */
  839. spin_lock(&odev->net_lock);
  840. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  841. (urb->transfer_buffer_length >
  842. urb->actual_length) ? 1 : 0);
  843. spin_unlock(&odev->net_lock);
  844. }
  845. /* We are done with this URB, resubmit it. Prep the USB to wait for
  846. * another frame. Reuse same as received. */
  847. usb_fill_bulk_urb(urb,
  848. odev->parent->usb,
  849. usb_rcvbulkpipe(odev->parent->usb,
  850. odev->in_endp->
  851. bEndpointAddress & 0x7F),
  852. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  853. read_bulk_callback, odev);
  854. /* Give this to the USB subsystem so it can tell us when more data
  855. * arrives. */
  856. result = usb_submit_urb(urb, GFP_ATOMIC);
  857. if (result)
  858. dev_warn(&odev->parent->interface->dev,
  859. "%s failed submit mux_bulk_rx_urb %d", __func__,
  860. result);
  861. }
  862. /* Serial driver functions */
  863. static void hso_init_termios(struct ktermios *termios)
  864. {
  865. /*
  866. * The default requirements for this device are:
  867. */
  868. termios->c_iflag &=
  869. ~(IGNBRK /* disable ignore break */
  870. | BRKINT /* disable break causes interrupt */
  871. | PARMRK /* disable mark parity errors */
  872. | ISTRIP /* disable clear high bit of input characters */
  873. | INLCR /* disable translate NL to CR */
  874. | IGNCR /* disable ignore CR */
  875. | ICRNL /* disable translate CR to NL */
  876. | IXON); /* disable enable XON/XOFF flow control */
  877. /* disable postprocess output characters */
  878. termios->c_oflag &= ~OPOST;
  879. termios->c_lflag &=
  880. ~(ECHO /* disable echo input characters */
  881. | ECHONL /* disable echo new line */
  882. | ICANON /* disable erase, kill, werase, and rprnt
  883. special characters */
  884. | ISIG /* disable interrupt, quit, and suspend special
  885. characters */
  886. | IEXTEN); /* disable non-POSIX special characters */
  887. termios->c_cflag &=
  888. ~(CSIZE /* no size */
  889. | PARENB /* disable parity bit */
  890. | CBAUD /* clear current baud rate */
  891. | CBAUDEX); /* clear current buad rate */
  892. termios->c_cflag |= CS8; /* character size 8 bits */
  893. /* baud rate 115200 */
  894. tty_termios_encode_baud_rate(termios, 115200, 115200);
  895. }
  896. static void _hso_serial_set_termios(struct tty_struct *tty,
  897. struct ktermios *old)
  898. {
  899. struct hso_serial *serial = get_serial_by_tty(tty);
  900. struct ktermios *termios;
  901. if (!serial) {
  902. printk(KERN_ERR "%s: no tty structures", __func__);
  903. return;
  904. }
  905. D4("port %d", serial->minor);
  906. /*
  907. * Fix up unsupported bits
  908. */
  909. termios = tty->termios;
  910. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  911. termios->c_cflag &=
  912. ~(CSIZE /* no size */
  913. | PARENB /* disable parity bit */
  914. | CBAUD /* clear current baud rate */
  915. | CBAUDEX); /* clear current buad rate */
  916. termios->c_cflag |= CS8; /* character size 8 bits */
  917. /* baud rate 115200 */
  918. tty_encode_baud_rate(tty, 115200, 115200);
  919. }
  920. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  921. {
  922. int result;
  923. #ifdef CONFIG_HSO_AUTOPM
  924. usb_mark_last_busy(urb->dev);
  925. #endif
  926. /* We are done with this URB, resubmit it. Prep the USB to wait for
  927. * another frame */
  928. usb_fill_bulk_urb(urb, serial->parent->usb,
  929. usb_rcvbulkpipe(serial->parent->usb,
  930. serial->in_endp->
  931. bEndpointAddress & 0x7F),
  932. urb->transfer_buffer, serial->rx_data_length,
  933. hso_std_serial_read_bulk_callback, serial);
  934. /* Give this to the USB subsystem so it can tell us when more data
  935. * arrives. */
  936. result = usb_submit_urb(urb, GFP_ATOMIC);
  937. if (result) {
  938. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  939. __func__, result);
  940. }
  941. }
  942. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  943. {
  944. int count;
  945. struct urb *curr_urb;
  946. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  947. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  948. count = put_rxbuf_data(curr_urb, serial);
  949. if (count == -1)
  950. return;
  951. if (count == 0) {
  952. serial->curr_rx_urb_idx++;
  953. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  954. serial->curr_rx_urb_idx = 0;
  955. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  956. }
  957. }
  958. }
  959. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  960. {
  961. int count = 0;
  962. struct urb *urb;
  963. urb = serial->rx_urb[0];
  964. if (serial->open_count > 0) {
  965. count = put_rxbuf_data(urb, serial);
  966. if (count == -1)
  967. return;
  968. }
  969. /* Re issue a read as long as we receive data. */
  970. if (count == 0 && ((urb->actual_length != 0) ||
  971. (serial->rx_state == RX_PENDING))) {
  972. serial->rx_state = RX_SENT;
  973. hso_mux_serial_read(serial);
  974. } else
  975. serial->rx_state = RX_IDLE;
  976. }
  977. /* read callback for Diag and CS port */
  978. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  979. {
  980. struct hso_serial *serial = urb->context;
  981. int status = urb->status;
  982. /* sanity check */
  983. if (!serial) {
  984. D1("serial == NULL");
  985. return;
  986. } else if (status) {
  987. log_usb_status(status, __func__);
  988. return;
  989. }
  990. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  991. D1("Actual length = %d\n", urb->actual_length);
  992. DUMP1(urb->transfer_buffer, urb->actual_length);
  993. /* Anyone listening? */
  994. if (serial->open_count == 0)
  995. return;
  996. if (status == 0) {
  997. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  998. u32 rest;
  999. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1000. rest =
  1001. urb->actual_length %
  1002. serial->in_endp->wMaxPacketSize;
  1003. if (((rest == 5) || (rest == 6))
  1004. && !memcmp(((u8 *) urb->transfer_buffer) +
  1005. urb->actual_length - 4, crc_check, 4)) {
  1006. urb->actual_length -= 4;
  1007. }
  1008. }
  1009. /* Valid data, handle RX data */
  1010. spin_lock(&serial->serial_lock);
  1011. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1012. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1013. spin_unlock(&serial->serial_lock);
  1014. } else if (status == -ENOENT || status == -ECONNRESET) {
  1015. /* Unlinked - check for throttled port. */
  1016. D2("Port %d, successfully unlinked urb", serial->minor);
  1017. spin_lock(&serial->serial_lock);
  1018. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1019. hso_resubmit_rx_bulk_urb(serial, urb);
  1020. spin_unlock(&serial->serial_lock);
  1021. } else {
  1022. D2("Port %d, status = %d for read urb", serial->minor, status);
  1023. return;
  1024. }
  1025. }
  1026. /*
  1027. * This needs to be a tasklet otherwise we will
  1028. * end up recursively calling this function.
  1029. */
  1030. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1031. {
  1032. unsigned long flags;
  1033. spin_lock_irqsave(&serial->serial_lock, flags);
  1034. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1035. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1036. else
  1037. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1038. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1039. }
  1040. static void hso_unthrottle(struct tty_struct *tty)
  1041. {
  1042. struct hso_serial *serial = get_serial_by_tty(tty);
  1043. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1044. }
  1045. void hso_unthrottle_workfunc(struct work_struct *work)
  1046. {
  1047. struct hso_serial *serial =
  1048. container_of(work, struct hso_serial,
  1049. retry_unthrottle_workqueue);
  1050. hso_unthrottle_tasklet(serial);
  1051. }
  1052. /* open the requested serial port */
  1053. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1054. {
  1055. struct hso_serial *serial = get_serial_by_index(tty->index);
  1056. int result;
  1057. /* sanity check */
  1058. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1059. WARN_ON(1);
  1060. tty->driver_data = NULL;
  1061. D1("Failed to open port");
  1062. return -ENODEV;
  1063. }
  1064. mutex_lock(&serial->parent->mutex);
  1065. result = usb_autopm_get_interface(serial->parent->interface);
  1066. if (result < 0)
  1067. goto err_out;
  1068. D1("Opening %d", serial->minor);
  1069. kref_get(&serial->parent->ref);
  1070. /* setup */
  1071. spin_lock_irq(&serial->serial_lock);
  1072. tty->driver_data = serial;
  1073. serial->tty = tty_kref_get(tty);
  1074. spin_unlock_irq(&serial->serial_lock);
  1075. /* check for port already opened, if not set the termios */
  1076. serial->open_count++;
  1077. if (serial->open_count == 1) {
  1078. tty->low_latency = 1;
  1079. serial->rx_state = RX_IDLE;
  1080. /* Force default termio settings */
  1081. _hso_serial_set_termios(tty, NULL);
  1082. tasklet_init(&serial->unthrottle_tasklet,
  1083. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1084. (unsigned long)serial);
  1085. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1086. hso_unthrottle_workfunc);
  1087. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1088. if (result) {
  1089. hso_stop_serial_device(serial->parent);
  1090. serial->open_count--;
  1091. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1092. }
  1093. } else {
  1094. D1("Port was already open");
  1095. }
  1096. usb_autopm_put_interface(serial->parent->interface);
  1097. /* done */
  1098. if (result)
  1099. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1100. err_out:
  1101. mutex_unlock(&serial->parent->mutex);
  1102. return result;
  1103. }
  1104. /* close the requested serial port */
  1105. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1106. {
  1107. struct hso_serial *serial = tty->driver_data;
  1108. u8 usb_gone;
  1109. D1("Closing serial port");
  1110. /* Open failed, no close cleanup required */
  1111. if (serial == NULL)
  1112. return;
  1113. mutex_lock(&serial->parent->mutex);
  1114. usb_gone = serial->parent->usb_gone;
  1115. if (!usb_gone)
  1116. usb_autopm_get_interface(serial->parent->interface);
  1117. /* reset the rts and dtr */
  1118. /* do the actual close */
  1119. serial->open_count--;
  1120. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1121. if (serial->open_count <= 0) {
  1122. serial->open_count = 0;
  1123. spin_lock_irq(&serial->serial_lock);
  1124. if (serial->tty == tty) {
  1125. serial->tty->driver_data = NULL;
  1126. serial->tty = NULL;
  1127. tty_kref_put(tty);
  1128. }
  1129. spin_unlock_irq(&serial->serial_lock);
  1130. if (!usb_gone)
  1131. hso_stop_serial_device(serial->parent);
  1132. tasklet_kill(&serial->unthrottle_tasklet);
  1133. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1134. }
  1135. if (!usb_gone)
  1136. usb_autopm_put_interface(serial->parent->interface);
  1137. mutex_unlock(&serial->parent->mutex);
  1138. }
  1139. /* close the requested serial port */
  1140. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1141. int count)
  1142. {
  1143. struct hso_serial *serial = get_serial_by_tty(tty);
  1144. int space, tx_bytes;
  1145. unsigned long flags;
  1146. /* sanity check */
  1147. if (serial == NULL) {
  1148. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1149. return -ENODEV;
  1150. }
  1151. spin_lock_irqsave(&serial->serial_lock, flags);
  1152. space = serial->tx_data_length - serial->tx_buffer_count;
  1153. tx_bytes = (count < space) ? count : space;
  1154. if (!tx_bytes)
  1155. goto out;
  1156. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1157. serial->tx_buffer_count += tx_bytes;
  1158. out:
  1159. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1160. hso_kick_transmit(serial);
  1161. /* done */
  1162. return tx_bytes;
  1163. }
  1164. /* how much room is there for writing */
  1165. static int hso_serial_write_room(struct tty_struct *tty)
  1166. {
  1167. struct hso_serial *serial = get_serial_by_tty(tty);
  1168. int room;
  1169. unsigned long flags;
  1170. spin_lock_irqsave(&serial->serial_lock, flags);
  1171. room = serial->tx_data_length - serial->tx_buffer_count;
  1172. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1173. /* return free room */
  1174. return room;
  1175. }
  1176. /* setup the term */
  1177. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1178. {
  1179. struct hso_serial *serial = get_serial_by_tty(tty);
  1180. unsigned long flags;
  1181. if (old)
  1182. D5("Termios called with: cflags new[%d] - old[%d]",
  1183. tty->termios->c_cflag, old->c_cflag);
  1184. /* the actual setup */
  1185. spin_lock_irqsave(&serial->serial_lock, flags);
  1186. if (serial->open_count)
  1187. _hso_serial_set_termios(tty, old);
  1188. else
  1189. tty->termios = old;
  1190. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1191. /* done */
  1192. return;
  1193. }
  1194. /* how many characters in the buffer */
  1195. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1196. {
  1197. struct hso_serial *serial = get_serial_by_tty(tty);
  1198. int chars;
  1199. unsigned long flags;
  1200. /* sanity check */
  1201. if (serial == NULL)
  1202. return 0;
  1203. spin_lock_irqsave(&serial->serial_lock, flags);
  1204. chars = serial->tx_buffer_count;
  1205. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1206. return chars;
  1207. }
  1208. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1209. {
  1210. unsigned int value;
  1211. struct hso_serial *serial = get_serial_by_tty(tty);
  1212. unsigned long flags;
  1213. /* sanity check */
  1214. if (!serial) {
  1215. D1("no tty structures");
  1216. return -EINVAL;
  1217. }
  1218. spin_lock_irqsave(&serial->serial_lock, flags);
  1219. value = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1220. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1221. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1222. return value;
  1223. }
  1224. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1225. unsigned int set, unsigned int clear)
  1226. {
  1227. int val = 0;
  1228. unsigned long flags;
  1229. int if_num;
  1230. struct hso_serial *serial = get_serial_by_tty(tty);
  1231. /* sanity check */
  1232. if (!serial) {
  1233. D1("no tty structures");
  1234. return -EINVAL;
  1235. }
  1236. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1237. spin_lock_irqsave(&serial->serial_lock, flags);
  1238. if (set & TIOCM_RTS)
  1239. serial->rts_state = 1;
  1240. if (set & TIOCM_DTR)
  1241. serial->dtr_state = 1;
  1242. if (clear & TIOCM_RTS)
  1243. serial->rts_state = 0;
  1244. if (clear & TIOCM_DTR)
  1245. serial->dtr_state = 0;
  1246. if (serial->dtr_state)
  1247. val |= 0x01;
  1248. if (serial->rts_state)
  1249. val |= 0x02;
  1250. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1251. return usb_control_msg(serial->parent->usb,
  1252. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1253. 0x21, val, if_num, NULL, 0,
  1254. USB_CTRL_SET_TIMEOUT);
  1255. }
  1256. /* starts a transmit */
  1257. static void hso_kick_transmit(struct hso_serial *serial)
  1258. {
  1259. u8 *temp;
  1260. unsigned long flags;
  1261. int res;
  1262. spin_lock_irqsave(&serial->serial_lock, flags);
  1263. if (!serial->tx_buffer_count)
  1264. goto out;
  1265. if (serial->tx_urb_used)
  1266. goto out;
  1267. /* Wakeup USB interface if necessary */
  1268. if (hso_get_activity(serial->parent) == -EAGAIN)
  1269. goto out;
  1270. /* Switch pointers around to avoid memcpy */
  1271. temp = serial->tx_buffer;
  1272. serial->tx_buffer = serial->tx_data;
  1273. serial->tx_data = temp;
  1274. serial->tx_data_count = serial->tx_buffer_count;
  1275. serial->tx_buffer_count = 0;
  1276. /* If temp is set, it means we switched buffers */
  1277. if (temp && serial->write_data) {
  1278. res = serial->write_data(serial);
  1279. if (res >= 0)
  1280. serial->tx_urb_used = 1;
  1281. }
  1282. out:
  1283. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1284. }
  1285. /* make a request (for reading and writing data to muxed serial port) */
  1286. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1287. struct urb *ctrl_urb,
  1288. struct usb_ctrlrequest *ctrl_req,
  1289. u8 *ctrl_urb_data, u32 size)
  1290. {
  1291. int result;
  1292. int pipe;
  1293. /* Sanity check */
  1294. if (!serial || !ctrl_urb || !ctrl_req) {
  1295. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1296. return -EINVAL;
  1297. }
  1298. /* initialize */
  1299. ctrl_req->wValue = 0;
  1300. ctrl_req->wIndex = hso_port_to_mux(port);
  1301. ctrl_req->wLength = size;
  1302. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1303. /* Reading command */
  1304. ctrl_req->bRequestType = USB_DIR_IN |
  1305. USB_TYPE_OPTION_VENDOR |
  1306. USB_RECIP_INTERFACE;
  1307. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1308. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1309. } else {
  1310. /* Writing command */
  1311. ctrl_req->bRequestType = USB_DIR_OUT |
  1312. USB_TYPE_OPTION_VENDOR |
  1313. USB_RECIP_INTERFACE;
  1314. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1315. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1316. }
  1317. /* syslog */
  1318. D2("%s command (%02x) len: %d, port: %d",
  1319. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1320. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1321. /* Load ctrl urb */
  1322. ctrl_urb->transfer_flags = 0;
  1323. usb_fill_control_urb(ctrl_urb,
  1324. serial->parent->usb,
  1325. pipe,
  1326. (u8 *) ctrl_req,
  1327. ctrl_urb_data, size, ctrl_callback, serial);
  1328. /* Send it on merry way */
  1329. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1330. if (result) {
  1331. dev_err(&ctrl_urb->dev->dev,
  1332. "%s failed submit ctrl_urb %d type %d", __func__,
  1333. result, type);
  1334. return result;
  1335. }
  1336. /* done */
  1337. return size;
  1338. }
  1339. /* called by intr_callback when read occurs */
  1340. static int hso_mux_serial_read(struct hso_serial *serial)
  1341. {
  1342. if (!serial)
  1343. return -EINVAL;
  1344. /* clean data */
  1345. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1346. /* make the request */
  1347. if (serial->num_rx_urbs != 1) {
  1348. dev_err(&serial->parent->interface->dev,
  1349. "ERROR: mux'd reads with multiple buffers "
  1350. "not possible\n");
  1351. return 0;
  1352. }
  1353. return mux_device_request(serial,
  1354. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1355. serial->parent->port_spec & HSO_PORT_MASK,
  1356. serial->rx_urb[0],
  1357. &serial->ctrl_req_rx,
  1358. serial->rx_data[0], serial->rx_data_length);
  1359. }
  1360. /* used for muxed serial port callback (muxed serial read) */
  1361. static void intr_callback(struct urb *urb)
  1362. {
  1363. struct hso_shared_int *shared_int = urb->context;
  1364. struct hso_serial *serial;
  1365. unsigned char *port_req;
  1366. int status = urb->status;
  1367. int i;
  1368. usb_mark_last_busy(urb->dev);
  1369. /* sanity check */
  1370. if (!shared_int)
  1371. return;
  1372. /* status check */
  1373. if (status) {
  1374. log_usb_status(status, __func__);
  1375. return;
  1376. }
  1377. D4("\n--- Got intr callback 0x%02X ---", status);
  1378. /* what request? */
  1379. port_req = urb->transfer_buffer;
  1380. D4(" port_req = 0x%.2X\n", *port_req);
  1381. /* loop over all muxed ports to find the one sending this */
  1382. for (i = 0; i < 8; i++) {
  1383. /* max 8 channels on MUX */
  1384. if (*port_req & (1 << i)) {
  1385. serial = get_serial_by_shared_int_and_type(shared_int,
  1386. (1 << i));
  1387. if (serial != NULL) {
  1388. D1("Pending read interrupt on port %d\n", i);
  1389. spin_lock(&serial->serial_lock);
  1390. if (serial->rx_state == RX_IDLE) {
  1391. /* Setup and send a ctrl req read on
  1392. * port i */
  1393. if (!serial->rx_urb_filled[0]) {
  1394. serial->rx_state = RX_SENT;
  1395. hso_mux_serial_read(serial);
  1396. } else
  1397. serial->rx_state = RX_PENDING;
  1398. } else {
  1399. D1("Already pending a read on "
  1400. "port %d\n", i);
  1401. }
  1402. spin_unlock(&serial->serial_lock);
  1403. }
  1404. }
  1405. }
  1406. /* Resubmit interrupt urb */
  1407. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1408. }
  1409. /* called for writing to muxed serial port */
  1410. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1411. {
  1412. if (NULL == serial)
  1413. return -EINVAL;
  1414. return mux_device_request(serial,
  1415. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1416. serial->parent->port_spec & HSO_PORT_MASK,
  1417. serial->tx_urb,
  1418. &serial->ctrl_req_tx,
  1419. serial->tx_data, serial->tx_data_count);
  1420. }
  1421. /* write callback for Diag and CS port */
  1422. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1423. {
  1424. struct hso_serial *serial = urb->context;
  1425. int status = urb->status;
  1426. struct tty_struct *tty;
  1427. /* sanity check */
  1428. if (!serial) {
  1429. D1("serial == NULL");
  1430. return;
  1431. }
  1432. spin_lock(&serial->serial_lock);
  1433. serial->tx_urb_used = 0;
  1434. tty = tty_kref_get(serial->tty);
  1435. spin_unlock(&serial->serial_lock);
  1436. if (status) {
  1437. log_usb_status(status, __func__);
  1438. tty_kref_put(tty);
  1439. return;
  1440. }
  1441. hso_put_activity(serial->parent);
  1442. if (tty) {
  1443. tty_wakeup(tty);
  1444. tty_kref_put(tty);
  1445. }
  1446. hso_kick_transmit(serial);
  1447. D1(" ");
  1448. return;
  1449. }
  1450. /* called for writing diag or CS serial port */
  1451. static int hso_std_serial_write_data(struct hso_serial *serial)
  1452. {
  1453. int count = serial->tx_data_count;
  1454. int result;
  1455. usb_fill_bulk_urb(serial->tx_urb,
  1456. serial->parent->usb,
  1457. usb_sndbulkpipe(serial->parent->usb,
  1458. serial->out_endp->
  1459. bEndpointAddress & 0x7F),
  1460. serial->tx_data, serial->tx_data_count,
  1461. hso_std_serial_write_bulk_callback, serial);
  1462. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1463. if (result) {
  1464. dev_warn(&serial->parent->usb->dev,
  1465. "Failed to submit urb - res %d\n", result);
  1466. return result;
  1467. }
  1468. return count;
  1469. }
  1470. /* callback after read or write on muxed serial port */
  1471. static void ctrl_callback(struct urb *urb)
  1472. {
  1473. struct hso_serial *serial = urb->context;
  1474. struct usb_ctrlrequest *req;
  1475. int status = urb->status;
  1476. struct tty_struct *tty;
  1477. /* sanity check */
  1478. if (!serial)
  1479. return;
  1480. spin_lock(&serial->serial_lock);
  1481. serial->tx_urb_used = 0;
  1482. tty = tty_kref_get(serial->tty);
  1483. spin_unlock(&serial->serial_lock);
  1484. if (status) {
  1485. log_usb_status(status, __func__);
  1486. tty_kref_put(tty);
  1487. return;
  1488. }
  1489. /* what request? */
  1490. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1491. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1492. D4("Actual length of urb = %d\n", urb->actual_length);
  1493. DUMP1(urb->transfer_buffer, urb->actual_length);
  1494. if (req->bRequestType ==
  1495. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1496. /* response to a read command */
  1497. serial->rx_urb_filled[0] = 1;
  1498. spin_lock(&serial->serial_lock);
  1499. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1500. spin_unlock(&serial->serial_lock);
  1501. } else {
  1502. hso_put_activity(serial->parent);
  1503. if (tty)
  1504. tty_wakeup(tty);
  1505. /* response to a write command */
  1506. hso_kick_transmit(serial);
  1507. }
  1508. tty_kref_put(tty);
  1509. }
  1510. /* handle RX data for serial port */
  1511. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1512. {
  1513. struct tty_struct *tty;
  1514. int write_length_remaining = 0;
  1515. int curr_write_len;
  1516. /* Sanity check */
  1517. if (urb == NULL || serial == NULL) {
  1518. D1("serial = NULL");
  1519. return -2;
  1520. }
  1521. spin_lock(&serial->serial_lock);
  1522. tty = tty_kref_get(serial->tty);
  1523. spin_unlock(&serial->serial_lock);
  1524. /* Push data to tty */
  1525. if (tty) {
  1526. write_length_remaining = urb->actual_length -
  1527. serial->curr_rx_urb_offset;
  1528. D1("data to push to tty");
  1529. while (write_length_remaining) {
  1530. if (test_bit(TTY_THROTTLED, &tty->flags))
  1531. return -1;
  1532. curr_write_len = tty_insert_flip_string
  1533. (tty, urb->transfer_buffer +
  1534. serial->curr_rx_urb_offset,
  1535. write_length_remaining);
  1536. serial->curr_rx_urb_offset += curr_write_len;
  1537. write_length_remaining -= curr_write_len;
  1538. tty_flip_buffer_push(tty);
  1539. }
  1540. }
  1541. if (write_length_remaining == 0) {
  1542. serial->curr_rx_urb_offset = 0;
  1543. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1544. }
  1545. tty_kref_put(tty);
  1546. return write_length_remaining;
  1547. }
  1548. /* Base driver functions */
  1549. static void hso_log_port(struct hso_device *hso_dev)
  1550. {
  1551. char *port_type;
  1552. char port_dev[20];
  1553. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1554. case HSO_PORT_CONTROL:
  1555. port_type = "Control";
  1556. break;
  1557. case HSO_PORT_APP:
  1558. port_type = "Application";
  1559. break;
  1560. case HSO_PORT_GPS:
  1561. port_type = "GPS";
  1562. break;
  1563. case HSO_PORT_GPS_CONTROL:
  1564. port_type = "GPS control";
  1565. break;
  1566. case HSO_PORT_APP2:
  1567. port_type = "Application2";
  1568. break;
  1569. case HSO_PORT_PCSC:
  1570. port_type = "PCSC";
  1571. break;
  1572. case HSO_PORT_DIAG:
  1573. port_type = "Diagnostic";
  1574. break;
  1575. case HSO_PORT_DIAG2:
  1576. port_type = "Diagnostic2";
  1577. break;
  1578. case HSO_PORT_MODEM:
  1579. port_type = "Modem";
  1580. break;
  1581. case HSO_PORT_NETWORK:
  1582. port_type = "Network";
  1583. break;
  1584. default:
  1585. port_type = "Unknown";
  1586. break;
  1587. }
  1588. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1589. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1590. } else
  1591. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1592. dev2ser(hso_dev)->minor);
  1593. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1594. port_type, port_dev);
  1595. }
  1596. static int hso_start_net_device(struct hso_device *hso_dev)
  1597. {
  1598. int i, result = 0;
  1599. struct hso_net *hso_net = dev2net(hso_dev);
  1600. if (!hso_net)
  1601. return -ENODEV;
  1602. /* send URBs for all read buffers */
  1603. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1604. /* Prep a receive URB */
  1605. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1606. hso_dev->usb,
  1607. usb_rcvbulkpipe(hso_dev->usb,
  1608. hso_net->in_endp->
  1609. bEndpointAddress & 0x7F),
  1610. hso_net->mux_bulk_rx_buf_pool[i],
  1611. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1612. hso_net);
  1613. /* Put it out there so the device can send us stuff */
  1614. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1615. GFP_NOIO);
  1616. if (result)
  1617. dev_warn(&hso_dev->usb->dev,
  1618. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1619. i, result);
  1620. }
  1621. return result;
  1622. }
  1623. static int hso_stop_net_device(struct hso_device *hso_dev)
  1624. {
  1625. int i;
  1626. struct hso_net *hso_net = dev2net(hso_dev);
  1627. if (!hso_net)
  1628. return -ENODEV;
  1629. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1630. if (hso_net->mux_bulk_rx_urb_pool[i])
  1631. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1632. }
  1633. if (hso_net->mux_bulk_tx_urb)
  1634. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1635. return 0;
  1636. }
  1637. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1638. {
  1639. int i, result = 0;
  1640. struct hso_serial *serial = dev2ser(hso_dev);
  1641. if (!serial)
  1642. return -ENODEV;
  1643. /* If it is not the MUX port fill in and submit a bulk urb (already
  1644. * allocated in hso_serial_start) */
  1645. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1646. for (i = 0; i < serial->num_rx_urbs; i++) {
  1647. usb_fill_bulk_urb(serial->rx_urb[i],
  1648. serial->parent->usb,
  1649. usb_rcvbulkpipe(serial->parent->usb,
  1650. serial->in_endp->
  1651. bEndpointAddress &
  1652. 0x7F),
  1653. serial->rx_data[i],
  1654. serial->rx_data_length,
  1655. hso_std_serial_read_bulk_callback,
  1656. serial);
  1657. result = usb_submit_urb(serial->rx_urb[i], flags);
  1658. if (result) {
  1659. dev_warn(&serial->parent->usb->dev,
  1660. "Failed to submit urb - res %d\n",
  1661. result);
  1662. break;
  1663. }
  1664. }
  1665. } else {
  1666. mutex_lock(&serial->shared_int->shared_int_lock);
  1667. if (!serial->shared_int->use_count) {
  1668. result =
  1669. hso_mux_submit_intr_urb(serial->shared_int,
  1670. hso_dev->usb, flags);
  1671. }
  1672. serial->shared_int->use_count++;
  1673. mutex_unlock(&serial->shared_int->shared_int_lock);
  1674. }
  1675. return result;
  1676. }
  1677. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1678. {
  1679. int i;
  1680. struct hso_serial *serial = dev2ser(hso_dev);
  1681. if (!serial)
  1682. return -ENODEV;
  1683. for (i = 0; i < serial->num_rx_urbs; i++) {
  1684. if (serial->rx_urb[i]) {
  1685. usb_kill_urb(serial->rx_urb[i]);
  1686. serial->rx_urb_filled[i] = 0;
  1687. }
  1688. }
  1689. serial->curr_rx_urb_idx = 0;
  1690. serial->curr_rx_urb_offset = 0;
  1691. if (serial->tx_urb)
  1692. usb_kill_urb(serial->tx_urb);
  1693. if (serial->shared_int) {
  1694. mutex_lock(&serial->shared_int->shared_int_lock);
  1695. if (serial->shared_int->use_count &&
  1696. (--serial->shared_int->use_count == 0)) {
  1697. struct urb *urb;
  1698. urb = serial->shared_int->shared_intr_urb;
  1699. if (urb)
  1700. usb_kill_urb(urb);
  1701. }
  1702. mutex_unlock(&serial->shared_int->shared_int_lock);
  1703. }
  1704. return 0;
  1705. }
  1706. static void hso_serial_common_free(struct hso_serial *serial)
  1707. {
  1708. int i;
  1709. if (serial->parent->dev)
  1710. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1711. tty_unregister_device(tty_drv, serial->minor);
  1712. for (i = 0; i < serial->num_rx_urbs; i++) {
  1713. /* unlink and free RX URB */
  1714. usb_free_urb(serial->rx_urb[i]);
  1715. /* free the RX buffer */
  1716. kfree(serial->rx_data[i]);
  1717. }
  1718. /* unlink and free TX URB */
  1719. usb_free_urb(serial->tx_urb);
  1720. kfree(serial->tx_data);
  1721. }
  1722. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1723. int rx_size, int tx_size)
  1724. {
  1725. struct device *dev;
  1726. int minor;
  1727. int i;
  1728. minor = get_free_serial_index();
  1729. if (minor < 0)
  1730. goto exit;
  1731. /* register our minor number */
  1732. serial->parent->dev = tty_register_device(tty_drv, minor,
  1733. &serial->parent->interface->dev);
  1734. dev = serial->parent->dev;
  1735. dev->driver_data = serial->parent;
  1736. i = device_create_file(dev, &dev_attr_hsotype);
  1737. /* fill in specific data for later use */
  1738. serial->minor = minor;
  1739. serial->magic = HSO_SERIAL_MAGIC;
  1740. spin_lock_init(&serial->serial_lock);
  1741. serial->num_rx_urbs = num_urbs;
  1742. /* RX, allocate urb and initialize */
  1743. /* prepare our RX buffer */
  1744. serial->rx_data_length = rx_size;
  1745. for (i = 0; i < serial->num_rx_urbs; i++) {
  1746. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1747. if (!serial->rx_urb[i]) {
  1748. dev_err(dev, "Could not allocate urb?\n");
  1749. goto exit;
  1750. }
  1751. serial->rx_urb[i]->transfer_buffer = NULL;
  1752. serial->rx_urb[i]->transfer_buffer_length = 0;
  1753. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1754. GFP_KERNEL);
  1755. if (!serial->rx_data[i]) {
  1756. dev_err(dev, "%s - Out of memory\n", __func__);
  1757. goto exit;
  1758. }
  1759. }
  1760. /* TX, allocate urb and initialize */
  1761. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1762. if (!serial->tx_urb) {
  1763. dev_err(dev, "Could not allocate urb?\n");
  1764. goto exit;
  1765. }
  1766. serial->tx_urb->transfer_buffer = NULL;
  1767. serial->tx_urb->transfer_buffer_length = 0;
  1768. /* prepare our TX buffer */
  1769. serial->tx_data_count = 0;
  1770. serial->tx_buffer_count = 0;
  1771. serial->tx_data_length = tx_size;
  1772. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1773. if (!serial->tx_data) {
  1774. dev_err(dev, "%s - Out of memory", __func__);
  1775. goto exit;
  1776. }
  1777. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1778. if (!serial->tx_buffer) {
  1779. dev_err(dev, "%s - Out of memory", __func__);
  1780. goto exit;
  1781. }
  1782. return 0;
  1783. exit:
  1784. hso_serial_common_free(serial);
  1785. return -1;
  1786. }
  1787. /* Frees a general hso device */
  1788. static void hso_free_device(struct hso_device *hso_dev)
  1789. {
  1790. kfree(hso_dev);
  1791. }
  1792. /* Creates a general hso device */
  1793. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1794. int port_spec)
  1795. {
  1796. struct hso_device *hso_dev;
  1797. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  1798. if (!hso_dev)
  1799. return NULL;
  1800. hso_dev->port_spec = port_spec;
  1801. hso_dev->usb = interface_to_usbdev(intf);
  1802. hso_dev->interface = intf;
  1803. kref_init(&hso_dev->ref);
  1804. mutex_init(&hso_dev->mutex);
  1805. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1806. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1807. return hso_dev;
  1808. }
  1809. /* Removes a network device in the network device table */
  1810. static int remove_net_device(struct hso_device *hso_dev)
  1811. {
  1812. int i;
  1813. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1814. if (network_table[i] == hso_dev) {
  1815. network_table[i] = NULL;
  1816. break;
  1817. }
  1818. }
  1819. if (i == HSO_MAX_NET_DEVICES)
  1820. return -1;
  1821. return 0;
  1822. }
  1823. /* Frees our network device */
  1824. static void hso_free_net_device(struct hso_device *hso_dev)
  1825. {
  1826. int i;
  1827. struct hso_net *hso_net = dev2net(hso_dev);
  1828. if (!hso_net)
  1829. return;
  1830. /* start freeing */
  1831. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1832. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1833. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  1834. }
  1835. usb_free_urb(hso_net->mux_bulk_tx_urb);
  1836. kfree(hso_net->mux_bulk_tx_buf);
  1837. remove_net_device(hso_net->parent);
  1838. if (hso_net->net) {
  1839. unregister_netdev(hso_net->net);
  1840. free_netdev(hso_net->net);
  1841. }
  1842. hso_free_device(hso_dev);
  1843. }
  1844. /* initialize the network interface */
  1845. static void hso_net_init(struct net_device *net)
  1846. {
  1847. struct hso_net *hso_net = netdev_priv(net);
  1848. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  1849. /* fill in the other fields */
  1850. net->open = hso_net_open;
  1851. net->stop = hso_net_close;
  1852. net->hard_start_xmit = hso_net_start_xmit;
  1853. net->tx_timeout = hso_net_tx_timeout;
  1854. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  1855. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1856. net->type = ARPHRD_NONE;
  1857. net->mtu = DEFAULT_MTU - 14;
  1858. net->tx_queue_len = 10;
  1859. SET_ETHTOOL_OPS(net, &ops);
  1860. /* and initialize the semaphore */
  1861. spin_lock_init(&hso_net->net_lock);
  1862. }
  1863. /* Adds a network device in the network device table */
  1864. static int add_net_device(struct hso_device *hso_dev)
  1865. {
  1866. int i;
  1867. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1868. if (network_table[i] == NULL) {
  1869. network_table[i] = hso_dev;
  1870. break;
  1871. }
  1872. }
  1873. if (i == HSO_MAX_NET_DEVICES)
  1874. return -1;
  1875. return 0;
  1876. }
  1877. static int hso_radio_toggle(void *data, enum rfkill_state state)
  1878. {
  1879. struct hso_device *hso_dev = data;
  1880. int enabled = (state == RFKILL_STATE_ON);
  1881. int rv;
  1882. mutex_lock(&hso_dev->mutex);
  1883. if (hso_dev->usb_gone)
  1884. rv = 0;
  1885. else
  1886. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  1887. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  1888. USB_CTRL_SET_TIMEOUT);
  1889. mutex_unlock(&hso_dev->mutex);
  1890. return rv;
  1891. }
  1892. /* Creates and sets up everything for rfkill */
  1893. static void hso_create_rfkill(struct hso_device *hso_dev,
  1894. struct usb_interface *interface)
  1895. {
  1896. struct hso_net *hso_net = dev2net(hso_dev);
  1897. struct device *dev = &hso_net->net->dev;
  1898. char *rfkn;
  1899. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  1900. RFKILL_TYPE_WWAN);
  1901. if (!hso_net->rfkill) {
  1902. dev_err(dev, "%s - Out of memory\n", __func__);
  1903. return;
  1904. }
  1905. rfkn = kzalloc(20, GFP_KERNEL);
  1906. if (!rfkn) {
  1907. rfkill_free(hso_net->rfkill);
  1908. hso_net->rfkill = NULL;
  1909. dev_err(dev, "%s - Out of memory\n", __func__);
  1910. return;
  1911. }
  1912. snprintf(rfkn, 20, "hso-%d",
  1913. interface->altsetting->desc.bInterfaceNumber);
  1914. hso_net->rfkill->name = rfkn;
  1915. hso_net->rfkill->state = RFKILL_STATE_ON;
  1916. hso_net->rfkill->data = hso_dev;
  1917. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  1918. if (rfkill_register(hso_net->rfkill) < 0) {
  1919. kfree(rfkn);
  1920. hso_net->rfkill->name = NULL;
  1921. rfkill_free(hso_net->rfkill);
  1922. hso_net->rfkill = NULL;
  1923. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  1924. return;
  1925. }
  1926. }
  1927. /* Creates our network device */
  1928. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  1929. {
  1930. int result, i;
  1931. struct net_device *net;
  1932. struct hso_net *hso_net;
  1933. struct hso_device *hso_dev;
  1934. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  1935. if (!hso_dev)
  1936. return NULL;
  1937. /* allocate our network device, then we can put in our private data */
  1938. /* call hso_net_init to do the basic initialization */
  1939. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  1940. if (!net) {
  1941. dev_err(&interface->dev, "Unable to create ethernet device\n");
  1942. goto exit;
  1943. }
  1944. hso_net = netdev_priv(net);
  1945. hso_dev->port_data.dev_net = hso_net;
  1946. hso_net->net = net;
  1947. hso_net->parent = hso_dev;
  1948. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1949. USB_DIR_IN);
  1950. if (!hso_net->in_endp) {
  1951. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  1952. goto exit;
  1953. }
  1954. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1955. USB_DIR_OUT);
  1956. if (!hso_net->out_endp) {
  1957. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  1958. goto exit;
  1959. }
  1960. SET_NETDEV_DEV(net, &interface->dev);
  1961. /* registering our net device */
  1962. result = register_netdev(net);
  1963. if (result) {
  1964. dev_err(&interface->dev, "Failed to register device\n");
  1965. goto exit;
  1966. }
  1967. /* start allocating */
  1968. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1969. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  1970. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  1971. dev_err(&interface->dev, "Could not allocate rx urb\n");
  1972. goto exit;
  1973. }
  1974. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  1975. GFP_KERNEL);
  1976. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  1977. dev_err(&interface->dev, "Could not allocate rx buf\n");
  1978. goto exit;
  1979. }
  1980. }
  1981. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1982. if (!hso_net->mux_bulk_tx_urb) {
  1983. dev_err(&interface->dev, "Could not allocate tx urb\n");
  1984. goto exit;
  1985. }
  1986. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  1987. if (!hso_net->mux_bulk_tx_buf) {
  1988. dev_err(&interface->dev, "Could not allocate tx buf\n");
  1989. goto exit;
  1990. }
  1991. add_net_device(hso_dev);
  1992. hso_log_port(hso_dev);
  1993. hso_create_rfkill(hso_dev, interface);
  1994. return hso_dev;
  1995. exit:
  1996. hso_free_net_device(hso_dev);
  1997. return NULL;
  1998. }
  1999. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2000. static void hso_free_serial_device(struct hso_device *hso_dev)
  2001. {
  2002. struct hso_serial *serial = dev2ser(hso_dev);
  2003. if (!serial)
  2004. return;
  2005. set_serial_by_index(serial->minor, NULL);
  2006. hso_serial_common_free(serial);
  2007. if (serial->shared_int) {
  2008. mutex_lock(&serial->shared_int->shared_int_lock);
  2009. if (--serial->shared_int->ref_count == 0)
  2010. hso_free_shared_int(serial->shared_int);
  2011. else
  2012. mutex_unlock(&serial->shared_int->shared_int_lock);
  2013. }
  2014. kfree(serial);
  2015. hso_free_device(hso_dev);
  2016. }
  2017. /* Creates a bulk AT channel */
  2018. static struct hso_device *hso_create_bulk_serial_device(
  2019. struct usb_interface *interface, int port)
  2020. {
  2021. struct hso_device *hso_dev;
  2022. struct hso_serial *serial;
  2023. int num_urbs;
  2024. hso_dev = hso_create_device(interface, port);
  2025. if (!hso_dev)
  2026. return NULL;
  2027. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2028. if (!serial)
  2029. goto exit;
  2030. serial->parent = hso_dev;
  2031. hso_dev->port_data.dev_serial = serial;
  2032. if (port & HSO_PORT_MODEM)
  2033. num_urbs = 2;
  2034. else
  2035. num_urbs = 1;
  2036. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2037. BULK_URB_TX_SIZE))
  2038. goto exit;
  2039. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2040. USB_DIR_IN);
  2041. if (!serial->in_endp) {
  2042. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2043. goto exit2;
  2044. }
  2045. if (!
  2046. (serial->out_endp =
  2047. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2048. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2049. goto exit2;
  2050. }
  2051. serial->write_data = hso_std_serial_write_data;
  2052. /* and record this serial */
  2053. set_serial_by_index(serial->minor, serial);
  2054. /* setup the proc dirs and files if needed */
  2055. hso_log_port(hso_dev);
  2056. /* done, return it */
  2057. return hso_dev;
  2058. exit2:
  2059. hso_serial_common_free(serial);
  2060. exit:
  2061. kfree(serial);
  2062. hso_free_device(hso_dev);
  2063. return NULL;
  2064. }
  2065. /* Creates a multiplexed AT channel */
  2066. static
  2067. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2068. int port,
  2069. struct hso_shared_int *mux)
  2070. {
  2071. struct hso_device *hso_dev;
  2072. struct hso_serial *serial;
  2073. int port_spec;
  2074. port_spec = HSO_INTF_MUX;
  2075. port_spec &= ~HSO_PORT_MASK;
  2076. port_spec |= hso_mux_to_port(port);
  2077. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2078. return NULL;
  2079. hso_dev = hso_create_device(interface, port_spec);
  2080. if (!hso_dev)
  2081. return NULL;
  2082. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2083. if (!serial)
  2084. goto exit;
  2085. hso_dev->port_data.dev_serial = serial;
  2086. serial->parent = hso_dev;
  2087. if (hso_serial_common_create
  2088. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2089. goto exit;
  2090. serial->tx_data_length--;
  2091. serial->write_data = hso_mux_serial_write_data;
  2092. serial->shared_int = mux;
  2093. mutex_lock(&serial->shared_int->shared_int_lock);
  2094. serial->shared_int->ref_count++;
  2095. mutex_unlock(&serial->shared_int->shared_int_lock);
  2096. /* and record this serial */
  2097. set_serial_by_index(serial->minor, serial);
  2098. /* setup the proc dirs and files if needed */
  2099. hso_log_port(hso_dev);
  2100. /* done, return it */
  2101. return hso_dev;
  2102. exit:
  2103. if (serial) {
  2104. tty_unregister_device(tty_drv, serial->minor);
  2105. kfree(serial);
  2106. }
  2107. if (hso_dev)
  2108. hso_free_device(hso_dev);
  2109. return NULL;
  2110. }
  2111. static void hso_free_shared_int(struct hso_shared_int *mux)
  2112. {
  2113. usb_free_urb(mux->shared_intr_urb);
  2114. kfree(mux->shared_intr_buf);
  2115. mutex_unlock(&mux->shared_int_lock);
  2116. kfree(mux);
  2117. }
  2118. static
  2119. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2120. {
  2121. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2122. if (!mux)
  2123. return NULL;
  2124. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2125. USB_DIR_IN);
  2126. if (!mux->intr_endp) {
  2127. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2128. goto exit;
  2129. }
  2130. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2131. if (!mux->shared_intr_urb) {
  2132. dev_err(&interface->dev, "Could not allocate intr urb?");
  2133. goto exit;
  2134. }
  2135. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2136. GFP_KERNEL);
  2137. if (!mux->shared_intr_buf) {
  2138. dev_err(&interface->dev, "Could not allocate intr buf?");
  2139. goto exit;
  2140. }
  2141. mutex_init(&mux->shared_int_lock);
  2142. return mux;
  2143. exit:
  2144. kfree(mux->shared_intr_buf);
  2145. usb_free_urb(mux->shared_intr_urb);
  2146. kfree(mux);
  2147. return NULL;
  2148. }
  2149. /* Gets the port spec for a certain interface */
  2150. static int hso_get_config_data(struct usb_interface *interface)
  2151. {
  2152. struct usb_device *usbdev = interface_to_usbdev(interface);
  2153. u8 config_data[17];
  2154. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2155. s32 result;
  2156. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2157. 0x86, 0xC0, 0, 0, config_data, 17,
  2158. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2159. return -EIO;
  2160. }
  2161. switch (config_data[if_num]) {
  2162. case 0x0:
  2163. result = 0;
  2164. break;
  2165. case 0x1:
  2166. result = HSO_PORT_DIAG;
  2167. break;
  2168. case 0x2:
  2169. result = HSO_PORT_GPS;
  2170. break;
  2171. case 0x3:
  2172. result = HSO_PORT_GPS_CONTROL;
  2173. break;
  2174. case 0x4:
  2175. result = HSO_PORT_APP;
  2176. break;
  2177. case 0x5:
  2178. result = HSO_PORT_APP2;
  2179. break;
  2180. case 0x6:
  2181. result = HSO_PORT_CONTROL;
  2182. break;
  2183. case 0x7:
  2184. result = HSO_PORT_NETWORK;
  2185. break;
  2186. case 0x8:
  2187. result = HSO_PORT_MODEM;
  2188. break;
  2189. case 0x9:
  2190. result = HSO_PORT_MSD;
  2191. break;
  2192. case 0xa:
  2193. result = HSO_PORT_PCSC;
  2194. break;
  2195. case 0xb:
  2196. result = HSO_PORT_VOICE;
  2197. break;
  2198. default:
  2199. result = 0;
  2200. }
  2201. if (result)
  2202. result |= HSO_INTF_BULK;
  2203. if (config_data[16] & 0x1)
  2204. result |= HSO_INFO_CRC_BUG;
  2205. return result;
  2206. }
  2207. /* called once for each interface upon device insertion */
  2208. static int hso_probe(struct usb_interface *interface,
  2209. const struct usb_device_id *id)
  2210. {
  2211. int mux, i, if_num, port_spec;
  2212. unsigned char port_mask;
  2213. struct hso_device *hso_dev = NULL;
  2214. struct hso_shared_int *shared_int;
  2215. struct hso_device *tmp_dev = NULL;
  2216. if_num = interface->altsetting->desc.bInterfaceNumber;
  2217. /* Get the interface/port specification from either driver_info or from
  2218. * the device itself */
  2219. if (id->driver_info)
  2220. port_spec = ((u32 *)(id->driver_info))[if_num];
  2221. else
  2222. port_spec = hso_get_config_data(interface);
  2223. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2224. dev_err(&interface->dev, "Not our interface\n");
  2225. return -ENODEV;
  2226. }
  2227. /* Check if we need to switch to alt interfaces prior to port
  2228. * configuration */
  2229. if (interface->num_altsetting > 1)
  2230. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2231. interface->needs_remote_wakeup = 1;
  2232. /* Allocate new hso device(s) */
  2233. switch (port_spec & HSO_INTF_MASK) {
  2234. case HSO_INTF_MUX:
  2235. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2236. /* Create the network device */
  2237. if (!disable_net) {
  2238. hso_dev = hso_create_net_device(interface);
  2239. if (!hso_dev)
  2240. goto exit;
  2241. tmp_dev = hso_dev;
  2242. }
  2243. }
  2244. if (hso_get_mux_ports(interface, &port_mask))
  2245. /* TODO: de-allocate everything */
  2246. goto exit;
  2247. shared_int = hso_create_shared_int(interface);
  2248. if (!shared_int)
  2249. goto exit;
  2250. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2251. if (port_mask & i) {
  2252. hso_dev = hso_create_mux_serial_device(
  2253. interface, i, shared_int);
  2254. if (!hso_dev)
  2255. goto exit;
  2256. }
  2257. }
  2258. if (tmp_dev)
  2259. hso_dev = tmp_dev;
  2260. break;
  2261. case HSO_INTF_BULK:
  2262. /* It's a regular bulk interface */
  2263. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2264. && !disable_net)
  2265. hso_dev = hso_create_net_device(interface);
  2266. else
  2267. hso_dev =
  2268. hso_create_bulk_serial_device(interface, port_spec);
  2269. if (!hso_dev)
  2270. goto exit;
  2271. break;
  2272. default:
  2273. goto exit;
  2274. }
  2275. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2276. /* save our data pointer in this device */
  2277. usb_set_intfdata(interface, hso_dev);
  2278. /* done */
  2279. return 0;
  2280. exit:
  2281. hso_free_interface(interface);
  2282. return -ENODEV;
  2283. }
  2284. /* device removed, cleaning up */
  2285. static void hso_disconnect(struct usb_interface *interface)
  2286. {
  2287. hso_free_interface(interface);
  2288. /* remove reference of our private data */
  2289. usb_set_intfdata(interface, NULL);
  2290. usb_driver_release_interface(&hso_driver, interface);
  2291. }
  2292. static void async_get_intf(struct work_struct *data)
  2293. {
  2294. struct hso_device *hso_dev =
  2295. container_of(data, struct hso_device, async_get_intf);
  2296. usb_autopm_get_interface(hso_dev->interface);
  2297. }
  2298. static void async_put_intf(struct work_struct *data)
  2299. {
  2300. struct hso_device *hso_dev =
  2301. container_of(data, struct hso_device, async_put_intf);
  2302. usb_autopm_put_interface(hso_dev->interface);
  2303. }
  2304. static int hso_get_activity(struct hso_device *hso_dev)
  2305. {
  2306. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2307. if (!hso_dev->is_active) {
  2308. hso_dev->is_active = 1;
  2309. schedule_work(&hso_dev->async_get_intf);
  2310. }
  2311. }
  2312. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2313. return -EAGAIN;
  2314. usb_mark_last_busy(hso_dev->usb);
  2315. return 0;
  2316. }
  2317. static int hso_put_activity(struct hso_device *hso_dev)
  2318. {
  2319. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2320. if (hso_dev->is_active) {
  2321. hso_dev->is_active = 0;
  2322. schedule_work(&hso_dev->async_put_intf);
  2323. return -EAGAIN;
  2324. }
  2325. }
  2326. hso_dev->is_active = 0;
  2327. return 0;
  2328. }
  2329. /* called by kernel when we need to suspend device */
  2330. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2331. {
  2332. int i, result;
  2333. /* Stop all serial ports */
  2334. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2335. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2336. result = hso_stop_serial_device(serial_table[i]);
  2337. if (result)
  2338. goto out;
  2339. }
  2340. }
  2341. /* Stop all network ports */
  2342. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2343. if (network_table[i] &&
  2344. (network_table[i]->interface == iface)) {
  2345. result = hso_stop_net_device(network_table[i]);
  2346. if (result)
  2347. goto out;
  2348. }
  2349. }
  2350. out:
  2351. return 0;
  2352. }
  2353. /* called by kernel when we need to resume device */
  2354. static int hso_resume(struct usb_interface *iface)
  2355. {
  2356. int i, result = 0;
  2357. struct hso_net *hso_net;
  2358. /* Start all serial ports */
  2359. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2360. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2361. if (dev2ser(serial_table[i])->open_count) {
  2362. result =
  2363. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2364. hso_kick_transmit(dev2ser(serial_table[i]));
  2365. if (result)
  2366. goto out;
  2367. }
  2368. }
  2369. }
  2370. /* Start all network ports */
  2371. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2372. if (network_table[i] &&
  2373. (network_table[i]->interface == iface)) {
  2374. hso_net = dev2net(network_table[i]);
  2375. if (hso_net->flags & IFF_UP) {
  2376. /* First transmit any lingering data,
  2377. then restart the device. */
  2378. if (hso_net->skb_tx_buf) {
  2379. dev_dbg(&iface->dev,
  2380. "Transmitting"
  2381. " lingering data\n");
  2382. hso_net_start_xmit(hso_net->skb_tx_buf,
  2383. hso_net->net);
  2384. hso_net->skb_tx_buf = NULL;
  2385. }
  2386. result = hso_start_net_device(network_table[i]);
  2387. if (result)
  2388. goto out;
  2389. }
  2390. }
  2391. }
  2392. out:
  2393. return result;
  2394. }
  2395. static void hso_serial_ref_free(struct kref *ref)
  2396. {
  2397. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2398. hso_free_serial_device(hso_dev);
  2399. }
  2400. static void hso_free_interface(struct usb_interface *interface)
  2401. {
  2402. struct hso_serial *hso_dev;
  2403. struct tty_struct *tty;
  2404. int i;
  2405. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2406. if (serial_table[i]
  2407. && (serial_table[i]->interface == interface)) {
  2408. hso_dev = dev2ser(serial_table[i]);
  2409. spin_lock_irq(&hso_dev->serial_lock);
  2410. tty = tty_kref_get(hso_dev->tty);
  2411. spin_unlock_irq(&hso_dev->serial_lock);
  2412. if (tty)
  2413. tty_hangup(tty);
  2414. mutex_lock(&hso_dev->parent->mutex);
  2415. tty_kref_put(tty);
  2416. hso_dev->parent->usb_gone = 1;
  2417. mutex_unlock(&hso_dev->parent->mutex);
  2418. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2419. }
  2420. }
  2421. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2422. if (network_table[i]
  2423. && (network_table[i]->interface == interface)) {
  2424. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2425. /* hso_stop_net_device doesn't stop the net queue since
  2426. * traffic needs to start it again when suspended */
  2427. netif_stop_queue(dev2net(network_table[i])->net);
  2428. hso_stop_net_device(network_table[i]);
  2429. cancel_work_sync(&network_table[i]->async_put_intf);
  2430. cancel_work_sync(&network_table[i]->async_get_intf);
  2431. if (rfk)
  2432. rfkill_unregister(rfk);
  2433. hso_free_net_device(network_table[i]);
  2434. }
  2435. }
  2436. }
  2437. /* Helper functions */
  2438. /* Get the endpoint ! */
  2439. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2440. int type, int dir)
  2441. {
  2442. int i;
  2443. struct usb_host_interface *iface = intf->cur_altsetting;
  2444. struct usb_endpoint_descriptor *endp;
  2445. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2446. endp = &iface->endpoint[i].desc;
  2447. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2448. (usb_endpoint_type(endp) == type))
  2449. return endp;
  2450. }
  2451. return NULL;
  2452. }
  2453. /* Get the byte that describes which ports are enabled */
  2454. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2455. {
  2456. int i;
  2457. struct usb_host_interface *iface = intf->cur_altsetting;
  2458. if (iface->extralen == 3) {
  2459. *ports = iface->extra[2];
  2460. return 0;
  2461. }
  2462. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2463. if (iface->endpoint[i].extralen == 3) {
  2464. *ports = iface->endpoint[i].extra[2];
  2465. return 0;
  2466. }
  2467. }
  2468. return -1;
  2469. }
  2470. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2471. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2472. struct usb_device *usb, gfp_t gfp)
  2473. {
  2474. int result;
  2475. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2476. usb_rcvintpipe(usb,
  2477. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2478. shared_int->shared_intr_buf,
  2479. shared_int->intr_endp->wMaxPacketSize,
  2480. intr_callback, shared_int,
  2481. shared_int->intr_endp->bInterval);
  2482. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2483. if (result)
  2484. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2485. result);
  2486. return result;
  2487. }
  2488. /* operations setup of the serial interface */
  2489. static const struct tty_operations hso_serial_ops = {
  2490. .open = hso_serial_open,
  2491. .close = hso_serial_close,
  2492. .write = hso_serial_write,
  2493. .write_room = hso_serial_write_room,
  2494. .set_termios = hso_serial_set_termios,
  2495. .chars_in_buffer = hso_serial_chars_in_buffer,
  2496. .tiocmget = hso_serial_tiocmget,
  2497. .tiocmset = hso_serial_tiocmset,
  2498. .unthrottle = hso_unthrottle
  2499. };
  2500. static struct usb_driver hso_driver = {
  2501. .name = driver_name,
  2502. .probe = hso_probe,
  2503. .disconnect = hso_disconnect,
  2504. .id_table = hso_ids,
  2505. .suspend = hso_suspend,
  2506. .resume = hso_resume,
  2507. .reset_resume = hso_resume,
  2508. .supports_autosuspend = 1,
  2509. };
  2510. static int __init hso_init(void)
  2511. {
  2512. int i;
  2513. int result;
  2514. /* put it in the log */
  2515. printk(KERN_INFO "hso: %s\n", version);
  2516. /* Initialise the serial table semaphore and table */
  2517. spin_lock_init(&serial_table_lock);
  2518. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2519. serial_table[i] = NULL;
  2520. /* allocate our driver using the proper amount of supported minors */
  2521. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2522. if (!tty_drv)
  2523. return -ENOMEM;
  2524. /* fill in all needed values */
  2525. tty_drv->magic = TTY_DRIVER_MAGIC;
  2526. tty_drv->owner = THIS_MODULE;
  2527. tty_drv->driver_name = driver_name;
  2528. tty_drv->name = tty_filename;
  2529. /* if major number is provided as parameter, use that one */
  2530. if (tty_major)
  2531. tty_drv->major = tty_major;
  2532. tty_drv->minor_start = 0;
  2533. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2534. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2535. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2536. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2537. tty_drv->init_termios = tty_std_termios;
  2538. hso_init_termios(&tty_drv->init_termios);
  2539. tty_set_operations(tty_drv, &hso_serial_ops);
  2540. /* register the tty driver */
  2541. result = tty_register_driver(tty_drv);
  2542. if (result) {
  2543. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2544. __func__, result);
  2545. return result;
  2546. }
  2547. /* register this module as an usb driver */
  2548. result = usb_register(&hso_driver);
  2549. if (result) {
  2550. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2551. result);
  2552. /* cleanup serial interface */
  2553. tty_unregister_driver(tty_drv);
  2554. return result;
  2555. }
  2556. /* done */
  2557. return 0;
  2558. }
  2559. static void __exit hso_exit(void)
  2560. {
  2561. printk(KERN_INFO "hso: unloaded\n");
  2562. tty_unregister_driver(tty_drv);
  2563. /* deregister the usb driver */
  2564. usb_deregister(&hso_driver);
  2565. }
  2566. /* Module definitions */
  2567. module_init(hso_init);
  2568. module_exit(hso_exit);
  2569. MODULE_AUTHOR(MOD_AUTHOR);
  2570. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2571. MODULE_LICENSE(MOD_LICENSE);
  2572. MODULE_INFO(Version, DRIVER_VERSION);
  2573. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2574. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2575. module_param(debug, int, S_IRUGO | S_IWUSR);
  2576. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2577. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2578. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2579. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2580. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2581. module_param(disable_net, int, S_IRUGO | S_IWUSR);